KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > transport > EventHandlerBase


1 /*
2  * @(#)EventHandlerBase.java 1.9 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.transport;
9
10 import java.nio.channels.SelectionKey JavaDoc;
11
12 import org.omg.CORBA.INTERNAL JavaDoc;
13
14 import com.sun.corba.se.pept.transport.Acceptor;
15 import com.sun.corba.se.pept.transport.Connection;
16 import com.sun.corba.se.pept.transport.EventHandler;
17
18 import com.sun.corba.se.spi.orb.ORB;
19 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException;
20 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
21 import com.sun.corba.se.spi.orbutil.threadpool.Work;
22
23 import com.sun.corba.se.impl.orbutil.ORBUtility;
24
25 public abstract class EventHandlerBase
26     implements
27     EventHandler
28 {
29     protected ORB orb;
30     protected Work work;
31     protected boolean useWorkerThreadForEvent;
32     protected boolean useSelectThreadToWait;
33     protected SelectionKey JavaDoc selectionKey;
34
35     ////////////////////////////////////////////////////
36
//
37
// EventHandler methods
38
//
39

40     public void setUseSelectThreadToWait(boolean x)
41     {
42     useSelectThreadToWait = x;
43     }
44
45     public boolean shouldUseSelectThreadToWait()
46     {
47     return useSelectThreadToWait;
48     }
49
50     public void setSelectionKey(SelectionKey JavaDoc selectionKey)
51     {
52     this.selectionKey = selectionKey;
53     }
54
55     public SelectionKey JavaDoc getSelectionKey()
56     {
57     return selectionKey;
58     }
59
60     /*
61      * NOTE:
62      * This is not thread-safe by design.
63      * Only one thread should call it - a reader/listener/select thread.
64      * Not stateless: interest ops, registration.
65      */

66     public void handleEvent()
67     {
68     if (orb.transportDebugFlag) {
69         dprint(".handleEvent->: " + this);
70     }
71     getSelectionKey().interestOps(getSelectionKey().interestOps() &
72                       (~ getInterestOps()));
73     if (shouldUseWorkerThreadForEvent()) {
74         Throwable JavaDoc throwable = null;
75         try {
76         if (orb.transportDebugFlag) {
77             dprint(".handleEvent: addWork to pool: " + 0);
78         }
79         orb.getThreadPoolManager().getThreadPool(0)
80             .getWorkQueue(0).addWork(getWork());
81         } catch (NoSuchThreadPoolException e) {
82         throwable = e;
83         } catch (NoSuchWorkQueueException e) {
84         throwable = e;
85         }
86         // REVISIT: need to close connection.
87
if (throwable != null) {
88         if (orb.transportDebugFlag) {
89             dprint(".handleEvent: " + throwable);
90         }
91         INTERNAL JavaDoc i = new INTERNAL JavaDoc("NoSuchThreadPoolException");
92         i.initCause(throwable);
93         throw i;
94         }
95     } else {
96         if (orb.transportDebugFlag) {
97         dprint(".handleEvent: doWork");
98         }
99         getWork().doWork();
100     }
101     if (orb.transportDebugFlag) {
102         dprint(".handleEvent<-: " + this);
103     }
104     }
105
106     public boolean shouldUseWorkerThreadForEvent()
107     {
108     return useWorkerThreadForEvent;
109     }
110
111     public void setUseWorkerThreadForEvent(boolean x)
112     {
113     useWorkerThreadForEvent = x;
114     }
115
116     public void setWork(Work work)
117     {
118     this.work = work;
119     }
120
121     public Work getWork()
122     {
123     return work;
124     }
125
126     private void dprint(String JavaDoc msg)
127     {
128     ORBUtility.dprint("EventHandlerBase", msg);
129     }
130 }
131
132 // End of file.
133
Popular Tags