KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
26 import java.nio.channels.ClosedChannelException JavaDoc;
27 import java.nio.channels.SelectableChannel JavaDoc;
28 import java.nio.channels.SelectionKey JavaDoc;
29 import java.nio.channels.Selector JavaDoc;
30 import java.nio.channels.ServerSocketChannel JavaDoc;
31 import java.nio.channels.spi.AbstractSelectableChannel JavaDoc;
32 import java.nio.channels.spi.AbstractSelector JavaDoc;
33 import java.nio.channels.spi.SelectorProvider JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 import com.sun.enterprise.server.ss.spi.ASSocketFacadeUtils;
38 import com.sun.enterprise.server.ss.util.ASSet;
39 import com.sun.enterprise.server.ss.util.ASWrapperCreator;
40 import com.sun.logging.LogDomains;
41
42 /**
43  * Selector implementation of the quick appserver startup implementation.
44  * JDK's NIO implementation only recognises its own channel implementation.
45  * It doesnt accept any other implementation for select registrations.
46  * This implementation make sure that JDK's NIO implementation gets only
47  * the JDK's channels.
48  */

49 class ASSelector extends AbstractSelector JavaDoc implements ASWrapperCreator{
50     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
51     private AbstractSelector JavaDoc sel = null;
52
53     private boolean wakenup = false;
54     private boolean wrapSelectionKeys = false;
55     private int port = 0;
56
57     ASSelector(AbstractSelector JavaDoc sel, SelectorProvider JavaDoc provider) {
58         super(provider);
59         this.sel = sel;
60     }
61
62     protected void implCloseSelector() throws IOException JavaDoc {
63         if ( logger.isLoggable(Level.FINER) ) {
64             logger.log(Level.FINER, "Selector is getting closed :" + sel);
65         }
66         sel.close();
67     }
68
69     protected SelectionKey JavaDoc register(AbstractSelectableChannel JavaDoc ac,
70                     int ops, Object JavaDoc obj) {
71         try {
72             if (ac instanceof ASChannel) {
73                 ASChannel asc = (ASChannel) ac;
74                 SelectableChannel JavaDoc sc = asc.getActualChannel();
75
76                 if (ac instanceof ASServerSocketChannel &&
77                     ops == SelectionKey.OP_ACCEPT) {
78                     this.port = ((ASServerSocketChannel)ac).getPortNumber();
79                     wrapSelectionKeys = true;
80                 }
81                 return sc.register(sel, ops, obj);
82             } else {
83                 if ( logger.isLoggable(Level.FINEST) ) {
84                 logger.log(java.util.logging.Level.FINEST,
85                     "In ASSelector.register channel class = "
86                     + ac.getClass().getName(), new Exception JavaDoc());
87                 }
88                 return ac.register(sel, ops, obj);
89             }
90         } catch (ClosedChannelException JavaDoc ce) {
91             throw new RuntimeException JavaDoc (ce);
92         }
93     }
94
95     public java.util.Set JavaDoc keys() {
96         if (wrapSelectionKeys &&
97             ASSocketFacadeUtils.getASSocketService().isServerStartingUp(port)) {
98             return new ASSet(sel.keys(), this);
99         } else {
100             return sel.keys();
101         }
102     }
103
104     public java.util.Set JavaDoc selectedKeys() {
105         if (wrapSelectionKeys &&
106             ASSocketFacadeUtils.getASSocketService().isServerStartingUp(port)) {
107             return new ASSet(sel.selectedKeys(), this);
108         } else {
109             return sel.selectedKeys();
110         }
111  
112     }
113
114     public Selector JavaDoc wakeup() {
115         wakenup = true;
116         return sel.wakeup();
117     }
118
119     public int select(long l) throws IOException JavaDoc {
120         return sel.select(l);
121     }
122
123     public int selectNow() throws IOException JavaDoc {
124         return sel.selectNow();
125     }
126
127     public int select() throws IOException JavaDoc {
128         return sel.select();
129     }
130
131     boolean wakenUp() {
132         boolean result = wakenup;
133         wakenup = false;
134         return result;
135     }
136
137     public Object JavaDoc wrapIfNecessary(Object JavaDoc next) {
138        SelectionKey JavaDoc selKey = (SelectionKey JavaDoc) next;
139         if ((selKey.interestOps() & SelectionKey.OP_ACCEPT) != 0) {
140             ASServerSocketChannel channel = new ASServerSocketChannel(
141             (ServerSocketChannel JavaDoc) selKey.channel(), this.sel.provider());
142             SelectionKey JavaDoc key = new ASSelectionKey(channel, selKey, this);
143             key.attach(selKey.attachment());
144             return key;
145         } else {
146             return selKey;
147         }
148     }
149
150 }
151
Popular Tags