KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > etf > ListenerBase


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.orb.etf;
22
23 import java.util.*;
24
25 import org.omg.ETF.*;
26
27 import org.apache.avalon.framework.logger.*;
28 import org.apache.avalon.framework.configuration.*;
29
30 import org.jacorb.orb.*;
31
32 /**
33  * @author Andre Spiegel
34  * @version $Id: ListenerBase.java,v 1.1 2004/08/25 09:31:41 simon.mcqueen Exp $
35  */

36 public abstract class ListenerBase
37     extends _ListenerLocalBase
38     implements Configurable
39 {
40     /**
41     * The ORB.
42     */

43     protected ORB orb = null;
44     
45     /**
46     * The profile of this listener's endpoint.
47     */

48     protected Profile endpoint = null;
49     
50     /**
51     * The primary acceptor of this listener.
52     */

53     protected Acceptor acceptor = null;
54     
55     /**
56     * The configuration.
57     */

58     protected org.jacorb.config.Configuration configuration = null;
59     
60     /**
61     * The logger.
62     */

63     protected Logger logger = null;
64     
65     /**
66      * Reference to the ORB, for delivering
67      * incoming connections via upcalls.
68      */

69     protected org.omg.ETF.Handle up = null;
70
71     /**
72      * Queue of incoming connections, which will be
73      * delivered via calls to the accept() method.
74      * Connections will only be put into this list
75      * if no Handle has been set.
76      */

77     protected List incoming_connections = new ArrayList();
78
79     protected boolean terminated = false;
80
81     public ListenerBase()
82     {
83     }
84     
85     /**
86     * @deprecated
87     */

88     public ListenerBase(ORB orb)
89     {
90         this.orb = orb;
91     }
92     
93     public void configure(Configuration configuration)
94         throws ConfigurationException
95     {
96         this.configuration = (org.jacorb.config.Configuration)configuration;
97         
98         if (orb == null)
99         {
100             // c.f. with the constructor taking an ORB param.
101
this.orb = this.configuration.getORB();
102         }
103         
104         logger = this.configuration.getNamedLogger(this.configuration.getLoggerName(this.getClass()));
105     }
106     
107     /**
108      * This call establishes the link between the ORB (i.e. the Handle
109      * instance) and a server endpoint of the plugged-in transport.
110      * All calls upwards into the ORB shall use the given instance.
111      */

112     public void set_handle(Handle up)
113     {
114         this.up = up;
115     }
116     
117     /**
118      * It is possible that connection requests arrive <i>after</i> the
119      * initial creation of the Listener instance but <i>before</i> the
120      * conclusion of the configuration of the specific endpoint in this
121      * plugin. In order to provide a clear end of this configuration state,
122      * we added the listen() method. It is called by the ORB when it ready
123      * for incoming connection and thus signals the Listener instance to
124      * start processing the incoming connection requests. Therefore,
125      * a Listener instance shall not deliver incoming connections to the
126      * ORB before this method was called.
127      */

128     public void listen()
129     {
130         if (acceptor != null)
131             acceptor.start();
132     }
133
134     /**
135     * Method the Acceptor implementation should call to pass
136     * an opened connection to the ORB.
137     */

138     protected void deliverConnection(Connection connection)
139     {
140         if (up != null)
141         {
142             up.add_input(connection);
143         }
144         else
145         {
146             synchronized (incoming_connections)
147             {
148                 incoming_connections.add(connection);
149                 incoming_connections.notifyAll();
150             }
151         }
152     }
153
154     /**
155      * This call is an alternative to using set_handle() to initiate the
156      * callback-style of accepting new connections. This call blocks until
157      * a client connects to the server. Then a new Connection instance is
158      * returned. The transport plug-in must ensure that a thread blocked
159      * in accept() returns when destroy() is called with a null object
160      * reference. The transport plug-in must raise the CORBA::BAD_INV_ORDER
161      * with minor code {TBD} if the ORB calls this operation and set_handle()
162      * has ever been called previously on the same listener instance.
163      */

164     public Connection accept()
165     {
166         if (up != null)
167             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
168                 ("Must not call accept() when a Handle has been set");
169         else
170         {
171             synchronized (incoming_connections)
172             {
173                 while (!terminated &&
174                        incoming_connections.isEmpty())
175                 {
176                     try
177                     {
178                         incoming_connections.wait();
179                     }
180                     catch (InterruptedException JavaDoc ex)
181                     {
182                         // ignore
183
}
184                 }
185                 if (!terminated)
186                     return (Connection)incoming_connections.remove (0);
187                 else
188                     return null;
189             }
190         }
191     }
192     
193     /**
194      * The connection instance is returned to the Listener. It now shall
195      * signal any incoming data to the Handle.
196      */

197     public void completed_data (Connection conn)
198     {
199         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
200     }
201     
202     /**
203      * The Listener is instructed to close its endpoint. It shall no
204      * longer accept any connection requests and shall close all
205      * connections opened by it.
206      */

207     public void destroy()
208     {
209         if (acceptor != null)
210             acceptor.terminate();
211
212         this.terminated = true;
213         if (up == null)
214             incoming_connections.notifyAll();
215     }
216
217     
218     /**
219      * Returns a copy of the profile describing the endpoint
220      * of this instance.
221      */

222     public Profile endpoint()
223     {
224         return endpoint.copy();
225     }
226     
227     protected abstract class Acceptor
228         extends Thread JavaDoc
229     {
230         protected abstract void init();
231         
232         public abstract void run();
233         
234         public abstract void terminate();
235     }
236 }
237
Popular Tags