KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > KernelFactory


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23
24 import org.apache.geronimo.kernel.basic.BasicKernelFactory;
25
26 /**
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public abstract class KernelFactory {
30     public static final String JavaDoc KERNEL_FACTORY_KEY = KernelFactory.class.getName();
31
32     public static KernelFactory newInstance() {
33         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
34         if (classLoader == null) {
35             classLoader = KernelFactory.class.getClassLoader();
36         }
37
38         // System property
39
try {
40             String JavaDoc kernelFactoryName = System.getProperty(KERNEL_FACTORY_KEY);
41             if (kernelFactoryName != null) {
42                 return createKernelFactory(kernelFactoryName, classLoader);
43             }
44         } catch (SecurityException JavaDoc se) {
45         }
46
47         // Jar Service Specification - http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
48
String JavaDoc serviceId = "META-INF/services/" + KERNEL_FACTORY_KEY;
49         InputStream JavaDoc inputStream = null;
50         try {
51             inputStream = classLoader.getResourceAsStream(serviceId);
52             if (inputStream != null) {
53                 BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream, "UTF-8"));
54                 String JavaDoc kernelFactoryName = reader.readLine();
55                 reader.close();
56
57                 if (kernelFactoryName != null && kernelFactoryName.length() > 0) {
58                     return createKernelFactory(kernelFactoryName, classLoader);
59                 }
60             }
61         } catch (Exception JavaDoc ignored) {
62         } finally {
63             if (inputStream != null) {
64                 try {
65                     inputStream.close();
66                 } catch (IOException JavaDoc ignored) {
67                 }
68                 inputStream = null;
69             }
70         }
71
72         // Default is the basic kernel
73
return new BasicKernelFactory();
74     }
75
76     private static KernelFactory createKernelFactory(String JavaDoc className, ClassLoader JavaDoc classLoader) {
77         try {
78             return (KernelFactory) classLoader.loadClass(className).newInstance();
79         } catch (ClassCastException JavaDoc e) {
80             throw new KernelFactoryError("Kernel factory class does not implement KernelFactory: " + className);
81         } catch (ClassNotFoundException JavaDoc e) {
82             throw new KernelFactoryError("Kernel factory class not found: " + className);
83         } catch (Exception JavaDoc e) {
84             throw new KernelFactoryError("Unable to instantiate kernel factory class: " + className, e);
85         }
86     }
87
88     public abstract Kernel createKernel(String JavaDoc kernelName);
89 }
90
Popular Tags