001    /*
002    @license.text@
003     */
004    package biz.hammurapi.ant;
005    
006    import java.io.File;
007    import java.io.IOException;
008    import java.io.InputStream;
009    import java.net.URL;
010    import java.util.ArrayList;
011    import java.util.Collection;
012    import java.util.Iterator;
013    
014    import org.apache.tools.ant.BuildException;
015    import org.apache.tools.ant.Task;
016    
017    import biz.hammurapi.config.Command;
018    import biz.hammurapi.config.ConfigurationException;
019    import biz.hammurapi.config.Context;
020    import biz.hammurapi.config.DomConfigFactory;
021    import biz.hammurapi.config.DomConfigurableContainer;
022    
023    
024    /**
025     * Container wrapped in Ant task.
026     * Instantiates container, mounts /logger and /taks and then
027     * executes nested <command> entries
028     * @ant.task name="Container" category="Common"
029     * @author Pavel Vlasov
030     * @revision $Revision$
031     */
032    public class ContainerTask extends Task {
033            private Collection executes=new ArrayList();
034            
035            /**
036             * Context to "execute".
037             * @ant.type name="Execute" category="Common"
038             * @author Pavel Vlasov
039             * @revision $Revision$
040             */
041            public class Execute extends ObjectEntry {
042                    private String path;
043                    
044                    /**
045                     * Path to component to execute
046                     * @ant.required
047                     * @param path
048                     */
049                    public void setPath(String path) {
050                            this.path=path;
051                    }
052            }
053            
054            /**
055             * Execute command
056             * @return
057             */
058            public Execute createExecute() {
059                    Execute ret=new Execute();
060                    executes.add(ret);
061                    return ret;
062            }
063            
064            public ContainerTask() {
065                    super();
066            }
067            
068            private String url;
069            private File file;
070            private String resource;
071            
072            /**
073             * URL to load configuration from. 
074             * One of URL, file or resource is mandatory. 
075             * These attributes are mutually exclusive.
076             * @ant.not-required
077             * @param url
078             */
079            public void setUrl(String url) {
080                    checkAlreadySet();
081                    this.url=url;
082            }
083            
084            /**
085             * File to load configuration from. 
086             * One of URL, file or resource is mandatory. 
087             * These attributes are mutually exclusive.
088             * @ant.not-required
089             * @param url
090             */
091            public void setFile(File file) {
092                    checkAlreadySet();
093                    this.file=file;
094            }
095            
096            /**
097             * Classloader resource to load configuration from. 
098             * One of URL, file or resource is mandatory. 
099             * These attributes are mutually exclusive.
100             * @ant.not-required
101             * @param url
102             */
103            public void setResource(String resource) {
104                    checkAlreadySet();
105                    this.resource=resource;
106            }
107            
108            private void checkAlreadySet() {
109                    if (url!=null) {
110                            throw new BuildException("url already set");
111                    }
112                    
113                    if (file!=null) {
114                            throw new BuildException("file already set");
115                    }
116                    
117                    if (resource!=null) {
118                            throw new BuildException("resource already set");
119                    }
120            }
121            
122            public void execute() throws BuildException {
123                    try {
124                            ClassLoader classLoader = getClass().getClassLoader();
125                            DomConfigFactory factory=new DomConfigFactory(classLoader, (Context) null);
126                            Object o;
127                            if (url!=null) {
128                                    o=factory.create(new URL(url), null);
129                            } else if (file!=null) {
130                                    o=factory.create(file, null);                   
131                            } else if (resource!=null) {
132                                    InputStream in=classLoader.getResourceAsStream(resource);
133                                    if (in==null) {
134                                            throw new BuildException("Resource "+resource+" not found");
135                                    }
136                                    o=factory.create(in, null);                     
137                            } else {
138                                    throw new BuildException("One of url, file or resource attributes must be set");
139                            }
140                            
141                            if (o instanceof DomConfigurableContainer) {
142                                    DomConfigurableContainer container=(DomConfigurableContainer) o;
143                                    if (container.get("task")==null) {
144                                            container.addComponent("task", this); 
145                                    }
146                                    
147                                    container.start();
148                                    try {
149                                            Iterator it=executes.iterator();
150                                            while (it.hasNext()) {
151                                                    Execute execute=(Execute) it.next();
152                                                    Object target=container.get(execute.path);
153                                                    if (target instanceof Command) {
154                                                            ((Command) target).execute(execute.getObject(classLoader));
155                                                    }
156                                            }
157                                    } finally {
158                                            container.stop();
159                                    }
160                            }                       
161                    } catch (IOException e) {
162                            throw new BuildException(e.toString(), e);
163                    } catch (ConfigurationException e) {
164                            throw new BuildException(e.toString(), e);
165                    }
166                    
167            }
168            
169            
170    }