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.phase;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.ListIterator;
023: import java.util.SortedSet;
024: import java.util.concurrent.atomic.AtomicReference;
025:
026: import org.apache.cxf.common.util.ModCountCopyOnWriteArrayList;
027: import org.apache.cxf.interceptor.Interceptor;
028:
029: public final class PhaseChainCache {
030: AtomicReference<ChainHolder> lastData = new AtomicReference<ChainHolder>();
031:
032: @SuppressWarnings("unchecked")
033: public PhaseInterceptorChain get(SortedSet<Phase> phaseList,
034: List<Interceptor> p1) {
035: return getChain(phaseList, p1);
036: }
037:
038: @SuppressWarnings("unchecked")
039: public PhaseInterceptorChain get(SortedSet<Phase> phaseList,
040: List<Interceptor> p1, List<Interceptor> p2) {
041: return getChain(phaseList, p1, p2);
042: }
043:
044: @SuppressWarnings("unchecked")
045: public PhaseInterceptorChain get(SortedSet<Phase> phaseList,
046: List<Interceptor> p1, List<Interceptor> p2,
047: List<Interceptor> p3) {
048: return getChain(phaseList, p1, p2, p3);
049: }
050:
051: @SuppressWarnings("unchecked")
052: public PhaseInterceptorChain get(SortedSet<Phase> phaseList,
053: List<Interceptor> p1, List<Interceptor> p2,
054: List<Interceptor> p3, List<Interceptor> p4) {
055: return getChain(phaseList, p1, p2, p3, p4);
056: }
057:
058: @SuppressWarnings("unchecked")
059: public PhaseInterceptorChain get(SortedSet<Phase> phaseList,
060: List<Interceptor> p1, List<Interceptor> p2,
061: List<Interceptor> p3, List<Interceptor> p4,
062: List<Interceptor> p5) {
063: return getChain(phaseList, p1, p2, p3, p4, p5);
064: }
065:
066: private PhaseInterceptorChain getChain(SortedSet<Phase> phaseList,
067: List<Interceptor>... providers) {
068: ChainHolder last = lastData.get();
069:
070: if (last == null || !last.matches(providers)) {
071:
072: PhaseInterceptorChain chain = new PhaseInterceptorChain(
073: phaseList);
074: List<ModCountCopyOnWriteArrayList<Interceptor>> copy = new ArrayList<ModCountCopyOnWriteArrayList<Interceptor>>(
075: providers.length);
076: for (List<Interceptor> p : providers) {
077: copy.add(new ModCountCopyOnWriteArrayList<Interceptor>(
078: p));
079: chain.add(p);
080: }
081: last = new ChainHolder(chain, copy);
082: lastData.set(last);
083: }
084:
085: return last.chain.cloneChain();
086: }
087:
088: private static class ChainHolder {
089: List<ModCountCopyOnWriteArrayList<Interceptor>> lists;
090: PhaseInterceptorChain chain;
091:
092: ChainHolder(PhaseInterceptorChain c,
093: List<ModCountCopyOnWriteArrayList<Interceptor>> l) {
094: lists = l;
095: chain = c;
096: }
097:
098: boolean matches(List<Interceptor>... providers) {
099: if (lists.size() == providers.length) {
100: for (int x = 0; x < providers.length; x++) {
101: if (lists.get(x).size() != providers[x].size()) {
102: return false;
103: }
104:
105: if (providers[x].getClass() == ModCountCopyOnWriteArrayList.class) {
106: if (((ModCountCopyOnWriteArrayList) providers[x])
107: .getModCount() != lists.get(x)
108: .getModCount()) {
109: return false;
110: }
111: } else {
112: ListIterator<Interceptor> i1 = lists.get(x)
113: .listIterator();
114: ListIterator<Interceptor> i2 = providers[x]
115: .listIterator();
116:
117: while (i1.hasNext()) {
118: if (i1.next() != i2.next()) {
119: return false;
120: }
121: }
122: }
123: }
124: return true;
125: }
126: return false;
127: }
128: }
129: }
|