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: */
019: package org.apache.axis2.engine;
020:
021: import org.apache.axis2.AxisFault;
022: import org.apache.axis2.addressing.EndpointReference;
023: import org.apache.axis2.context.ConfigurationContext;
024: import org.apache.axis2.description.AxisModule;
025: import org.apache.axis2.description.AxisService;
026: import org.apache.axis2.description.TransportInDescription;
027: import org.apache.axis2.description.TransportOutDescription;
028: import org.apache.axis2.i18n.Messages;
029: import org.apache.axis2.modules.Module;
030: import org.apache.axis2.transport.TransportListener;
031: import org.apache.axis2.transport.TransportSender;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: public class ListenerManager {
040:
041: private static final Log log = LogFactory
042: .getLog(ListenerManager.class);
043:
044: public static ConfigurationContext defaultConfigurationContext;
045:
046: public static ListenerManager getDefaultListenerManager() {
047: if (defaultConfigurationContext == null)
048: return null;
049: return defaultConfigurationContext.getListenerManager();
050: }
051:
052: private ConfigurationContext configctx;
053: private HashMap startedTransports = new HashMap();
054: private boolean stopped;
055:
056: public void init(ConfigurationContext configCtx) {
057: configCtx.setTransportManager(this );
058: this .configctx = configCtx;
059: }
060:
061: public ConfigurationContext getConfigctx() {
062: return configctx;
063: }
064:
065: /**
066: * To get an EPR for a given service
067: *
068: * @param serviceName : Name of the service
069: * @param transportName : name of the trasport can be null , if it is null then
070: * @return String
071: */
072: public synchronized EndpointReference getEPRforService(
073: String serviceName, String opName, String transportName)
074: throws AxisFault {
075: if (transportName == null || "".equals(transportName)) {
076: AxisService service = configctx.getAxisConfiguration()
077: .getService(serviceName);
078: if (service == null) {
079: throw new AxisFault(Messages.getMessage(
080: "servicenotfoundinthesystem", serviceName));
081: }
082: if (service.isEnableAllTransports()) {
083: Iterator itr_st = startedTransports.values().iterator();
084: while (itr_st.hasNext()) {
085: TransportListener transportListener = (TransportListener) itr_st
086: .next();
087: EndpointReference[] epRsForService = transportListener
088: .getEPRsForService(serviceName, null);
089: if (epRsForService != null) {
090: return epRsForService[0];
091: }
092: }
093:
094: // if nothing can be found return null
095: return null;
096:
097: } else {
098: List exposeTransport = service.getExposedTransports();
099: TransportListener listener = (TransportListener) startedTransports
100: .get(exposeTransport.get(0));
101:
102: EndpointReference[] eprsForService;
103: eprsForService = listener.getEPRsForService(
104: serviceName, null);
105: return eprsForService != null ? eprsForService[0]
106: : null;
107: }
108:
109: } else {
110: TransportInDescription trsIN = configctx
111: .getAxisConfiguration().getTransportIn(
112: transportName);
113: TransportListener listener = trsIN.getReceiver();
114: EndpointReference[] eprsForService;
115: eprsForService = listener.getEPRsForService(serviceName,
116: null);
117: return eprsForService != null ? eprsForService[0] : null;
118: }
119: }
120:
121: /**
122: * To start all the transports
123: */
124: public synchronized void start() {
125:
126: for (Iterator transportNames = configctx.getAxisConfiguration()
127: .getTransportsIn().values().iterator(); transportNames
128: .hasNext();) {
129: try {
130: TransportInDescription transportIn = (TransportInDescription) transportNames
131: .next();
132: TransportListener listener = transportIn.getReceiver();
133: if (listener != null
134: && startedTransports.get(transportIn.getName()) == null) {
135: listener.init(configctx, transportIn);
136: listener.start();
137: if (startedTransports.get(transportIn.getName()) == null) {
138: startedTransports.put(transportIn.getName(),
139: listener);
140: }
141: }
142: } catch (Exception e) {
143: log.info(e.getMessage());
144: }
145: }
146: Runtime.getRuntime().addShutdownHook(
147: new ListenerManagerShutdownThread(this ));
148: }
149:
150: public synchronized void startSystem(
151: ConfigurationContext configurationContext) {
152: init(configurationContext);
153: start();
154: }
155:
156: /**
157: * Stop all the transports and notify modules of shutdown.
158: */
159: public synchronized void stop() throws AxisFault {
160: if (stopped) {
161: return;
162: }
163:
164: for (Iterator iter = startedTransports.values().iterator(); iter
165: .hasNext();) {
166: TransportListener transportListener = (TransportListener) iter
167: .next();
168: transportListener.stop();
169: }
170:
171: /*Stop the transport senders*/
172: HashMap transportOut = configctx.getAxisConfiguration()
173: .getTransportsOut();
174: if (transportOut.size() > 0) {
175: Iterator trsItr = transportOut.values().iterator();
176: while (trsItr.hasNext()) {
177: TransportOutDescription outDescription = (TransportOutDescription) trsItr
178: .next();
179: TransportSender trsSededer = outDescription.getSender();
180: if (trsSededer != null) {
181: trsSededer.stop();
182: }
183: }
184: }
185: /*Shut down the modules*/
186: HashMap modules = configctx.getAxisConfiguration().getModules();
187: if (modules != null) {
188: Iterator moduleitr = modules.values().iterator();
189: while (moduleitr.hasNext()) {
190: AxisModule axisModule = (AxisModule) moduleitr.next();
191: Module module = axisModule.getModule();
192: if (module != null) {
193: module.shutdown(configctx);
194: }
195: }
196: }
197: configctx.cleanupContexts();
198: /*Shut down the services*/
199: for (Iterator services = configctx.getAxisConfiguration()
200: .getServices().values().iterator(); services.hasNext();) {
201: AxisService axisService = (AxisService) services.next();
202: ServiceLifeCycle serviceLifeCycle = axisService
203: .getServiceLifeCycle();
204: if (serviceLifeCycle != null) {
205: serviceLifeCycle.shutDown(configctx, axisService);
206: }
207: }
208: stopped = true;
209: }
210:
211: /**
212: * @param trsIn : Transport in description (which contains Transport Listener)
213: * @param started : whether transport Listener running or not
214: * @throws AxisFault : will throw AxisFault if something goes wrong
215: */
216: public synchronized void addListener(TransportInDescription trsIn,
217: boolean started) throws AxisFault {
218: configctx.getAxisConfiguration().addTransportIn(trsIn);
219: TransportListener transportListener = trsIn.getReceiver();
220: if (transportListener != null) {
221: if (!started) {
222: transportListener.init(configctx, trsIn);
223: transportListener.start();
224: }
225: startedTransports.put(trsIn.getName(), transportListener);
226: }
227: }
228:
229: public synchronized boolean isListenerRunning(String transportName) {
230: return startedTransports.get(transportName) != null;
231: }
232:
233: public boolean isStopped() {
234: return stopped;
235: }
236:
237: public void destroy() throws AxisFault {
238: stop();
239: this .configctx.setTransportManager(null);
240: for (Iterator iter = startedTransports.values().iterator(); iter
241: .hasNext();) {
242: TransportListener transportListener = (TransportListener) iter
243: .next();
244: transportListener.destroy();
245: }
246: this .startedTransports.clear();
247: this .configctx = null;
248: defaultConfigurationContext = null;
249: }
250:
251: static class ListenerManagerShutdownThread extends Thread {
252: ListenerManager listenerManager;
253:
254: public ListenerManagerShutdownThread(
255: ListenerManager listenerManager) {
256: super ();
257: this .listenerManager = listenerManager;
258: }
259:
260: public void run() {
261: try {
262: if (!listenerManager.stopped) {
263: listenerManager.stop();
264: }
265: } catch (AxisFault axisFault) {
266: log.error(axisFault.getMessage(), axisFault);
267: }
268: }
269: }
270: }
|