View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  
24  import org.apache.geronimo.kernel.basic.BasicKernelFactory;
25  
26  /**
27   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
28   */
29  public abstract class KernelFactory {
30      public static final String KERNEL_FACTORY_KEY = KernelFactory.class.getName();
31  
32      public static KernelFactory newInstance() {
33          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
34          if (classLoader == null) {
35              classLoader = KernelFactory.class.getClassLoader();
36          }
37  
38          // System property
39          try {
40              String kernelFactoryName = System.getProperty(KERNEL_FACTORY_KEY);
41              if (kernelFactoryName != null) {
42                  return createKernelFactory(kernelFactoryName, classLoader);
43              }
44          } catch (SecurityException se) {
45          }
46  
47          // Jar Service Specification - http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
48          String serviceId = "META-INF/services/" + KERNEL_FACTORY_KEY;
49          InputStream inputStream = null;
50          try {
51              inputStream = classLoader.getResourceAsStream(serviceId);
52              if (inputStream != null) {
53                  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
54                  String kernelFactoryName = reader.readLine();
55                  reader.close();
56  
57                  if (kernelFactoryName != null && kernelFactoryName.length() > 0) {
58                      return createKernelFactory(kernelFactoryName, classLoader);
59                  }
60              }
61          } catch (Exception ignored) {
62          } finally {
63              if (inputStream != null) {
64                  try {
65                      inputStream.close();
66                  } catch (IOException 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 className, ClassLoader classLoader) {
77          try {
78              return (KernelFactory) classLoader.loadClass(className).newInstance();
79          } catch (ClassCastException e) {
80              throw new KernelFactoryError("Kernel factory class does not implement KernelFactory: " + className);
81          } catch (ClassNotFoundException e) {
82              throw new KernelFactoryError("Kernel factory class not found: " + className);
83          } catch (Exception e) {
84              throw new KernelFactoryError("Unable to instantiate kernel factory class: " + className, e);
85          }
86      }
87  
88      public abstract Kernel createKernel(String kernelName);
89  }