001: /*
002: * $Id: ContainerLoader.java,v 1.7 2003/12/01 20:43:39 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.base.container;
026:
027: import java.util.Collection;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031:
032: import org.ofbiz.base.start.StartupException;
033: import org.ofbiz.base.start.StartupLoader;
034: import org.ofbiz.base.start.Start;
035: import org.ofbiz.base.util.Debug;
036:
037: /**
038: * ContainerLoader - StartupLoader for the container
039: *
040: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041: *@version $Revision: 1.7 $
042: * @since 3.0
043: */
044: public class ContainerLoader implements StartupLoader {
045:
046: public static final String module = ContainerLoader.class.getName();
047: public static final String CONTAINER_CONFIG = "ofbiz-containers.xml";
048:
049: protected List loadedContainers = new LinkedList();
050:
051: /**
052: * @see org.ofbiz.base.start.StartupLoader#load(Start.Config)
053: */
054: public void load(Start.Config config) throws StartupException {
055: Debug.logInfo("[Startup] Loading ContainerLoader...", module);
056:
057: // get the master container configuration file
058: String configFileLocation = config.containerConfig;
059:
060: Collection containers = null;
061: try {
062: containers = ContainerConfig
063: .getContainers(configFileLocation);
064: } catch (ContainerException e) {
065: throw new StartupException(e);
066: }
067:
068: if (containers != null) {
069: Iterator i = containers.iterator();
070: while (i.hasNext()) {
071: ContainerConfig.Container containerCfg = (ContainerConfig.Container) i
072: .next();
073: loadedContainers.add(loadContainer(
074: containerCfg.className, configFileLocation));
075: }
076: }
077: }
078:
079: /**
080: * @see org.ofbiz.base.start.StartupLoader#unload()
081: */
082: public void unload() throws StartupException {
083: Debug.logInfo("Shutting down containers", module);
084: // shutting down in reverse order
085: for (int i = loadedContainers.size(); i > 0; i--) {
086: Container container = (Container) loadedContainers
087: .get(i - 1);
088: try {
089: container.stop();
090: } catch (ContainerException e) {
091: Debug.logError(e, module);
092: }
093: }
094: }
095:
096: private Container loadContainer(String classname,
097: String configFileLocation) throws StartupException {
098: // load the component container class
099: ClassLoader loader = Thread.currentThread()
100: .getContextClassLoader();
101: if (loader == null) {
102: Debug.logWarning(
103: "Unable to get context classloader; using system",
104: module);
105: loader = ClassLoader.getSystemClassLoader();
106: }
107: Class componentClass = null;
108: try {
109: componentClass = loader.loadClass(classname);
110: } catch (ClassNotFoundException e) {
111: throw new StartupException("Cannot locate container class",
112: e);
113: }
114: if (componentClass == null) {
115: throw new StartupException(
116: "Component container class not loaded");
117: }
118:
119: Container componentObj = null;
120: try {
121: componentObj = (Container) componentClass.newInstance();
122: } catch (InstantiationException e) {
123: throw new StartupException(e);
124: } catch (IllegalAccessException e) {
125: throw new StartupException(e);
126: } catch (ClassCastException e) {
127: throw new StartupException(e);
128: }
129:
130: if (componentObj == null) {
131: throw new StartupException(
132: "Unable to create instance of component container");
133: }
134:
135: try {
136: componentObj.start(configFileLocation);
137: } catch (ContainerException e) {
138: throw new StartupException(e);
139: }
140:
141: return componentObj;
142: }
143: }
|