1 25 package org.ofbiz.base.container; 26 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 32 import org.ofbiz.base.start.StartupException; 33 import org.ofbiz.base.start.StartupLoader; 34 import org.ofbiz.base.start.Start; 35 import org.ofbiz.base.util.Debug; 36 37 44 public class ContainerLoader implements StartupLoader { 45 46 public static final String module = ContainerLoader.class.getName(); 47 public static final String CONTAINER_CONFIG = "ofbiz-containers.xml"; 48 private static boolean loaded = false; 49 50 protected List loadedContainers = new LinkedList (); 51 protected String configFile = null; 52 53 56 public void load(Start.Config config, String args[]) throws StartupException { 57 Debug.logInfo("[Startup] Loading containers...", module); 58 loaded = true; 59 60 this.configFile = config.containerConfig; 62 63 Collection containers = null; 64 try { 65 containers = ContainerConfig.getContainers(configFile); 66 } catch (ContainerException e) { 67 throw new StartupException(e); 68 } 69 70 if (containers != null) { 71 Iterator i = containers.iterator(); 72 while (i.hasNext()) { 73 ContainerConfig.Container containerCfg = (ContainerConfig.Container) i.next(); 74 loadedContainers.add(loadContainer(containerCfg, args)); 75 } 76 } 77 } 78 79 82 public void start() throws StartupException { 83 Debug.logInfo("[Startup] Starting containers...", module); 84 85 for (int i = 0; i < loadedContainers.size(); i++) { 87 Container container = (Container) loadedContainers.get(i); 88 try { 89 container.start(); 90 } catch (ContainerException e) { 91 throw new StartupException("Cannot start() " + container.getClass().getName(), e); 92 } catch (java.lang.AbstractMethodError e) { 93 throw new StartupException("Cannot start() " + container.getClass().getName(), e); 94 } 95 } 96 } 97 98 101 public void unload() throws StartupException { 102 Debug.logInfo("Shutting down containers", module); 103 104 for (int i = loadedContainers.size(); i > 0; i--) { 106 Container container = (Container) loadedContainers.get(i-1); 107 try { 108 container.stop(); 109 } catch (ContainerException e) { 110 Debug.logError(e, module); 111 } 112 } 113 } 114 115 private Container loadContainer(ContainerConfig.Container containerCfg, String [] args) throws StartupException { 116 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 118 if (loader == null) { 119 Debug.logWarning("Unable to get context classloader; using system", module); 120 loader = ClassLoader.getSystemClassLoader(); 121 } 122 Class containerClass = null; 123 try { 124 containerClass = loader.loadClass(containerCfg.className); 125 } catch (ClassNotFoundException e) { 126 throw new StartupException("Cannot locate container class", e); 127 } 128 if (containerClass == null) { 129 throw new StartupException("Component container class not loaded"); 130 } 131 132 Container containerObj = null; 134 try { 135 containerObj = (Container) containerClass.newInstance(); 136 } catch (InstantiationException e) { 137 throw new StartupException("Cannot create " + containerCfg.name, e); 138 } catch (IllegalAccessException e) { 139 throw new StartupException("Cannot create " + containerCfg.name, e); 140 } catch (ClassCastException e) { 141 throw new StartupException("Cannot create " + containerCfg.name, e); 142 } 143 144 if (containerObj == null) { 145 throw new StartupException("Unable to create instance of component container"); 146 } 147 148 try { 150 containerObj.init(args, configFile); 151 } catch (ContainerException e) { 152 throw new StartupException("Cannot init() " + containerCfg.name, e); 153 } catch (java.lang.AbstractMethodError e) { 154 throw new StartupException("Cannot init() " + containerCfg.name, e); 155 } 156 157 return containerObj; 158 } 159 160 public static synchronized boolean loadContainers(String config, String [] args) throws StartupException { 161 if (!loaded) { 162 ContainerLoader loader = new ContainerLoader(); 163 Start.Config cfg = new Start.Config(); 164 cfg.containerConfig = config == null ? "limited-containers.xml" : config; 165 loader.load(cfg, args); 166 return true; 167 } 168 return false; 169 } 170 } 171 | Popular Tags |