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.ws.rm;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.concurrent.locks.Condition;
025: import java.util.concurrent.locks.Lock;
026: import java.util.concurrent.locks.ReentrantLock;
027:
028: import org.apache.cxf.helpers.CastUtils;
029: import org.apache.cxf.ws.rm.persistence.RMStore;
030:
031: public class Source extends AbstractEndpoint {
032:
033: private static final String REQUESTOR_SEQUENCE_ID = "";
034:
035: private Map<String, SourceSequence> map;
036: private Map<String, SourceSequence> current;
037: private Lock sequenceCreationLock;
038: private Condition sequenceCreationCondition;
039: private boolean sequenceCreationNotified;
040:
041: Source(RMEndpoint reliableEndpoint) {
042: super (reliableEndpoint);
043: map = new HashMap<String, SourceSequence>();
044: current = new HashMap<String, SourceSequence>();
045:
046: sequenceCreationLock = new ReentrantLock();
047: sequenceCreationCondition = sequenceCreationLock.newCondition();
048: }
049:
050: public SourceSequence getSequence(Identifier id) {
051: return map.get(id.getValue());
052: }
053:
054: public Collection<SourceSequence> getAllSequences() {
055: return CastUtils.cast(map.values());
056: }
057:
058: public void addSequence(SourceSequence seq) {
059: addSequence(seq, true);
060: }
061:
062: public void addSequence(SourceSequence seq, boolean persist) {
063: seq.setSource(this );
064: map.put(seq.getIdentifier().getValue(), seq);
065: if (persist) {
066: RMStore store = getReliableEndpoint().getManager()
067: .getStore();
068: if (null != store) {
069: store.createSourceSequence(seq);
070: }
071: }
072: }
073:
074: public void removeSequence(SourceSequence seq) {
075: map.remove(seq.getIdentifier().getValue());
076: RMStore store = getReliableEndpoint().getManager().getStore();
077: if (null != store) {
078: store.removeSourceSequence(seq.getIdentifier());
079: }
080: }
081:
082: /**
083: * Returns a collection of all sequences for which have not yet been
084: * completely acknowledged.
085: *
086: * @return the collection of unacknowledged sequences.
087: */
088: public Collection<SourceSequence> getAllUnacknowledgedSequences() {
089: Collection<SourceSequence> seqs = new ArrayList<SourceSequence>();
090: for (SourceSequence seq : map.values()) {
091: if (!seq.allAcknowledged()) {
092: seqs.add(seq);
093: }
094: }
095: return seqs;
096: }
097:
098: /**
099: * Returns the current sequence used by a client side source.
100: *
101: * @return the current sequence.
102: */
103: SourceSequence getCurrent() {
104: return getCurrent(null);
105: }
106:
107: /**
108: * Sets the current sequence used by a client side source.
109: * @param s the current sequence.
110: */
111: public void setCurrent(SourceSequence s) {
112: setCurrent(null, s);
113: }
114:
115: /**
116: * Returns the current sequence used by a server side source for responses to a message
117: * sent as part of the inbound sequence with the specified identifier.
118: *
119: * @return the current sequence.
120: */
121: SourceSequence getCurrent(Identifier i) {
122: sequenceCreationLock.lock();
123: try {
124: return getAssociatedSequence(i);
125: } finally {
126: sequenceCreationLock.unlock();
127: }
128: }
129:
130: /**
131: * Returns the sequence associated with the given identifier.
132: *
133: * @param i the corresponding sequence identifier
134: * @return the associated sequence
135: * @pre the sequenceCreationLock is already held
136: */
137: SourceSequence getAssociatedSequence(Identifier i) {
138: return current.get(i == null ? REQUESTOR_SEQUENCE_ID : i
139: .getValue());
140: }
141:
142: /**
143: * Await the availability of a sequence corresponding to the given identifier.
144: *
145: * @param i the sequence identifier
146: * @return
147: */
148: SourceSequence awaitCurrent(Identifier i) {
149: sequenceCreationLock.lock();
150: try {
151: SourceSequence seq = getAssociatedSequence(i);
152: while (seq == null) {
153: while (!sequenceCreationNotified) {
154: try {
155: sequenceCreationCondition.await();
156: } catch (InterruptedException ie) {
157: // ignore
158: }
159: }
160: seq = getAssociatedSequence(i);
161: }
162: return seq;
163: } finally {
164: sequenceCreationLock.unlock();
165: }
166: }
167:
168: /**
169: * Sets the current sequence used by a server side source for responses to a message
170: * sent as part of the inbound sequence with the specified identifier.
171: * @param s the current sequence.
172: */
173: void setCurrent(Identifier i, SourceSequence s) {
174: sequenceCreationLock.lock();
175: try {
176: current.put(i == null ? REQUESTOR_SEQUENCE_ID : i
177: .getValue(), s);
178: sequenceCreationNotified = true;
179: sequenceCreationCondition.signal();
180: } finally {
181: sequenceCreationLock.unlock();
182: }
183: }
184: }
|