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.Logger;
022:
023: import org.apache.cxf.Bus;
024: import org.apache.cxf.BusException;
025: import org.apache.cxf.interceptor.Fault;
026: import org.apache.cxf.message.Exchange;
027: import org.apache.cxf.message.Message;
028: import org.apache.cxf.service.model.EndpointInfo;
029: import org.apache.cxf.transport.Conduit;
030: import org.apache.cxf.transport.ConduitInitiator;
031: import org.apache.cxf.transport.ConduitInitiatorManager;
032: import org.apache.cxf.transport.MessageObserver;
033:
034: /**
035: * Abstract base class holding logic common to any ConduitSelector
036: * that retreives a Conduit from the ConduitInitiator.
037: */
038: public abstract class AbstractConduitSelector implements
039: ConduitSelector {
040:
041: protected Conduit selectedConduit;
042: protected Endpoint endpoint;
043:
044: /**
045: * Constructor, allowing a specific conduit to override normal selection.
046: *
047: * @param c specific conduit
048: */
049: public AbstractConduitSelector(Conduit c) {
050: selectedConduit = c;
051: }
052:
053: /**
054: * Mechanics to actually get the Conduit from the ConduitInitiator
055: * if necessary.
056: *
057: * @param message the current Message
058: */
059: protected Conduit getSelectedConduit(Message message) {
060: if (selectedConduit == null) {
061: Exchange exchange = message.getExchange();
062: EndpointInfo ei = endpoint.getEndpointInfo();
063: String transportID = ei.getTransportId();
064: try {
065: ConduitInitiatorManager conduitInitiatorMgr = getBus(
066: exchange).getExtension(
067: ConduitInitiatorManager.class);
068: if (conduitInitiatorMgr != null) {
069: ConduitInitiator conduitInitiator = conduitInitiatorMgr
070: .getConduitInitiator(transportID);
071: if (conduitInitiator != null) {
072: selectedConduit = conduitInitiator
073: .getConduit(ei);
074: MessageObserver observer = exchange
075: .get(MessageObserver.class);
076: if (observer != null) {
077: selectedConduit
078: .setMessageObserver(observer);
079: } else {
080: getLogger().warning(
081: "MessageObserver not found");
082: }
083: } else {
084: getLogger().warning(
085: "ConduitInitiator not found: "
086: + ei.getAddress());
087: }
088: } else {
089: getLogger().warning(
090: "ConduitInitiatorManager not found");
091: }
092: } catch (BusException ex) {
093: throw new Fault(ex);
094: } catch (IOException ex) {
095: throw new Fault(ex);
096: }
097: }
098: return selectedConduit;
099: }
100:
101: /**
102: * @return the encapsulated Endpoint
103: */
104: public Endpoint getEndpoint() {
105: return endpoint;
106: }
107:
108: /**
109: * @param ep the endpoint to encapsulate
110: */
111: public void setEndpoint(Endpoint ep) {
112: endpoint = ep;
113: }
114:
115: /**
116: * Get the associated Bus instance from the exchange.
117: *
118: * @param exchange the current exchange
119: * @return the Bus instance
120: */
121: private Bus getBus(Exchange exchange) {
122: return exchange.get(Bus.class);
123: }
124:
125: /**
126: * Called on completion of the MEP for which the Conduit was required.
127: *
128: * @param exchange represents the completed MEP
129: */
130: public void complete(Exchange exchange) {
131: try {
132: if (exchange.getInMessage() != null) {
133: getSelectedConduit(exchange.getInMessage()).close(
134: exchange.getInMessage());
135: }
136: } catch (IOException e) {
137: //IGNORE
138: }
139: }
140:
141: /**
142: * @return the logger to use
143: */
144: protected abstract Logger getLogger();
145: }
|