1 6 package org.jfox.ioc.deploy; 7 8 import org.jfox.ioc.Constants; 9 import org.jfox.ioc.classloader.ClassLoaderRepository; 10 import org.jfox.ioc.classloader.LocalClassLoader; 11 import org.jfox.ioc.deployment.Deployable; 12 import org.jfox.ioc.deployment.Deployer; 13 import org.jfox.ioc.logger.Logger; 14 import org.jfox.ioc.util.FileFilters; 15 import org.jfox.ioc.util.Files; 16 import org.jfox.ioc.util.XMLUtils; 17 18 import java.io.File ; 19 import java.net.URL ; 20 import java.net.URLClassLoader ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 29 30 public class ComponentModule implements Deployable, Comparable <ComponentModule> { 31 private File moduleDir; 32 33 private LocalClassLoader moduleClassLoader; 34 35 private ClassLoaderRepository loaderRepo; 36 37 private ComponentModuleDescriptor componentModuleDescriptor; 38 39 private Logger logger; 40 41 private static final String singleComponentDescriptorSuffix = ".component.xml"; 42 43 public ComponentModule(File moduleDir, ClassLoaderRepository loaderRespo) 44 throws ComponentDescriptionException { 45 this.moduleDir = moduleDir; 46 this.loaderRepo = loaderRespo; 47 logger = Logger.getLogger(this.getClass().getName() + "[" 48 + moduleDir.getName() + "]"); 49 init(); 50 } 51 52 public File getPackage() { 53 return moduleDir; 54 } 55 56 public void deploy(Deployer deployer) throws DeployException { 57 deployer.deploy(this); 58 } 59 60 public File getTempDeployDirectory() { 61 return moduleDir; 62 } 63 64 public void init() throws ComponentDescriptionException { 65 try { 66 componentModuleDescriptor = new ComponentModuleDescriptor(XMLUtils 67 .getDocument(getDescriptorURL())); 68 } 69 catch(Exception e) { 70 throw new ComponentDescriptionException("build module descriptor failed.", e); 71 } 72 73 try { 75 List components = Files.listFiles(new File (moduleDir, "META-INF"), FileFilters.suffixFileFilter(singleComponentDescriptorSuffix)); 76 for(int i = 0; i < components.size(); i++) { 77 File componentXml = (File ) components.get(i); 78 ComponentDescriptor cd = new ComponentDescriptor(componentModuleDescriptor); 79 cd.processXML(XMLUtils.getDocument(componentXml.toURL()).getDocumentElement()); 80 componentModuleDescriptor.addComponent(cd); 81 } 82 83 } 84 catch(Exception e) { 85 throw new ComponentDescriptionException("build module descriptor failed.", e); 86 } 87 88 File [] componentJars = getComponentJars(); 90 List urls = new ArrayList (componentJars.length + 1); 91 try { 92 File classesDir = new File (moduleDir, Constants.JFOXCO_MODULE_ClASSPATH); 94 if(classesDir.exists() && classesDir.isDirectory()) { 95 urls.add(classesDir.toURL()); 96 logger 97 .debug("add component classes directory to module's public ClassLoader " 98 + classesDir.toString()); 99 } 100 101 for(int i = 0; i < componentJars.length; i++) { 102 urls.add(componentJars[i].toURL()); 103 logger 104 .debug("add component jar to module's public ClassLoader " 105 + componentJars[i].toString()); 106 } 107 108 File resourceDir = new File (moduleDir, Constants.JFOXCO_MODULE_CONFIG_PATH); 110 if(resourceDir.exists() && resourceDir.isDirectory()) { 111 urls.add(resourceDir.toURL()); 112 logger 113 .debug("add module resource directory to module's public ClassLoader " 114 + resourceDir.toString()); 115 } 116 117 } 118 catch(Exception e) { 119 e.printStackTrace(); 120 } 121 moduleClassLoader = new LocalClassLoader((URL []) urls 123 .toArray(new URL [urls.size()]), loaderRepo); 124 125 File [] libJars = getComponentLibJars(); 127 try { 128 for(int i = 0; i < libJars.length; i++) { 129 moduleClassLoader.addURL(libJars[i].toURL()); 130 logger 131 .debug("add component lib to module's private ClassLoader " 132 + libJars[i].toString()); 133 } 134 } 135 catch(Exception e) { 136 e.printStackTrace(); 137 } 138 } 139 140 public URL getDescriptorURL() { 141 try { 142 return new File (moduleDir, "META-INF/" + Constants.JFOXCO_MODULE_DEPLOY_FILE).toURL(); 143 } 144 catch(Exception e) { 145 e.printStackTrace(); 146 return null; 147 } 148 } 149 150 public URLClassLoader getClassLoader() { 151 return moduleClassLoader; 152 } 153 154 public ComponentModuleDescriptor getModuleDescriptor() { 155 return componentModuleDescriptor; 156 } 157 158 public File [] getComponentJars() { 159 return moduleDir.listFiles(FileFilters.suffixFileFilter(new String []{ 160 "jar", "zip"})); 161 } 162 163 private File [] getComponentLibJars() { 164 File lib = new File (moduleDir, Constants.JFOXCO_MODULE_PRIVATE_LIBS); 165 if(!lib.exists()) 166 return new File [0]; 167 List files = Files.listFiles(lib, FileFilters 168 .suffixFileFilter(new String []{"jar", "zip"})); 169 return (File []) files.toArray(new File [files.size()]); 170 } 171 172 public int compareTo(ComponentModule o) { 173 ComponentModule _module = (ComponentModule) o; 174 return new Integer (componentModuleDescriptor.getPriority()) 175 .compareTo(new Integer (_module.getModuleDescriptor() 176 .getPriority())); 177 } 178 179 public boolean equals(Object obj) { 180 return moduleDir.getName().equals(((ComponentModule) obj).moduleDir.getName()); 181 } 182 183 public int hashCode() { 184 return moduleDir.getName().toString().hashCode(); 185 } 186 187 public String toString() { 188 return "Module{" + "moduleDir=" + moduleDir + ", description=" 189 + componentModuleDescriptor.getDescription() + "}"; 190 } 191 192 public static void main(String [] args) { 193 194 } 195 } 196 197 | Popular Tags |