KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > SelectorFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.web.connector.grizzly;
24
25 import java.io.IOException JavaDoc;
26 import java.nio.channels.Selector JavaDoc;
27 import java.util.EmptyStackException JavaDoc;
28 import java.util.Stack JavaDoc;
29
30 /**
31  * Factory used to dispatch/share <code>Selector</code>.
32  *
33  * @author Scott Oaks
34  * @author Jean-Francois Arcand
35  */

36 public class SelectorFactory{
37     
38     /**
39      * The timeout before we exit.
40      */

41     protected static long timeout = 5000;
42     
43     
44     /**
45      * The number of <code>Selector</code> to create.
46      */

47     protected static int maxSelectors = 20;
48     
49     
50     /**
51      * Cache of <code>Selector</code>
52      */

53     private final static Stack JavaDoc<Selector JavaDoc> selectors = new Stack JavaDoc<Selector JavaDoc>();
54     
55     
56     /**
57      * Creates the <code>Selector</code>
58      */

59     static {
60         try{
61             for (int i = 0; i < maxSelectors; i++)
62                 selectors.add(Selector.open());
63         } catch (IOException JavaDoc ex){
64             ; // do nothing.
65
}
66     }
67
68     
69     /**
70      * Get a exclusive <code>Selector</code>
71      */

72     public final static Selector JavaDoc getSelector() {
73         synchronized(selectors) {
74             Selector JavaDoc s = null;
75             try {
76                 if ( selectors.size() != 0 )
77                     s = selectors.pop();
78             } catch (EmptyStackException JavaDoc ex){}
79                        
80             int attempts = 0;
81             try{
82                 while (s == null && attempts < 2) {
83                     selectors.wait(timeout);
84                     try {
85                         if ( selectors.size() != 0 )
86                             s = selectors.pop();
87                     } catch (EmptyStackException JavaDoc ex){
88                         break;
89                     }
90                     attempts++;
91                 }
92             } catch (InterruptedException JavaDoc ex){};
93             return s;
94         }
95     }
96
97
98     /**
99      * Return the <code>Selector</code> to the cache
100      */

101     public final static void returnSelector(Selector JavaDoc s) {
102         synchronized(selectors) {
103             selectors.push(s);
104             if (selectors.size() == 1)
105                 selectors.notify();
106         }
107     }
108
109 }
110
Popular Tags