1 package org.apache.beehive.controls.runtime.assembly; 2 3 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.TreeSet ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.Task; 34 import org.apache.tools.ant.types.Path; 35 import org.apache.tools.ant.types.FileSet; 36 import org.apache.beehive.controls.runtime.generator.apt.ControlClientManifest; 37 import org.apache.beehive.controls.api.assembly.ControlAssemblyException; 38 39 79 public class AssembleTask extends Task 80 { 81 public AssembleTask() 82 { 83 } 85 86 public void setContextFactoryClassName(String contextFactoryClassName) 87 { 88 _contextFactoryClassName = contextFactoryClassName; 89 } 90 91 public void setModuleDir( File moduleDir ) 92 { 93 _moduleDir = moduleDir; 94 } 95 96 public void setModuleName( String moduleName ) 97 { 98 _moduleName = moduleName; 99 } 100 101 public void setSrcOutputDir( File srcOutputDir ) 102 { 103 _srcOutputDir = srcOutputDir; 104 } 105 106 public void setBindingFile(File bindingFile) 107 { 108 _bindingFile = bindingFile; 109 } 110 111 public FileSet createFileset() 112 { 113 _clientManifestFileSet = new FileSet(); 114 return _clientManifestFileSet; 115 } 116 117 public void setClasspath(Path classpath) 119 { 120 _classPath = new Path(getProject()); 121 _classPath.append(classpath); 122 } 123 124 public Path createClasspath() 126 { 127 _classPath = new Path(getProject()); 128 return _classPath; 129 } 130 131 public void execute() 132 { 133 validateAttributeSettings(); 134 135 if (_clientManifestFileSet == null) 136 { 137 log("No input fileset specified, nothing to do."); 138 return; 139 } 140 141 File filesetDir = _clientManifestFileSet.getDir(getProject()); 143 String [] clientManifests = _clientManifestFileSet. 144 getDirectoryScanner(getProject()).getIncludedFiles(); 145 146 if (clientManifests.length == 0) 147 { 148 log("Input fileset contained no files, nothing to do."); 149 return; 150 } 151 152 List <File > manifestFiles = new ArrayList <File >(); 153 for ( String mf : clientManifests ) 154 { 155 File f = new File (filesetDir, mf ); 156 if (!f.exists()) 157 { 158 log("File " + f.getAbsolutePath() + 159 " in input fileset does not exist."); 160 continue; 161 } 162 163 manifestFiles.add(f); 164 } 165 166 try 172 { 173 175 Map <String ,String > controlTypesToImpls = new HashMap <String ,String >(); 176 Map <String ,Set <String >> controlTypesToClients = 177 new HashMap <String , Set <String >>(); 178 179 for ( File mf : manifestFiles ) 180 { 181 ControlClientManifest ccmf = new ControlClientManifest( mf ); 182 String controlClient = ccmf.getControlClient(); 183 List <String > controlTypes = ccmf.getControlTypes(); 184 for ( String ct : controlTypes ) 185 { 186 controlTypesToImpls.put( ct, ccmf.getDefaultImpl( ct ) ); 187 Set <String > clients = controlTypesToClients.get( ct ); 188 if (clients == null) 189 { 190 clients = new TreeSet <String >(); 191 controlTypesToClients.put( ct, clients ); 192 } 193 clients.add( controlClient ); 194 } 195 } 196 197 202 String [] classpaths = _classPath == null ? new String [0] : _classPath.list(); 203 ClassLoader cl = buildClassLoader( classpaths, Assembler.class.getClassLoader() ); 204 205 Assembler.assemble( _moduleDir, _moduleName, _srcOutputDir, _contextFactoryClassName, 206 controlTypesToImpls, controlTypesToClients, cl ); 207 } 208 catch (Exception e) 209 { 210 e.printStackTrace(); 211 throw new BuildException("Assembly failed.", e); 212 } 213 } 214 215 private void validateAttributeSettings() throws BuildException 216 { 217 if (_contextFactoryClassName == null) 218 throw new BuildException("The contextFactoryClassName attribute must be set"); 219 220 if (_moduleDir == null) 221 throw new BuildException("The moduleDir attribute must be set"); 222 223 if (_srcOutputDir == null) 224 throw new BuildException("The srcOutputDir attribute must be set"); 225 } 226 227 private ClassLoader buildClassLoader( String [] paths, ClassLoader parentCL) 228 throws ControlAssemblyException 229 { 230 List list = new ArrayList (); 231 for (int i=0; i<paths.length; i++) 232 { 233 try 234 { 235 File file = new File (paths[i]); 236 String filePath = file.getCanonicalPath(); 237 if (!filePath.toLowerCase().endsWith(".jar") && 239 !filePath.endsWith("/") ) 240 { 241 filePath += "/"; 242 } 243 URL url = new URL ("file:" + filePath); 244 list.add(url); 245 } 246 catch (IOException e) 247 { 248 throw new ControlAssemblyException("Unable to include path " + 249 paths[i] + " in classpath. Caught " + 250 e.getClass().getName() + " trying to form this path as a URL.", e); 251 } 252 } 253 254 URL [] urlArray = new URL [list.size()]; 255 urlArray = (URL [])list.toArray(urlArray); 256 257 return new URLClassLoader (urlArray, parentCL); 258 } 259 260 protected String _contextFactoryClassName; 262 protected File _moduleDir; 263 protected String _moduleName; 264 protected File _srcOutputDir; 265 protected File _bindingFile; 266 protected Path _classPath; 267 protected FileSet _clientManifestFileSet; 268 } 269 | Popular Tags |