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.binding.coloc;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.SortedSet;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import org.apache.cxf.Bus;
028: import org.apache.cxf.endpoint.Endpoint;
029: import org.apache.cxf.interceptor.Interceptor;
030: import org.apache.cxf.interceptor.InterceptorChain;
031: import org.apache.cxf.message.Exchange;
032: import org.apache.cxf.phase.Phase;
033: import org.apache.cxf.phase.PhaseInterceptorChain;
034: import org.apache.cxf.service.model.FaultInfo;
035: import org.apache.cxf.service.model.MessageInfo;
036: import org.apache.cxf.service.model.MessagePartInfo;
037: import org.apache.cxf.service.model.OperationInfo;
038:
039: public final class ColocUtil {
040: //private static final ResourceBundle BUNDLE = BundleUtils.getBundle(ColocInInterceptor.class);
041: private static final Logger LOG = Logger.getLogger(ColocUtil.class
042: .getName());
043:
044: private ColocUtil() {
045: //Completge
046: }
047:
048: public static void setPhases(SortedSet<Phase> list, String start,
049: String end) {
050: Phase startPhase = new Phase(start, 1);
051: Phase endPhase = new Phase(end, 2);
052: Iterator<Phase> iter = list.iterator();
053: boolean remove = true;
054: while (iter.hasNext()) {
055: Phase p = iter.next();
056: if (remove && p.getName().equals(startPhase.getName())) {
057: remove = false;
058: } else if (p.getName().equals(endPhase.getName())) {
059: remove = true;
060: } else if (remove) {
061: iter.remove();
062: }
063: }
064: }
065:
066: public static InterceptorChain getOutInterceptorChain(Exchange ex,
067: SortedSet<Phase> phases) {
068: Bus bus = ex.get(Bus.class);
069: PhaseInterceptorChain chain = new PhaseInterceptorChain(phases);
070:
071: Endpoint ep = ex.get(Endpoint.class);
072: List<Interceptor> il = ep.getOutInterceptors();
073: if (LOG.isLoggable(Level.FINE)) {
074: LOG.fine("Interceptors contributed by endpoint: " + il);
075: }
076: chain.add(il);
077: il = ep.getService().getOutInterceptors();
078: if (LOG.isLoggable(Level.FINE)) {
079: LOG.fine("Interceptors contributed by service: " + il);
080: }
081: chain.add(il);
082: il = bus.getOutInterceptors();
083: if (LOG.isLoggable(Level.FINE)) {
084: LOG.fine("Interceptors contributed by bus: " + il);
085: }
086: chain.add(il);
087:
088: return chain;
089: }
090:
091: public static InterceptorChain getInInterceptorChain(Exchange ex,
092: SortedSet<Phase> phases) {
093: Bus bus = ex.get(Bus.class);
094: PhaseInterceptorChain chain = new PhaseInterceptorChain(phases);
095:
096: Endpoint ep = ex.get(Endpoint.class);
097: List<Interceptor> il = ep.getInInterceptors();
098: if (LOG.isLoggable(Level.FINE)) {
099: LOG.fine("Interceptors contributed by endpoint: " + il);
100: }
101: chain.add(il);
102: il = ep.getService().getInInterceptors();
103: if (LOG.isLoggable(Level.FINE)) {
104: LOG.fine("Interceptors contributed by service: " + il);
105: }
106: chain.add(il);
107: il = bus.getInInterceptors();
108: if (LOG.isLoggable(Level.FINE)) {
109: LOG.fine("Interceptors contributed by bus: " + il);
110: }
111: chain.add(il);
112: chain.setFaultObserver(new ColocOutFaultObserver(bus));
113:
114: return chain;
115: }
116:
117: public static boolean isSameOperationInfo(OperationInfo oi1,
118: OperationInfo oi2) {
119: return oi1.getName().equals(oi2.getName())
120: && isSameMessageInfo(oi1.getInput(), oi2.getInput())
121: && isSameMessageInfo(oi1.getOutput(), oi2.getOutput())
122: && isSameFaultInfo(oi1.getFaults(), oi2.getFaults());
123: }
124:
125: public static boolean isSameMessageInfo(MessageInfo mi1,
126: MessageInfo mi2) {
127: if ((mi1 == null && mi2 != null)
128: || (mi1 != null && mi2 == null)) {
129: return false;
130: }
131:
132: if (mi1 != null && mi2 != null) {
133: List<MessagePartInfo> mpil1 = mi1.getMessageParts();
134: List<MessagePartInfo> mpil2 = mi2.getMessageParts();
135: if (mpil1.size() != mpil2.size()) {
136: return false;
137: }
138: int idx = 0;
139: for (MessagePartInfo mpi1 : mpil1) {
140: MessagePartInfo mpi2 = mpil2.get(idx);
141: if (!mpi1.getTypeClass().equals(mpi2.getTypeClass())) {
142: return false;
143: }
144: ++idx;
145: }
146: }
147: return true;
148: }
149:
150: public static boolean isSameFaultInfo(Collection<FaultInfo> fil1,
151: Collection<FaultInfo> fil2) {
152: if ((fil1 == null && fil2 != null)
153: || (fil1 != null && fil2 == null)) {
154: return false;
155: }
156:
157: if (fil1 != null && fil2 != null) {
158: if (fil1.size() != fil2.size()) {
159: return false;
160: }
161: for (FaultInfo fi1 : fil1) {
162: Iterator<FaultInfo> iter = fil2.iterator();
163: Class<?> fiClass1 = fi1.getProperty(Class.class
164: .getName(), Class.class);
165: boolean match = false;
166: while (iter.hasNext()) {
167: FaultInfo fi2 = iter.next();
168: Class<?> fiClass2 = fi2.getProperty(Class.class
169: .getName(), Class.class);
170: //Sender/Receiver Service Model not same for faults wr.t message names.
171: //So Compare Exception Class Instance.
172: if (fiClass1.equals(fiClass2)) {
173: match = true;
174: break;
175: }
176: }
177: if (!match) {
178: return false;
179: }
180: }
181: }
182: return true;
183: }
184: }
|