EventHandlerBase.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.transport;
27
28import java.nio.channels.SelectionKey;
29
30import org.omg.CORBA.INTERNAL;
31
32import com.sun.corba.se.pept.transport.Acceptor;
33import com.sun.corba.se.pept.transport.Connection;
34import com.sun.corba.se.pept.transport.EventHandler;
35
36import com.sun.corba.se.spi.orb.ORB;
37import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException;
38import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
39import com.sun.corba.se.spi.orbutil.threadpool.Work;
40
41import com.sun.corba.se.impl.orbutil.ORBUtility;
42
43public abstract class EventHandlerBase
44    implements
45        EventHandler
46{
47    protected ORB orb;
48    protected Work work;
49    protected boolean useWorkerThreadForEvent;
50    protected boolean useSelectThreadToWait;
51    protected SelectionKey selectionKey;
52
53    ////////////////////////////////////////////////////
54    //
55    // EventHandler methods
56    //
57
58    public void setUseSelectThreadToWait(boolean x)
59    {
60        useSelectThreadToWait = x;
61    }
62
63    public boolean shouldUseSelectThreadToWait()
64    {
65        return useSelectThreadToWait;
66    }
67
68    public void setSelectionKey(SelectionKey selectionKey)
69    {
70        this.selectionKey = selectionKey;
71    }
72
73    public SelectionKey getSelectionKey()
74    {
75        return selectionKey;
76    }
77
78    /*
79     * NOTE:
80     * This is not thread-safe by design.
81     * Only one thread should call it - a reader/listener/select thread.
82     * Not stateless: interest ops, registration.
83     */
84    public void handleEvent()
85    {
86        if (orb.transportDebugFlag) {
87            dprint(".handleEvent->: " + this);
88        }
89        getSelectionKey().interestOps(getSelectionKey().interestOps() &
90                                      (~ getInterestOps()));
91        if (shouldUseWorkerThreadForEvent()) {
92            Throwable throwable = null;
93            try {
94                if (orb.transportDebugFlag) {
95                    dprint(".handleEvent: addWork to pool: " + 0);
96                }
97                orb.getThreadPoolManager().getThreadPool(0)
98                    .getWorkQueue(0).addWork(getWork());
99            } catch (NoSuchThreadPoolException e) {
100                throwable = e;
101            } catch (NoSuchWorkQueueException e) {
102                throwable = e;
103            }
104            // REVISIT: need to close connection.
105            if (throwable != null) {
106                if (orb.transportDebugFlag) {
107                    dprint(".handleEvent: " + throwable);
108                }
109                INTERNAL i = new INTERNAL("NoSuchThreadPoolException");
110                i.initCause(throwable);
111                throw i;
112            }
113        } else {
114            if (orb.transportDebugFlag) {
115                dprint(".handleEvent: doWork");
116            }
117            getWork().doWork();
118        }
119        if (orb.transportDebugFlag) {
120            dprint(".handleEvent<-: " + this);
121        }
122    }
123
124    public boolean shouldUseWorkerThreadForEvent()
125    {
126        return useWorkerThreadForEvent;
127    }
128
129    public void setUseWorkerThreadForEvent(boolean x)
130    {
131        useWorkerThreadForEvent = x;
132    }
133
134    public void setWork(Work work)
135    {
136        this.work = work;
137    }
138
139    public Work getWork()
140    {
141        return work;
142    }
143
144    private void dprint(String msg)
145    {
146        ORBUtility.dprint("EventHandlerBase", msg);
147    }
148}
149
150// End of file.
151