001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022:
023: import org.apache.geronimo.kernel.basic.BasicKernelFactory;
024:
025: /**
026: * @version $Rev: 549825 $ $Date: 2007-06-22 07:17:31 -0700 (Fri, 22 Jun 2007) $
027: */
028: public abstract class KernelFactory {
029: public static final String KERNEL_FACTORY_KEY = KernelFactory.class
030: .getName();
031:
032: public static KernelFactory newInstance() {
033: ClassLoader classLoader = Thread.currentThread()
034: .getContextClassLoader();
035: if (classLoader == null) {
036: classLoader = KernelFactory.class.getClassLoader();
037: }
038:
039: // System property
040: try {
041: String kernelFactoryName = System
042: .getProperty(KERNEL_FACTORY_KEY);
043: if (kernelFactoryName != null) {
044: return createKernelFactory(kernelFactoryName,
045: classLoader);
046: }
047: } catch (SecurityException se) {
048: }
049:
050: // Jar Service Specification - http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
051: String serviceId = "META-INF/services/" + KERNEL_FACTORY_KEY;
052: InputStream inputStream = null;
053: try {
054: inputStream = classLoader.getResourceAsStream(serviceId);
055: if (inputStream != null) {
056: BufferedReader reader = new BufferedReader(
057: new InputStreamReader(inputStream, "UTF-8"));
058: String kernelFactoryName = reader.readLine();
059: reader.close();
060:
061: if (kernelFactoryName != null
062: && kernelFactoryName.length() > 0) {
063: return createKernelFactory(kernelFactoryName,
064: classLoader);
065: }
066: }
067: } catch (Exception ignored) {
068: } finally {
069: if (inputStream != null) {
070: try {
071: inputStream.close();
072: } catch (IOException ignored) {
073: }
074: inputStream = null;
075: }
076: }
077:
078: // Default is the basic kernel
079: return new BasicKernelFactory();
080: }
081:
082: private static KernelFactory createKernelFactory(String className,
083: ClassLoader classLoader) {
084: try {
085: return (KernelFactory) classLoader.loadClass(className)
086: .newInstance();
087: } catch (ClassCastException e) {
088: throw new KernelFactoryError(
089: "Kernel factory class does not implement KernelFactory: "
090: + className, e);
091: } catch (ClassNotFoundException e) {
092: throw new KernelFactoryError(
093: "Kernel factory class not found: " + className, e);
094: } catch (Exception e) {
095: throw new KernelFactoryError(
096: "Unable to instantiate kernel factory class: "
097: + className, e);
098: }
099: }
100:
101: public abstract Kernel createKernel(String kernelName);
102: }
|