001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf;
019:
020: import java.io.BufferedReader;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.apache.cxf.buslifecycle.BusLifeCycleManager;
027: import org.apache.cxf.common.logging.LogUtils;
028:
029: public abstract class BusFactory {
030:
031: public static final String BUS_FACTORY_PROPERTY_NAME = "org.apache.cxf.bus.factory";
032: public static final String DEFAULT_BUS_FACTORY = "org.apache.cxf.bus.CXFBusFactory";
033:
034: protected static Bus defaultBus;
035: protected static ThreadLocal<Bus> localBus = new ThreadLocal<Bus>();
036:
037: private static final Logger LOG = LogUtils.getL7dLogger(
038: BusFactory.class, "APIMessages");
039:
040: /**
041: * Creates a new bus.
042: * While concrete <code>BusFactory</code> may offer differently
043: * parametrized methods for creating a bus, all factories support
044: * this no-arg factory method.
045: *
046: * @return the newly created bus.
047: */
048: public abstract Bus createBus();
049:
050: /**
051: * Returns the default bus, creating it if necessary.
052: *
053: * @return the default bus.
054: */
055: public static synchronized Bus getDefaultBus() {
056: return getDefaultBus(true);
057: }
058:
059: /**
060: * Returns the default bus
061: * @param createIfNeeded Set to true to create a default bus if one doesn't exist
062: * @return the default bus.
063: */
064: public static synchronized Bus getDefaultBus(boolean createIfNeeded) {
065: if (defaultBus == null && createIfNeeded) {
066: defaultBus = newInstance().createBus();
067: }
068: return defaultBus;
069: }
070:
071: /**
072: * Sets the default bus.
073: * @param bus the default bus.
074: */
075: public static synchronized void setDefaultBus(Bus bus) {
076: defaultBus = bus;
077: setThreadDefaultBus(bus);
078: }
079:
080: /**
081: * Sets the default bus for the thread.
082: * @param bus the default bus.
083: */
084: public static synchronized void setThreadDefaultBus(Bus bus) {
085: localBus.set(bus);
086: }
087:
088: /**
089: * Gets the default bus for the thread.
090: * @return the default bus.
091: */
092: public static synchronized Bus getThreadDefaultBus() {
093: if (localBus.get() == null) {
094: Bus b = getDefaultBus();
095: localBus.set(b);
096: }
097: return localBus.get();
098: }
099:
100: /**
101: * Sets the default bus if a default bus is not already set.
102: * @param bus the default bus.
103: * @return true if the bus was not set and is now set
104: */
105: public static synchronized boolean possiblySetDefaultBus(Bus bus) {
106: if (localBus.get() == null) {
107: localBus.set(bus);
108: }
109:
110: if (defaultBus == null) {
111: defaultBus = bus;
112: return true;
113: }
114: return false;
115: }
116:
117: /**
118: * Create a new BusFactory
119: *
120: * The class of the BusFactory is determined by looking for the system propery:
121: * org.apache.cxf.bus.factory
122: * or by searching the classpath for:
123: * META-INF/services/org.apache.cxf.bus.factory
124: *
125: * @return a new BusFactory to be used to create Bus objects
126: */
127: public static BusFactory newInstance() {
128: return newInstance(null);
129: }
130:
131: /**
132: * Create a new BusFactory
133: * @param className The class of the BusFactory to create. If null, uses the
134: * default search algorithm.
135: * @return a new BusFactory to be used to create Bus objects
136: */
137: public static BusFactory newInstance(String className) {
138: BusFactory instance = null;
139: ClassLoader classLoader = Thread.currentThread()
140: .getContextClassLoader();
141: if (className == null) {
142: className = getBusFactoryClass(classLoader);
143: }
144: Class<? extends BusFactory> busFactoryClass;
145: try {
146: busFactoryClass = Class.forName(className, true,
147: classLoader).asSubclass(BusFactory.class);
148: instance = busFactoryClass.newInstance();
149: } catch (Exception ex) {
150: LogUtils.log(LOG, Level.SEVERE,
151: "BUS_FACTORY_INSTANTIATION_EXC", ex);
152: throw new RuntimeException(ex);
153: }
154: return instance;
155: }
156:
157: protected void initializeBus(Bus bus) {
158: BusLifeCycleManager lifeCycleManager = bus
159: .getExtension(BusLifeCycleManager.class);
160: if (null != lifeCycleManager) {
161: lifeCycleManager.initComplete();
162: }
163: }
164:
165: private static String getBusFactoryClass(ClassLoader classLoader) {
166:
167: String busFactoryClass = null;
168: String busFactoryCondition = null;
169:
170: // next check system properties
171: busFactoryClass = System
172: .getProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME);
173: if (isValidBusFactoryClass(busFactoryClass)) {
174: return busFactoryClass;
175: }
176:
177: try {
178: // next, check for the services stuff in the jar file
179: String serviceId = "META-INF/services/"
180: + BusFactory.BUS_FACTORY_PROPERTY_NAME;
181: InputStream is = null;
182:
183: if (classLoader == null) {
184: classLoader = Thread.currentThread()
185: .getContextClassLoader();
186: }
187:
188: if (classLoader == null) {
189: is = ClassLoader.getSystemResourceAsStream(serviceId);
190: } else {
191: is = classLoader.getResourceAsStream(serviceId);
192: }
193: if (is != null) {
194: BufferedReader rd = new BufferedReader(
195: new InputStreamReader(is, "UTF-8"));
196: busFactoryClass = rd.readLine();
197: busFactoryCondition = rd.readLine();
198: rd.close();
199: }
200: if (isValidBusFactoryClass(busFactoryClass)) {
201: if (busFactoryCondition != null) {
202: try {
203: classLoader.loadClass(busFactoryCondition);
204: return busFactoryClass;
205: } catch (ClassNotFoundException e) {
206: return DEFAULT_BUS_FACTORY;
207: }
208: } else {
209: return busFactoryClass;
210: }
211: }
212:
213: // otherwise use default
214: busFactoryClass = BusFactory.DEFAULT_BUS_FACTORY;
215: return busFactoryClass;
216: } catch (Exception ex) {
217: LogUtils.log(LOG, Level.SEVERE,
218: "FAILED_TO_DETERMINE_BUS_FACTORY_EXC", ex);
219: }
220: return busFactoryClass;
221: }
222:
223: private static boolean isValidBusFactoryClass(
224: String busFactoryClassName) {
225: return busFactoryClassName != null
226: && !"".equals(busFactoryClassName);
227: }
228:
229: }
|