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.endpoint;
019:
020: import java.io.IOException;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import javax.management.JMException;
025:
026: import org.apache.cxf.Bus;
027: import org.apache.cxf.BusException;
028: import org.apache.cxf.binding.BindingFactory;
029: import org.apache.cxf.common.logging.LogUtils;
030: import org.apache.cxf.management.InstrumentationManager;
031: import org.apache.cxf.service.model.EndpointInfo;
032: import org.apache.cxf.transport.Destination;
033: import org.apache.cxf.transport.DestinationFactory;
034: import org.apache.cxf.transport.DestinationFactoryManager;
035: import org.apache.cxf.transport.MessageObserver;
036: import org.apache.cxf.transport.MultipleEndpointObserver;
037:
038: public class ServerImpl implements Server {
039: private static final Logger LOG = LogUtils
040: .getL7dLogger(ServerImpl.class);
041: private Destination destination;
042: private Endpoint endpoint;
043: private ServerRegistry serverRegistry;
044: private Bus bus;
045: private ServerLifeCycleManager mgr;
046: private BindingFactory bindingFactory;
047: private MessageObserver messageObserver;
048:
049: public ServerImpl(Bus bus, Endpoint endpoint,
050: DestinationFactory destinationFactory,
051: MessageObserver observer) throws BusException, IOException {
052: this .endpoint = endpoint;
053: this .bus = bus;
054: this .messageObserver = observer;
055:
056: initDestination(destinationFactory);
057: }
058:
059: public ServerImpl(Bus bus, Endpoint endpoint,
060: DestinationFactory destinationFactory,
061: BindingFactory bindingFactory) throws BusException,
062: IOException {
063: this .endpoint = endpoint;
064: this .bus = bus;
065: this .bindingFactory = bindingFactory;
066:
067: initDestination(destinationFactory);
068: }
069:
070: private void initDestination(DestinationFactory destinationFactory)
071: throws BusException, IOException {
072: EndpointInfo ei = endpoint.getEndpointInfo();
073:
074: //Treat local transport as a special case, transports loaded by transportId can be replaced
075: //by local transport when the publishing address is a local transport protocol.
076: //Of course its not an ideal situation here to use a hard-coded prefix. To be refactored.
077: if (destinationFactory == null) {
078: if (ei.getAddress() != null
079: && ei.getAddress().indexOf("local://") != -1) {
080: destinationFactory = bus.getExtension(
081: DestinationFactoryManager.class)
082: .getDestinationFactoryForUri(ei.getAddress());
083: }
084:
085: if (destinationFactory == null) {
086: destinationFactory = bus.getExtension(
087: DestinationFactoryManager.class)
088: .getDestinationFactory(ei.getTransportId());
089: }
090: }
091:
092: destination = destinationFactory.getDestination(ei);
093: serverRegistry = bus.getExtension(ServerRegistry.class);
094:
095: ManagedEndpoint mep = new ManagedEndpoint(bus, endpoint, this );
096: mgr = bus.getExtension(ServerLifeCycleManager.class);
097: if (mgr != null) {
098: mgr.registerListener(mep);
099: }
100:
101: InstrumentationManager manager = bus
102: .getExtension(InstrumentationManager.class);
103: if (manager != null) {
104: try {
105: manager.register(mep);
106: } catch (JMException jmex) {
107: LOG.log(Level.WARNING,
108: "Registering ManagedEndpoint failed.", jmex);
109: }
110: }
111: }
112:
113: public Destination getDestination() {
114: return destination;
115: }
116:
117: public void setDestination(Destination destination) {
118: this .destination = destination;
119: }
120:
121: public void start() {
122: if (messageObserver != null) {
123: destination.setMessageObserver(messageObserver);
124: } else {
125: bindingFactory.addListener(destination, endpoint);
126: }
127:
128: // register the active server to run
129: if (null != serverRegistry) {
130: LOG.fine("register the server to serverRegistry ");
131: serverRegistry.register(this );
132: }
133: mgr = bus.getExtension(ServerLifeCycleManager.class);
134: if (mgr != null) {
135: mgr.startServer(this );
136: }
137: }
138:
139: public void stop() {
140: LOG.fine("Server is stopping.");
141: if (mgr != null) {
142: mgr.stopServer(this );
143: }
144:
145: MessageObserver mo = getDestination().getMessageObserver();
146: if (mo instanceof MultipleEndpointObserver) {
147: ((MultipleEndpointObserver) mo).getEndpoints().remove(
148: endpoint);
149: } else {
150: getDestination().setMessageObserver(null);
151: getDestination().shutdown();
152: }
153:
154: if (null != serverRegistry) {
155: LOG.fine("unregister the server to serverRegistry ");
156: serverRegistry.unregister(this );
157: }
158: }
159:
160: public Endpoint getEndpoint() {
161: return endpoint;
162: }
163:
164: public MessageObserver getMessageObserver() {
165: return messageObserver;
166: }
167:
168: public void setMessageObserver(MessageObserver messageObserver) {
169: this.messageObserver = messageObserver;
170: }
171:
172: }
|