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: package com.sun.xml.ws.transport.tcp.util;
037:
038: import java.io.IOException;
039: import java.nio.channels.Selector;
040: import java.util.EmptyStackException;
041: import java.util.Stack;
042:
043: /**
044: * Class was copied from GlassFish Grizzly sources to be available
045: * also for client side and don't require GlassFish to be installed
046:
047: * Factory used to dispatch/share <code>Selector</code>.
048: *
049: * @author Scott Oaks
050: * @author Jean-Francois Arcand
051: */
052: public final class SelectorFactory {
053:
054: /**
055: * The timeout before we exit.
056: */
057: static long timeout = 5000;
058:
059: /**
060: * The number of <code>Selector</code> to create.
061: */
062: static int maxSelectors = 20;
063:
064: /**
065: * Cache of <code>Selector</code>
066: */
067: private final static Stack<Selector> selectors = new Stack<Selector>();
068:
069: /**
070: * Creates the <code>Selector</code>
071: */
072: static {
073: try {
074: for (int i = 0; i < maxSelectors; i++)
075: selectors.add(Selector.open());
076: } catch (IOException ex) {
077: }
078: }
079:
080: /**
081: * Get a exclusive <code>Selector</code>
082: */
083: public static Selector getSelector() {
084: synchronized (selectors) {
085: Selector s = null;
086: try {
087: if (selectors.size() != 0)
088: s = selectors.pop();
089: } catch (EmptyStackException ex) {
090: }
091:
092: int attempts = 0;
093: try {
094: while (s == null && attempts < 2) {
095: selectors.wait(timeout);
096: try {
097: if (selectors.size() != 0)
098: s = selectors.pop();
099: } catch (EmptyStackException ex) {
100: break;
101: }
102: attempts++;
103: }
104: } catch (InterruptedException ex) {
105: }
106: ;
107: return s;
108: }
109: }
110:
111: /**
112: * Return the <code>Selector</code> to the cache
113: */
114: public static void returnSelector(final Selector s) {
115: synchronized (selectors) {
116: selectors.push(s);
117: if (selectors.size() == 1)
118: selectors.notify();
119: }
120: }
121:
122: }
|