KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > container > ContainerLoader


1 /*
2  * $Id: ContainerLoader.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.container;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
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 /**
38  * ContainerLoader - StartupLoader for the container
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41   *@version $Rev: 5462 $
42  * @since 3.0
43  */

44 public class ContainerLoader implements StartupLoader {
45     
46     public static final String JavaDoc module = ContainerLoader.class.getName();
47     public static final String JavaDoc CONTAINER_CONFIG = "ofbiz-containers.xml";
48     private static boolean loaded = false;
49
50     protected List JavaDoc loadedContainers = new LinkedList JavaDoc();
51     protected String JavaDoc configFile = null;
52
53     /**
54      * @see org.ofbiz.base.start.StartupLoader#load(Start.Config, String[])
55      */

56     public void load(Start.Config config, String JavaDoc args[]) throws StartupException {
57         Debug.logInfo("[Startup] Loading containers...", module);
58         loaded = true;
59         
60         // get the master container configuration file
61
this.configFile = config.containerConfig;
62         
63         Collection JavaDoc 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 JavaDoc 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     /**
80      * @see org.ofbiz.base.start.StartupLoader#start()
81      */

82     public void start() throws StartupException {
83         Debug.logInfo("[Startup] Starting containers...", module);
84
85         // start each container object
86
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 JavaDoc e) {
93                 throw new StartupException("Cannot start() " + container.getClass().getName(), e);
94             }
95         }
96     }
97
98     /**
99      * @see org.ofbiz.base.start.StartupLoader#unload()
100      */

101     public void unload() throws StartupException {
102         Debug.logInfo("Shutting down containers", module);
103
104         // shutting down in reverse order
105
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 JavaDoc[] args) throws StartupException {
116         // load the container class
117
ClassLoader JavaDoc 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 JavaDoc containerClass = null;
123         try {
124             containerClass = loader.loadClass(containerCfg.className);
125         } catch (ClassNotFoundException JavaDoc 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         // create a new instance of the container object
133
Container containerObj = null;
134         try {
135             containerObj = (Container) containerClass.newInstance();
136         } catch (InstantiationException JavaDoc e) {
137             throw new StartupException("Cannot create " + containerCfg.name, e);
138         } catch (IllegalAccessException JavaDoc e) {
139             throw new StartupException("Cannot create " + containerCfg.name, e);
140         } catch (ClassCastException JavaDoc 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         // initialize the container object
149
try {
150             containerObj.init(args, configFile);
151         } catch (ContainerException e) {
152             throw new StartupException("Cannot init() " + containerCfg.name, e);
153         } catch (java.lang.AbstractMethodError JavaDoc e) {
154             throw new StartupException("Cannot init() " + containerCfg.name, e);
155         }
156
157         return containerObj;
158     }
159
160     public static synchronized boolean loadContainers(String JavaDoc config, String JavaDoc[] 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