1 /**
2 * $Id: KernelFactory.java 74 2006-12-15 21:06:36Z maldito_orco $
3 *
4 * =========================================================================
5 *
6 * Copyright 2005 Tubo
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package org.tubo.kernel;
21
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.tubo.exception.TuboConfigurationException;
27 import org.tubo.exception.TuboKernelException;
28 import org.tubo.exception.TuboException;
29 import org.tubo.configuration.ConfigurationManager;
30
31 import java.util.ArrayList;
32 import java.util.Iterator;
33
34 /**
35 * <p>
36 * This class create the base kernel creational steps.<br>
37 * The role of this class is create and load ConfigManagerset, and the
38 * set environment for KernelBuilder
39 * </p>
40 * <p>
41 * Created: Aug 30, 2006, 4:54:32 PM <br>
42 * Last Modification Date: $Date: 2006-12-15 18:06:36 -0300 (Fri, 15 Dec 2006) $
43 * </p>
44 *
45 * @author maldito_orco (maldito_orco@users.sourceforge.net)
46 * @version $Revision: 74 $
47 */
48 public class KernelFactory {
49 public static final String RCS_ID = "$Id: KernelFactory.java 74 2006-12-15 21:06:36Z maldito_orco $";
50 private static Log log = LogFactory.getLog(KernelFactory.class);
51
52 /** Constant with base configuration location */
53 public static final String BASE_CONFIG_LOCATION = "META-INF/config/base-tubo-config.xml";
54
55 /** Base configuration */
56 private String baseConfigLocation = BASE_CONFIG_LOCATION;
57
58 /** Config locations Colection's */
59 private ArrayList configLocations = new ArrayList();
60
61 /**
62 * Class method to create a new default KernelFactory implementation
63 * @return a KernelFactory
64 */
65 public static KernelFactory getInstance() {
66 return new KernelFactory();
67 }
68
69 /**
70 * Get base configuration location
71 * @return the base tubo configuration
72 */
73 public String getBaseConfigLocation() {
74 return baseConfigLocation;
75 }
76
77 /**
78 * Change default base configuration
79 * @param baseConfigLocation A location
80 */
81 public void setBaseConfigLocation(String baseConfigLocation) {
82 this.baseConfigLocation = baseConfigLocation;
83 }
84
85 /**
86 * Add configurations to kernel
87 * @param configLocation Configuration URL
88 */
89 public void appendConfigLocation(String configLocation) {
90 configLocations.add(configLocation);
91 }
92
93 /**
94 * This class create the base kernel creational steps.
95 * The role of this inheritance is set the environment for Builder
96 * @return New Kernel instance
97 * @throws TuboKernelException If somthing fail
98 */
99 public Kernel createKernel() throws TuboKernelException {
100
101
102 ConfigurationManager configurationManager;
103 try {
104 configurationManager = new ConfigurationManager();
105 } catch (TuboConfigurationException e) {
106 throw new TuboKernelException("ConfigurationManager can't be created",e);
107 }
108
109
110 try {
111 String baseConfig = getBaseConfigLocation();
112 log.info(".createKernel() - Loading base config url="+baseConfig);
113 configurationManager.appendResource(baseConfig);
114 } catch (TuboConfigurationException e) {
115 throw new TuboKernelException("Base Config Location can't be configured",e);
116 }
117
118
119 for (Iterator it=configLocations.iterator(); it.hasNext();) {
120 String resourceLocation = (String)it.next();
121 log.info(".createKernel() - Appendig config url="+configLocations);
122 try {
123 configurationManager.appendResource(resourceLocation);
124 } catch (TuboConfigurationException e) {
125 throw new TuboKernelException("Config "+resourceLocation+" has problems to be appened",e);
126 }
127 }
128
129
130 String kernelBuilderClassName = configurationManager.getConfiguration().getKernelBuilderClassName();
131 KernelBuilder kernelBuilder;
132 try {
133 kernelBuilder = (KernelBuilder)Class.forName(kernelBuilderClassName).newInstance();
134 } catch (InstantiationException e) {
135 throw new TuboKernelException("Error instanciating KernelBuilder ("+kernelBuilderClassName+")",e);
136 } catch (IllegalAccessException e) {
137 throw new TuboKernelException("Illegal Access error when try to instanciate KernelBuilder ("+kernelBuilderClassName+")",e);
138 } catch (ClassNotFoundException e) {
139 throw new TuboKernelException("Class not found error when try to instanciate KernelBuilder ("+kernelBuilderClassName+")",e);
140 }
141
142
143 Kernel kernel;
144 try {
145 log.info(".createKernel() - Creating Kernel");
146 kernel = kernelBuilder.create(configurationManager);
147 } catch (TuboException e) {
148 throw new TuboKernelException("Error while creating Kernel",e);
149 }
150
151
152 log.info(".createKernel() - Kernel created");
153 return kernel;
154 }
155
156 }