KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ss > provider > ASSelectorProvider


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.server.ss.provider;
24
25 import java.nio.channels.spi.*;
26 import java.nio.channels.*;
27 import java.io.*;
28 import java.net.*;
29 import java.util.*;
30
31 /**
32  * Application Server's implementation of SelectorProvider,
33  * which wraps the JDK's default SelectorProvider obtained by
34  * sun.nio.ch.DefaultSelectorProvider.create().
35  * This wrapping is necessary to provide special implementations of
36  * ServerSocketChannel and Selector.
37  */

38 public class ASSelectorProvider extends SelectorProvider {
39
40     public static final int PORT_UNBOUND = 0;
41     public static final int PORT_BOUND = 1;
42     public static final int PORT_CONFLICT = 2;
43    
44     private SelectorProvider provider = null;
45
46     private HashMap<Integer JavaDoc, Integer JavaDoc> statemap =
47         new HashMap<Integer JavaDoc, Integer JavaDoc>();
48     private HashMap<Integer JavaDoc, ServerSocket> socketmap =
49         new HashMap<Integer JavaDoc, ServerSocket>();
50
51     public ASSelectorProvider() {
52         provider = getProvider();
53     }
54
55     public DatagramChannel openDatagramChannel() throws IOException {
56         return provider.openDatagramChannel();
57     }
58
59     public Pipe openPipe() throws IOException {
60         return provider.openPipe();
61     }
62
63     public ServerSocketChannel openServerSocketChannel() throws IOException {
64         ServerSocketChannel ssc = provider.openServerSocketChannel();
65         return new ASServerSocketChannel(ssc, this);
66     }
67
68     public SocketChannel openSocketChannel() throws IOException {
69         //SocketChannel sc = provider.openSocketChannel();
70
//return new ASSocketChannel(sc, this);
71
return provider.openSocketChannel();
72     }
73
74     public AbstractSelector openSelector() throws IOException {
75         AbstractSelector sel = provider.openSelector();
76         return new ASSelector(sel, this);
77     }
78
79
80     ServerSocket getServerSocket(int port) {
81         return socketmap.get(port);
82     }
83
84     int getPortState(int port) {
85         Integer JavaDoc i = statemap.get(port);
86         if (i == null) {
87             return PORT_UNBOUND;
88         } else {
89             return i.intValue();
90         }
91     }
92
93     public void setPortState(int port, int state) {
94         statemap.put(port, state);
95     }
96
97     public void clear(int port) {
98         statemap.remove(port);
99         socketmap.remove(port);
100     }
101
102
103     void setServerSocket(ServerSocket ss, int port) {
104         int state = getPortState(port);
105
106     /**
107         if (state < 3) {
108             state++;
109             statemap.put(s, new Integer(state));
110         }
111     **/

112
113         if (state == PORT_UNBOUND) {
114             socketmap.put(port, ss);
115             setPortState(port, PORT_BOUND);
116         }
117     else if (state == PORT_BOUND) {
118             setPortState(port, PORT_CONFLICT);
119     }
120     }
121
122     private SelectorProvider getProvider() {
123         SelectorProvider sp = null;
124         try {
125             Class JavaDoc clazz = Class.forName( "sun.nio.ch.DefaultSelectorProvider" );
126             java.lang.reflect.Method JavaDoc createMeth = clazz.getMethod("create", new Class JavaDoc[] {});
127             sp = (SelectorProvider) createMeth.invoke( null, new Object JavaDoc[] {} );
128         } catch( Exception JavaDoc e ) {
129         throw new RuntimeException JavaDoc("Unable to create default SelectorProvider.", e);
130     }
131
132     return sp;
133     }
134
135
136 }
137
138
Popular Tags