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.bus;
019:
020: import java.util.Collection;
021: import java.util.Map;
022: import java.util.concurrent.ConcurrentHashMap;
023:
024: import org.apache.cxf.Bus;
025: import org.apache.cxf.BusFactory;
026: import org.apache.cxf.buslifecycle.BusLifeCycleManager;
027: import org.apache.cxf.feature.AbstractFeature;
028: import org.apache.cxf.interceptor.AbstractBasicInterceptorProvider;
029:
030: public class CXFBusImpl extends AbstractBasicInterceptorProvider
031: implements Bus {
032:
033: protected final Map<Class, Object> extensions;
034: private String id;
035: private BusState state;
036: private Collection<AbstractFeature> features;
037:
038: public CXFBusImpl() {
039: this (null);
040: }
041:
042: public CXFBusImpl(Map<Class, Object> extensions) {
043: if (extensions == null) {
044: extensions = new ConcurrentHashMap<Class, Object>();
045: } else {
046: extensions = new ConcurrentHashMap<Class, Object>(
047: extensions);
048: }
049: this .extensions = extensions;
050:
051: state = BusState.INITIAL;
052:
053: CXFBusFactory.possiblySetDefaultBus(this );
054: }
055:
056: protected void setState(BusState state) {
057: this .state = state;
058: }
059:
060: public void setId(String i) {
061: id = i;
062: }
063:
064: public final <T> T getExtension(Class<T> extensionType) {
065: Object obj = extensions.get(extensionType);
066: if (null != obj) {
067: return extensionType.cast(obj);
068: }
069: return null;
070: }
071:
072: public <T> void setExtension(T extension, Class<T> extensionType) {
073: extensions.put(extensionType, extension);
074: }
075:
076: public String getId() {
077: return null == id ? DEFAULT_BUS_ID
078: + Integer.toString(this .hashCode()) : id;
079: }
080:
081: public void run() {
082: synchronized (this ) {
083: if (state == BusState.RUNNING) {
084: // REVISIT
085: return;
086: }
087: state = BusState.RUNNING;
088:
089: while (state == BusState.RUNNING) {
090:
091: try {
092: wait();
093: } catch (InterruptedException ex) {
094: // ignore;
095: }
096: }
097: }
098: }
099:
100: public void initialize() {
101: initializeFeatures();
102: }
103:
104: private void initializeFeatures() {
105: if (features != null) {
106: for (AbstractFeature f : features) {
107: f.initialize(this );
108: }
109: }
110: }
111:
112: public void shutdown(boolean wait) {
113: BusLifeCycleManager lifeCycleManager = this
114: .getExtension(BusLifeCycleManager.class);
115: if (null != lifeCycleManager) {
116: lifeCycleManager.preShutdown();
117: }
118: synchronized (this ) {
119: state = BusState.SHUTDOWN;
120: notifyAll();
121: }
122: if (null != lifeCycleManager) {
123: lifeCycleManager.postShutdown();
124: }
125: if (BusFactory.getDefaultBus(false) == this ) {
126: BusFactory.setDefaultBus(null);
127: }
128: }
129:
130: protected BusState getState() {
131: return state;
132: }
133:
134: public Collection<AbstractFeature> getFeatures() {
135: return features;
136: }
137:
138: public synchronized void setFeatures(
139: Collection<AbstractFeature> features) {
140: this.features = features;
141:
142: if (state == BusState.RUNNING) {
143: initializeFeatures();
144: }
145: }
146:
147: }
|