KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > KernelFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.kernel;
23
24 import org.jboss.kernel.spi.bootstrap.KernelInitializer;
25 import org.jboss.kernel.spi.config.KernelConfig;
26 import org.jboss.logging.Logger;
27
28 /**
29  * A <code>KernelFactory</code> is an implementation of the Factory
30  * design pattern that can construct new instances of {@link Kernel Kernel}
31  * objects.
32  *
33  * <p>If no <code>KernelConfig</code> is given to the <code>KernelFactory</code>,
34  * the <code>KernelFactory</code> will attempt to construct a
35  * <code>KernelConfig</code> object by using System properties
36  * (i.e. <code>System.getProperties()</code>).
37  *
38  * @author <a HREF="mailto:les.hazlewood@jboss.org">Les A. Hazlewood</a>
39  * @version $Revision: 37459 $
40  */

41 public class KernelFactory
42 {
43    /** The singleton factory */
44    private static final KernelFactory singleton = new KernelFactory();
45
46    /** The logger */
47    protected final Logger log = Logger.getLogger(getClass());
48
49    /**
50     * No public construction
51     */

52    private KernelFactory()
53    {
54    }
55
56    /**
57     * Returns a new instance of a Kernel based on the specified
58     * <code>KernelConfig</code> parameter.
59     *
60     * @param cfg the configuration used to construct a new
61     * <code>Kernel</code> object.
62     * @return a new <code>Kernel</code> instance constructed according to the
63     * specified configuration.
64     */

65    public static Kernel newInstance(KernelConfig cfg)
66    {
67       return singleton.assembleNewKernel(cfg);
68    }
69
70    /**
71     * Instantiates, configures, and initializes a new <code>Kernel</code>
72     * instance according to the specified <code>KernelConfig</code> parameter.
73     *
74     * @param cfg the KernelConfig that will be used in constructing a new
75     * Kernel.
76     * @return a newly constructed Kernel object configured according to the
77     * specified <code>KernelConfig</code> parameter.
78     * @throws RuntimeException
79     */

80    protected Kernel assembleNewKernel(KernelConfig cfg)
81       throws RuntimeException JavaDoc
82    {
83       long begin = 0;
84       long end = 0;
85
86       log.debug("Starting JBoss Kernel construction...");
87       begin = System.currentTimeMillis();
88
89       //local caching for easy reference:
90
boolean trace = log.isTraceEnabled();
91
92       if (trace)
93          log.trace("Using KernelConfig: " + cfg);
94
95       Kernel kernel = createKernel();
96       if (trace)
97          log.trace("Using Kernel: " + kernel);
98
99       KernelInitializer initializer = createKernelInitializer(cfg);
100       if (trace)
101          log.trace("Using KernelInitializer: " + initializer);
102
103       configureKernel(kernel, cfg);
104       if (trace)
105          log.trace("Configured kernel from KernelConfig");
106
107       initializeKernel(kernel, initializer);
108       if (trace)
109          log.trace("Kernel instance initialzed");
110
111       end = System.currentTimeMillis();
112       log.debug("Completed JBoss Kernel construction. Duration: " + (end - begin) + " milliseconds");
113
114       return kernel;
115    }
116
117    /**
118     * Constructs and returns a new, unconfigured, uninitialized
119     * <code>Kernel</code> instance. Configuration and initialization will be
120     * done explicitly in the construction process via the
121     * {@link #configureKernel(Kernel, KernelConfig) configureKernel} and
122     * {@link #initializeKernel(Kernel, KernelInitializer) initializeKernel}
123     * methods, respectively.
124     *
125     * @return An unconfigured, uninitialized <code>Kernel</code> instance.
126     */

127    protected Kernel createKernel()
128    {
129       return new Kernel();
130    }
131
132    /**
133     * Constructs a <code>KernelInitializer</code> based on the
134     * specified <code>KernelConfig</code> parameter. This initializer
135     * is used in the
136     * {@link #initializeKernel(Kernel, KernelInitializer) initializeKernel}
137     * method during Kernel construction.
138     *
139     * @param config the configuration with which to construct a new
140     * <code>KernelInitializer</code> instance.
141     * @return a new <code>KernelInitializer</code> instance based on the
142     * specified <code>KernelConfig</code>.
143     * @throws RuntimeException if the <code>config</code> object
144     * cannot properly construct a new <code>KernelInitializer</code>
145     */

146    protected KernelInitializer createKernelInitializer(KernelConfig config)
147    {
148       try
149       {
150          return config.createKernelInitializer();
151       }
152       catch (Throwable JavaDoc t)
153       {
154          String JavaDoc msg = "Unable to create a KernelInitializer based on the " +
155             "specified KernelConfig";
156          throw new RuntimeException JavaDoc(msg, t);
157       }
158    }
159
160    /**
161     * Configures the specified Kernel according to the specified
162     * <code>KernelConfig</code> parameter.
163     *
164     * @param kernel a new, unconfigured <code>Kernel</code> instance.
165     * @param cfg the <code>KernelConfig</code> to use to configure the
166     * specified <code>Kernel</code> parameter.
167     * @throws RuntimeException if the <code>KernelConfig</code> cannot be
168     * updated with the Kernel object.
169     */

170    protected void configureKernel(Kernel kernel, KernelConfig cfg)
171    {
172       kernel.setConfig(cfg);
173       try
174       {
175          cfg.setKernel(kernel);
176       }
177       catch (Throwable JavaDoc t)
178       {
179          String JavaDoc msg = "Unable to update the KernelConfig with the specified Kernel";
180          throw new RuntimeException JavaDoc(msg, t);
181       }
182    }
183
184    /**
185     * Initializes the specified Kernel according to the specified
186     * <code>KernelInitializer</code> parameter.
187     *
188     * @param kernel a new, uninitialized <code>Kernel</code> instance.
189     * @param initializer the <code>KernelInitializer</code> to use to
190     * initialize the specified <code>Kernel</code> parameter.
191     * @throws RuntimeException if the <code>KernelInitializer</code> cannot
192     * initialize the given <code>Kernel</code> parameter.
193     */

194    protected void initializeKernel(Kernel kernel, KernelInitializer initializer)
195    {
196       try
197       {
198          initializer.initKernel(kernel);
199       }
200       catch (Throwable JavaDoc t)
201       {
202          String JavaDoc msg = "Unable to properly initialize the Kernel";
203          throw new RuntimeException JavaDoc(msg, t);
204       }
205    }
206 }
207
Popular Tags