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: package com.sun.xml.ws.transport.tcp.client;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
042: import com.sun.xml.ws.transport.tcp.util.SessionAbortedException;
043: import com.sun.xml.ws.transport.tcp.util.ConnectionSession;
044: import java.util.Collections;
045:
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.Map;
049: import java.util.Set;
050: import java.util.concurrent.ConcurrentLinkedQueue;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * @author Alexey Stashok
056: */
057: public class WSConnectionCache {
058: // private static final Logger logger = Logger.getLogger(
059: // com.sun.xml.ws.transport.tcp.util.TCPConstants.LoggingDomain + ".client");
060: //
061: // // map contains all connection sessions to certain destination
062: // private final Map<Integer, Set<ConnectionSession>> allDstAddress2connectionSession;
063: //
064: // // map contains available connection sessions to certain destination (MAX_CHANNELS is not reached)
065: // private final Map<Integer, ConcurrentLinkedQueue<ConnectionSession>> availableDstAddress2connectionSession;
066: //
067: // // set of locked connections, which are in use
068: // private final Map<ConnectionSession, Thread> lockedConnections;
069: //
070: // public WSConnectionCache() {
071: // allDstAddress2connectionSession = new HashMap<Integer, Set<ConnectionSession>>();
072: // availableDstAddress2connectionSession = new HashMap<Integer, ConcurrentLinkedQueue<ConnectionSession>>();
073: // lockedConnections = new HashMap<ConnectionSession, Thread>();
074: // }
075: //
076: // public void registerConnectionSession(@NotNull final ConnectionSession connectionSession, final int dstAddrHashKey) {
077: // ConcurrentLinkedQueue<ConnectionSession> availableConnectionSessions = availableDstAddress2connectionSession.get(dstAddrHashKey);
078: // Set<ConnectionSession> allConnectionSessions = allDstAddress2connectionSession.get(dstAddrHashKey);
079: // synchronized(this) {
080: // //check if there is a record for such destination address
081: // if (allConnectionSessions == null) {
082: // allConnectionSessions = new HashSet<ConnectionSession>();
083: // allDstAddress2connectionSession.put(dstAddrHashKey, allConnectionSessions);
084: // availableConnectionSessions = new ConcurrentLinkedQueue<ConnectionSession>();
085: // availableDstAddress2connectionSession.put(dstAddrHashKey, availableConnectionSessions);
086: // }
087: // }
088: // availableConnectionSessions.offer(connectionSession);
089: // allConnectionSessions.add(connectionSession);
090: // }
091: //
092: // /**
093: // * Get all active sessions for given destination host:port
094: // */
095: // public @NotNull Set<ConnectionSession> getAllConnectionsByAddr(final int dstAddrHashKey) {
096: // final Set<ConnectionSession> allConnectionSessions = allDstAddress2connectionSession.get(dstAddrHashKey);
097: // return allConnectionSessions != null ? allConnectionSessions : Collections.<ConnectionSession>emptySet();
098: // }
099: //
100: // /**
101: // * Get session, where it is available to create one more virtual connection
102: // */
103: // public @Nullable ConnectionSession pollAvailableConnectionByAddr(final int dstAddrHashKey) {
104: // final ConcurrentLinkedQueue<ConnectionSession> availableConnectionSessions = availableDstAddress2connectionSession.get(dstAddrHashKey);
105: // return availableConnectionSessions != null ? availableConnectionSessions.poll() : null;
106: // }
107: //
108: // /**
109: // * Put back session to available session list
110: // */
111: // public void offerAvailableConnectionByAddr(@NotNull final ConnectionSession connectionSession, final int dstAddrHashKey) {
112: // final ConcurrentLinkedQueue<ConnectionSession> availableConnectionSessions = availableDstAddress2connectionSession.get(dstAddrHashKey);
113: // availableConnectionSessions.offer(connectionSession);
114: // }
115: //
116: // /**
117: // * Destroy connection session
118: // */
119: // public void removeConnectionSession(final @NotNull ConnectionSession tcpConnectionSession) {
120: // final int addressHashKey = tcpConnectionSession.getDstAddressHashKey();
121: // final Set<ConnectionSession> allConnectionSessions = allDstAddress2connectionSession.get(addressHashKey);
122: //
123: // // method is called before ConnectionSession was registered in cache
124: // if (allConnectionSessions != null) {
125: // final ConcurrentLinkedQueue<ConnectionSession> availableConnectionSessions = availableDstAddress2connectionSession.get(addressHashKey);
126: //
127: // synchronized(tcpConnectionSession) {
128: // // remove session from all and available lists
129: // allConnectionSessions.remove(tcpConnectionSession);
130: // availableConnectionSessions.remove(tcpConnectionSession);
131: //
132: // unlockConnection(tcpConnectionSession);
133: // tcpConnectionSession.notifyAll();
134: // }
135: // }
136: // }
137: //
138: // public void lockConnection(final @NotNull ConnectionSession tcpConnectionSession) throws InterruptedException, SessionAbortedException {
139: // logger.log(Level.FINEST, MessagesMessages.WSTCP_1020_CONNECTION_CACHE_ENTER());
140: // final Thread lockedThread = lockedConnections.get(tcpConnectionSession);
141: // if (Thread.currentThread().equals(lockedThread)) return;
142: //
143: // synchronized(tcpConnectionSession) {
144: // logger.log(Level.FINEST, MessagesMessages.WSTCP_1021_CONNECTION_CACHE_SYNC());
145: // while(lockedConnections.containsKey(tcpConnectionSession)) {
146: // tcpConnectionSession.wait();
147: // }
148: //
149: // // check whether session was aborted?
150: // final Set<ConnectionSession> allConnectionSessions = allDstAddress2connectionSession.get(tcpConnectionSession.getDstAddressHashKey());
151: // if (allConnectionSessions.contains(tcpConnectionSession)) {
152: // logger.log(Level.FINEST, MessagesMessages.WSTCP_1022_CONNECTION_CACHE_LOCK());
153: // lockedConnections.put(tcpConnectionSession, Thread.currentThread());
154: // } else {
155: // logger.log(Level.FINEST, MessagesMessages.WSTCP_1023_CONNECTION_CACHE_SESSION_ABORTED());
156: // throw new SessionAbortedException();
157: // }
158: // }
159: // }
160: //
161: // public void unlockConnection(final @NotNull ConnectionSession tcpConnectionSession) {
162: // synchronized(tcpConnectionSession) {
163: // lockedConnections.remove(tcpConnectionSession);
164: // tcpConnectionSession.notify();
165: // }
166: // }
167: }
|