KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > IoHandlerContext


1 // $Id: IoHandlerBase.java 1315 2007-06-10 08:05:00Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.util.concurrent.Executor JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import org.xsocket.ILifeCycle;
29 import org.xsocket.Synchronized;
30 import org.xsocket.Synchronized.Mode;
31 import org.xsocket.stream.io.spi.IIoHandlerContext;
32
33
34
35 /**
36  *
37  * @author grro@xsocket.org
38  */

39 final class IoHandlerContext implements IIoHandlerContext {
40     
41     private static final Logger JavaDoc LOG = Logger.getLogger(IoHandlerContext.class.getName());
42     
43     private static final SingleThreadedWorkerPool SINGLE_THREADED_POOL = new SingleThreadedWorkerPool();
44     
45     private boolean isConnectHandler = false;
46     private boolean isDisconnectHandler = false;
47     private boolean isDataHandler = false;
48     private boolean isTimeoutHandler = false;
49     private boolean isLifeCycleHandler = false;
50     private boolean isConnectionScoped = false;
51     private boolean isHandlerThreadSave = false;
52     private boolean isMultithreaded = false;
53
54     private Executor JavaDoc workerpool = null;
55     
56
57     /**
58      * constructor
59      *
60      * @param appHandler the app handler
61      */

62     IoHandlerContext(IHandler appHandler, Executor JavaDoc workerpool) {
63         updateAppHandler(appHandler);
64         updateWorkerpool(workerpool);
65     }
66     
67     
68     void updateAppHandler(IHandler appHandler) {
69         introspectHandler(appHandler);
70     }
71     
72     
73     void updateWorkerpool(Executor JavaDoc workerpool) {
74         if (workerpool != null) {
75             this.workerpool = workerpool;
76             isMultithreaded = true;
77         } else {
78             this.workerpool = SINGLE_THREADED_POOL;
79             isMultithreaded = false;
80         }
81     }
82     
83     
84     public Executor JavaDoc getWorkerpool() {
85         return workerpool;
86     }
87     
88     public boolean isAppHandlerListenForConnectEvent() {
89         return isConnectHandler;
90     }
91     
92     public boolean isAppHandlerListenForDataEvent() {
93         return isDataHandler;
94     }
95     
96     public boolean isAppHandlerListenforDisconnectEvent() {
97         return isDisconnectHandler;
98     }
99     
100     public boolean isAppHandlerListenForTimeoutEvent() {
101         return isTimeoutHandler;
102     }
103
104     public boolean isAppHandlerConnectionScoped() {
105         return isConnectionScoped;
106     }
107     
108     public boolean isAppHandlerThreadSave() {
109         return isHandlerThreadSave;
110     }
111     
112     boolean isConnectionScoped() {
113         return isConnectionScoped;
114     }
115
116     boolean isLifeCycleHandler() {
117         return isLifeCycleHandler;
118     }
119     
120     public boolean isAppHandlerThreadSafe() {
121         return isHandlerThreadSave;
122     }
123
124     
125     public boolean isMultithreaded() {
126         return isMultithreaded;
127     }
128     
129         
130     private void introspectHandler(IHandler appHandler) {
131         
132         if (appHandler == null) {
133             isDataHandler = true;
134             isConnectHandler = false;
135             isDisconnectHandler = false;
136             isTimeoutHandler = false;
137             isConnectionScoped = false;
138             isHandlerThreadSave = false;
139             isLifeCycleHandler = false;
140             
141         } else {
142             
143             isConnectHandler = (appHandler instanceof IConnectHandler);
144             isDisconnectHandler = (appHandler instanceof IDisconnectHandler);
145             isDataHandler = (appHandler instanceof IDataHandler);
146             isTimeoutHandler = (appHandler instanceof ITimeoutHandler);
147             isConnectionScoped = (appHandler instanceof IConnectionScoped);
148             isLifeCycleHandler = (appHandler instanceof ILifeCycle);
149             
150             Synchronized sync = appHandler.getClass().getAnnotation(Synchronized.class);
151             if (sync != null) {
152                 Mode scope = sync.value();
153                 isHandlerThreadSave = (scope == Synchronized.Mode.OFF);
154             } else {
155                 isHandlerThreadSave = false;
156             }
157         }
158     }
159     
160     
161     private static final class SingleThreadedWorkerPool implements Executor JavaDoc {
162         public void execute(Runnable JavaDoc command) {
163             try {
164                 
165                 command.run();
166                 
167             } catch (Exception JavaDoc e) {
168                 if (LOG.isLoggable(Level.FINE)) {
169                     LOG.fine("error occured within worker thread " + e.toString());
170                 }
171             }
172             
173         }
174     }
175 }
176
Popular Tags