001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * RMDestination.java
039: *
040: * @author Mike Grogan
041: * Created on October 15, 2005, 6:24 PM
042: */
043:
044: package com.sun.xml.ws.rm.jaxws.runtime.server;
045:
046: import com.sun.xml.ws.rm.InvalidSequenceException;
047: import com.sun.xml.ws.rm.RMException;
048: import com.sun.xml.ws.rm.Constants;
049: import com.sun.xml.ws.rm.jaxws.runtime.RMProvider;
050: import com.sun.xml.ws.rm.jaxws.runtime.SequenceConfig;
051: import java.net.URI;
052: import java.util.*;
053:
054: /**
055: * An RMDestination represents a Collection of Inbound RMSequences.
056: */
057: public class RMDestination extends
058: RMProvider<ServerInboundSequence, ServerOutboundSequence> {
059:
060: private static RMDestination rmDestination = new RMDestination();
061:
062: public static RMDestination getRMDestination() {
063: return rmDestination;
064: }
065:
066: //TODO - make an intelligent choice for wake-up interval.
067: private SequenceReaper reaper = new SequenceReaper(5000, inboundMap);
068:
069: private RMDestination() {
070: }
071:
072: public void terminateSequence(String id)
073: throws InvalidSequenceException {
074: ServerInboundSequence seq = getInboundSequence(id);
075:
076: if (seq == null) {
077: throw new InvalidSequenceException(String.format(
078: Constants.UNKNOWN_SEQUENCE_TEXT, id), id);
079: }
080:
081: ServerOutboundSequence out = (ServerOutboundSequence) seq
082: .getOutboundSequence();
083:
084: synchronized (this ) {
085: if (seq != null) {
086: inboundMap.remove(id);
087: }
088:
089: if (inboundMap.isEmpty()) {
090: reaper.stop();
091: }
092: }
093:
094: if (out != null) {
095: String outid = out.getId();
096: if (outid != null) {
097: outboundMap.remove(outid);
098: }
099: }
100: }
101:
102: //TODO add endpoint address argument to this method and corresponding
103: //member in ServerInboundSequence
104: public ServerInboundSequence createSequence(URI acksTo,
105: String inboundId, String outboundId, SequenceConfig config)
106: throws RMException {
107:
108: ServerInboundSequence seq = new ServerInboundSequence(acksTo,
109: inboundId, outboundId, config);
110:
111: synchronized (this ) {
112: inboundMap.put(seq.getId(), seq);
113:
114: if (inboundMap.size() == 1) {
115: reaper.start();
116: }
117: }
118:
119: ServerOutboundSequence outbound = (ServerOutboundSequence) seq
120: .getOutboundSequence();
121: String id = outbound.getId();
122:
123: if (id != null) {
124: outboundMap.put(id, outbound);
125: }
126:
127: return seq;
128: }
129:
130: /**
131: * SequenceReaper is a timer with a single task that periodically checks the map
132: * of active ServerInboundSequences for expired ones an peremptorily terminates them.
133: */
134: private class SequenceReaper extends Timer {
135:
136: private long frequency;
137: private Map<String, ServerInboundSequence> map;
138:
139: private TimerTask timerTask;
140:
141: public void start() {
142: timerTask = new TimerTask() {
143: public void run() {
144: //go though all the sequences and shut down any that
145: //are expired.
146: HashSet<String> keysToRemove = new HashSet<String>();
147: for (String key : map.keySet()) {
148:
149: ServerInboundSequence sis = map.get(key);
150: synchronized (sis) {
151: if (sis.isExpired()) {
152: System.out
153: .println("Terminating expired sequence "
154: + sis.getId());
155: keysToRemove.add(key);
156: }
157: }
158: }
159:
160: for (String str : keysToRemove) {
161: try {
162: terminateSequence(str);
163: } catch (Exception e) {
164: e.printStackTrace();
165: }
166: }
167:
168: }
169: };
170:
171: schedule(timerTask, new Date(System.currentTimeMillis()
172: + frequency), frequency);
173: }
174:
175: public void stop() {
176: timerTask.cancel();
177: }
178:
179: public SequenceReaper(long frequency,
180: Map<String, ServerInboundSequence> map) {
181: //make the Timer Thread a daemon.
182: super (true);
183: this.map = map;
184: this.frequency = frequency;
185:
186: }
187: }
188: }
|