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.transport;
019:
020: import java.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.List;
023: import java.util.Set;
024:
025: import org.apache.cxf.Bus;
026: import org.apache.cxf.endpoint.Endpoint;
027: import org.apache.cxf.interceptor.Interceptor;
028: import org.apache.cxf.message.Exchange;
029: import org.apache.cxf.message.ExchangeImpl;
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.phase.PhaseInterceptorChain;
032: import org.apache.cxf.phase.PhaseManager;
033:
034: /**
035: * This MessageObserver creates an Interceptor chain which adds in the interceptors
036: * set on this class and the global Bus interceptors. At somepoint, it is expected
037: * that these interceptors will resolve the appropriate Endpoint/Binding combination
038: * and continue setting up the chain.
039: *
040: */
041: public class MultipleEndpointObserver implements MessageObserver {
042:
043: public static final String ENDPOINTS = "multipleEndpointObserver.endpoints";
044:
045: protected Bus bus;
046: protected List<Interceptor> bindingInterceptors = new ArrayList<Interceptor>();
047: protected List<Interceptor> routingInterceptors = new ArrayList<Interceptor>();
048: private Set<Endpoint> endpoints = new HashSet<Endpoint>();
049:
050: public MultipleEndpointObserver(Bus bus) {
051: super ();
052: this .bus = bus;
053: }
054:
055: public void onMessage(Message message) {
056: message = createMessage(message);
057: Exchange exchange = message.getExchange();
058: if (exchange == null) {
059: exchange = new ExchangeImpl();
060: exchange.setInMessage(message);
061: }
062: setExchangeProperties(exchange, message);
063:
064: // setup chain
065: PhaseInterceptorChain chain = createChain();
066:
067: message.setInterceptorChain(chain);
068:
069: chain.add(bus.getInInterceptors());
070: if (bindingInterceptors != null) {
071: chain.add(bindingInterceptors);
072: }
073: if (routingInterceptors != null) {
074: chain.add(routingInterceptors);
075: }
076:
077: if (endpoints != null) {
078: exchange.put(ENDPOINTS, endpoints);
079: }
080:
081: chain.doIntercept(message);
082: }
083:
084: /**
085: * Give a chance for a Binding to customize their message
086: */
087: protected Message createMessage(Message message) {
088: return message;
089: }
090:
091: protected PhaseInterceptorChain createChain() {
092: PhaseInterceptorChain chain = new PhaseInterceptorChain(bus
093: .getExtension(PhaseManager.class).getInPhases());
094: return chain;
095: }
096:
097: protected void setExchangeProperties(Exchange exchange, Message m) {
098: exchange.put(Bus.class, bus);
099: if (exchange.getDestination() == null) {
100: exchange.setDestination(m.getDestination());
101: }
102: }
103:
104: public List<Interceptor> getBindingInterceptors() {
105: return bindingInterceptors;
106: }
107:
108: public void setBindingInterceptors(
109: List<Interceptor> bindingInterceptors) {
110: this .bindingInterceptors = bindingInterceptors;
111: }
112:
113: public List<Interceptor> getRoutingInterceptors() {
114: return routingInterceptors;
115: }
116:
117: public void setRoutingInterceptors(
118: List<Interceptor> routingInterceptors) {
119: this .routingInterceptors = routingInterceptors;
120: }
121:
122: public Set<Endpoint> getEndpoints() {
123: return endpoints;
124: }
125:
126: public void setEndpoints(Set<Endpoint> endpoints) {
127: this.endpoints = endpoints;
128: }
129:
130: }
|