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