1 25 package org.ofbiz.base.container; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.ofbiz.base.component.AlreadyLoadedException; 35 import org.ofbiz.base.component.ComponentConfig; 36 import org.ofbiz.base.component.ComponentException; 37 import org.ofbiz.base.component.ComponentLoaderConfig; 38 import org.ofbiz.base.start.Classpath; 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.UtilValidate; 41 42 54 public class ComponentContainer implements Container { 55 56 public static final String module = ComponentContainer.class.getName(); 57 58 protected Classpath classPath = new Classpath(System.getProperty("java.class.path")); 60 protected String configFileLocation = null; 61 private boolean loaded = false; 62 63 66 public void init(String [] args, String configFile) throws ContainerException { 67 this.configFileLocation = configFile; 68 69 ContainerConfig.Container cc = ContainerConfig.getContainer("component-container", configFileLocation); 71 72 String loaderConfig = null; 74 if (cc.getProperty("loader-config") != null) { 75 loaderConfig = cc.getProperty("loader-config").value; 76 } 77 78 boolean updateClassPath = true; 80 if (cc.getProperty("update-classpath") != null) { 81 updateClassPath = "true".equalsIgnoreCase(cc.getProperty("update-classpath").value); 82 } 83 84 try { 86 loadComponents(loaderConfig, updateClassPath); 87 } catch (AlreadyLoadedException e) { 88 throw new ContainerException(e); 89 } catch (ComponentException e) { 90 throw new ContainerException(e); 91 } 92 } 93 94 97 public boolean start() throws ContainerException { 98 return true; 99 } 100 101 public synchronized void loadComponents(String loaderConfig, boolean updateClasspath) throws AlreadyLoadedException, ComponentException { 102 if (!loaded) { 106 loaded = true; 107 } else { 108 throw new AlreadyLoadedException("Components already loaded, cannot start"); 109 } 110 111 List components = ComponentLoaderConfig.getRootComponents(loaderConfig); 113 114 if (components != null) { 116 Iterator ci = components.iterator(); 117 while (ci.hasNext()) { 118 ComponentLoaderConfig.ComponentDef def = (ComponentLoaderConfig.ComponentDef) ci.next(); 119 this.loadComponentFromConfig(def); 120 } 121 } 122 123 if (updateClasspath) { 125 System.setProperty("java.class.path", classPath.toString()); 126 ClassLoader cl = classPath.getClassLoader(); 127 Thread.currentThread().setContextClassLoader(cl); 128 } 129 130 Debug.logInfo("All components loaded", module); 131 } 132 133 private void loadComponentFromConfig(ComponentLoaderConfig.ComponentDef def) { 134 if (def.type == ComponentLoaderConfig.SINGLE_COMPONENT) { 135 ComponentConfig config = null; 136 try { 137 config = ComponentConfig.getComponentConfig(def.name, def.location); 138 if (UtilValidate.isEmpty(def.name)) { 139 def.name = config.getGlobalName(); 140 } 141 } catch (ComponentException e) { 142 Debug.logError("Cannot load component : " + def.name + " @ " + def.location + " : " + e.getMessage(), module); 143 } 144 if (config == null) { 145 Debug.logError("Cannot load component : " + def.name + " @ " + def.location, module); 146 } else { 147 this.loadComponent(config); 148 } 149 } else if (def.type == ComponentLoaderConfig.COMPONENT_DIRECTORY) { 150 this.loadComponentDirectory(def.location); 151 } 152 } 153 154 private void loadComponentDirectory(String directoryName) { 155 Debug.logInfo("Auto-Loading component directory : [" + directoryName + "]", module); 156 File parentPath = new File (directoryName); 157 if (!parentPath.exists() || !parentPath.isDirectory()) { 158 Debug.logError("Auto-Load Component directory not found : " + directoryName, module); 159 } else { 160 File componentLoadConfig = new File (parentPath, "component-load.xml"); 161 if (componentLoadConfig != null && componentLoadConfig.exists()) { 162 URL configUrl = null; 163 try { 164 configUrl = componentLoadConfig.toURL(); 165 List componentsToLoad = ComponentLoaderConfig.getComponentsFromConfig(configUrl); 166 if (componentsToLoad != null) { 167 Iterator i = componentsToLoad.iterator(); 168 while (i.hasNext()) { 169 ComponentLoaderConfig.ComponentDef def = (ComponentLoaderConfig.ComponentDef) i.next(); 170 this.loadComponentFromConfig(def); 171 } 172 } 173 } catch (MalformedURLException e) { 174 Debug.logError(e, "Unable to locate URL for component loading file: " + componentLoadConfig.getAbsolutePath(), module); 175 } catch (ComponentException e) { 176 Debug.logError(e, "Unable to load components from URL: " + configUrl.toExternalForm(), module); 177 } 178 } else { 179 String subs[] = parentPath.list(); 180 for (int i = 0; i < subs.length; i++) { 181 try { 182 File componentPath = new File (parentPath.getCanonicalPath() + "/" + subs[i]); 183 if (componentPath.isDirectory() && !subs[i].equals("CVS")) { 184 String componentLocation = componentPath.getCanonicalPath(); 186 File configFile = new File (componentLocation + "/ofbiz-component.xml"); 187 if (configFile.exists()) { 188 ComponentConfig config = null; 189 try { 190 config = ComponentConfig.getComponentConfig(null, componentLocation); 192 } catch (ComponentException e) { 193 Debug.logError(e, "Cannot load component : " + componentPath.getName() + " @ " + componentLocation + " : " + e.getMessage(), module); 194 } 195 if (config == null) { 196 Debug.logError("Cannot load component : " + componentPath.getName() + " @ " + componentLocation, module); 197 } else { 198 loadComponent(config); 199 } 200 } 201 } 202 } catch (IOException ioe) { 203 Debug.logError(ioe, module); 204 } 205 } 206 } 207 } 208 } 209 210 private void loadComponent(ComponentConfig config) { 211 if (!config.enabled()) { 213 Debug.logInfo("Not Loading component : [" + config.getComponentName() + "] (disabled)", module); 214 return; 215 } 216 217 Debug.logInfo("Loading component : [" + config.getComponentName() + "]", module); 218 List classpathInfos = config.getClasspathInfos(); 219 String configRoot = config.getRootLocation(); 220 configRoot = configRoot.replace('\\', '/'); 221 if (!configRoot.endsWith("/")) { 223 configRoot = configRoot + "/"; 224 } 225 if (classpathInfos != null) { 226 Iterator cpi = classpathInfos.iterator(); 227 while (cpi.hasNext()) { 228 ComponentConfig.ClasspathInfo cp = (ComponentConfig.ClasspathInfo) cpi.next(); 229 String location = cp.location.replace('\\', '/'); 230 if (location.startsWith("/")) { 232 location = location.substring(1); 233 } 234 if ("dir".equals(cp.type)) { 235 classPath.addComponent(configRoot + location); 236 } else if ("jar".equals(cp.type)) { 237 String dirLoc = location; 238 if (dirLoc.endsWith("/*")) { 239 dirLoc = location.substring(0, location.length() - 2); 241 } 242 File path = new File (configRoot + dirLoc); 243 if (path.exists()) { 244 if (path.isDirectory()) { 245 File files[] = path.listFiles(); 247 for (int i = 0; i < files.length; i++) { 248 String file = files[i].getName(); 249 if (file.endsWith(".jar") || file.endsWith(".zip")) { 250 classPath.addComponent(files[i]); 251 } 252 } 253 } else { 254 classPath.addComponent(configRoot + location); 256 } 257 } else { 258 Debug.logWarning("Location '" + configRoot + dirLoc + "' does not exist", module); 259 } 260 } else { 261 Debug.logError("Classpath type '" + cp.type + "' is not supported; '" + location + "' not loaded", module); 262 } 263 } 264 } 265 } 266 267 270 public void stop() throws ContainerException { 271 } 272 273 280 public static synchronized void loadComponents(boolean updateClasspath) throws AlreadyLoadedException, ComponentException { 281 ComponentContainer cc = new ComponentContainer(); 282 cc.loadComponents(null, updateClasspath); 283 cc = null; 284 } 285 } | Popular Tags |