KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > events > EventChannelImpl


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

22
23 import org.omg.CosEventChannelAdmin.*;
24 import org.omg.CosEventComm.*;
25 import org.omg.CORBA.*;
26 import org.omg.CosNaming.*;
27 import org.omg.PortableServer.*;
28 import java.util.*;
29 import org.jacorb.orb.*;
30 import java.net.*;
31
32 /**
33  * Simple implementation of the event channel spec.
34  * The event channel acts as a factory for proxy push/pull consumers/suppliers
35  * and interacts with the implementation objects locally, i.e. using Java
36  * references only.
37  *
38  * @author Joerg v. Frantzius, Rainer Lischetzki, Gerald Brose, Jeff Carlson
39  * @version $Id: EventChannelImpl.java,v 1.10 2004/05/06 12:39:58 nicolas Exp $
40  */

41
42 public class EventChannelImpl extends JacORBEventChannelPOA
43 {
44   private Vector pullSuppliers = new Vector();
45   private Vector pullConsumers = new Vector();
46   private Vector pushSuppliers = new Vector();
47   private Vector pushConsumers = new Vector();
48   private Vector pendingEvents = new Vector();
49   private org.omg.CORBA.Any JavaDoc nullAny = null;
50
51   private org.omg.CORBA.ORB JavaDoc myOrb = null;
52   private org.omg.PortableServer.POA JavaDoc myPoa = null;
53
54
55   /**
56    * EventChannel constructor.
57    */

58   public EventChannelImpl(org.omg.CORBA.ORB JavaDoc orb, org.omg.PortableServer.POA JavaDoc poa)
59   {
60     myOrb = orb;
61     myPoa = poa;
62
63     _this_object(myOrb);
64     nullAny = myOrb.create_any();
65     nullAny.type(myOrb.get_primitive_tc( TCKind.tk_null));
66
67     try
68     {
69       this.myPoa = poa;
70       myPoa.the_POAManager().activate();
71     }
72     catch( Exception JavaDoc e )
73     {
74       e.printStackTrace();
75     }
76   }
77
78   /**
79    * send the ConsumerAdmin vectors off for destrcution.
80    */

81   private void consumerAdminDestroy()
82   {
83     releaseList( pullSuppliers );
84     releaseList( pushSuppliers );
85   }
86
87   /**
88    * send the SupplierAdmin vectors off for destrcution.
89    */

90   private void supplierAdminDestroy()
91   {
92     releaseList( pullConsumers );
93     releaseList( pushConsumers );
94   }
95
96   /**
97    * Iteratre a list and send the servant off to be destroyed.
98    */

99   private void releaseList( Vector list )
100   {
101     for ( Enumeration e = list.elements(); e.hasMoreElements(); )
102     {
103       org.omg.PortableServer.Servant JavaDoc servant =
104           (org.omg.PortableServer.Servant JavaDoc)e.nextElement();
105       releaseServant( servant );
106     }
107   }
108
109   /**
110    * Destroy / deactivate the servant.
111    */

112   private void releaseServant( org.omg.PortableServer.Servant JavaDoc servant )
113   {
114     try
115     {
116       servant._poa().deactivate_object( servant._object_id() );
117     }
118     catch (org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc wpEx)
119     {
120       wpEx.printStackTrace();
121     }
122     catch (org.omg.PortableServer.POAPackage.ObjectNotActive JavaDoc onaEx)
123     {
124       onaEx.printStackTrace();
125     }
126   }
127
128   /**
129    * Destroy all objects which are managed by the POA.
130    */

131   public void destroy()
132   {
133     consumerAdminDestroy();
134     supplierAdminDestroy();
135     releaseServant(this);
136   }
137
138
139   /**
140    * Return the consumerAdmin interface
141    */

142   public ConsumerAdmin for_consumers()
143   {
144     try
145     {
146       return ConsumerAdminHelper.narrow(myPoa.servant_to_reference(this));
147     }
148     catch( Exception JavaDoc e )
149     {
150       e.printStackTrace();
151       return null;
152     }
153   }
154
155   /**
156    * Return the supplierAdmin interface
157    */

158   public SupplierAdmin for_suppliers()
159   {
160     try
161     {
162       return SupplierAdminHelper.narrow(myPoa.servant_to_reference(this));
163     }
164     catch( Exception JavaDoc e )
165     {
166       e.printStackTrace();
167       return null;
168     }
169   }
170
171   /**
172    * Return a ProxyPullConsumer reference to be used to connect to a
173    * PullSupplier.
174    */

175   public ProxyPullConsumer obtain_pull_consumer()
176   {
177     synchronized( pullConsumers )
178     {
179       ProxyPullConsumerImpl p = new ProxyPullConsumerImpl( this, _orb(), myPoa );
180       pullConsumers.addElement( p );
181       return p._this( myOrb );
182     }
183   }
184
185   /**
186    * Return a ProxyPullSupplier reference to be used to connect to a
187    * PullConsumer.
188    */

189   public ProxyPullSupplier obtain_pull_supplier()
190   {
191     synchronized( pullSuppliers )
192     {
193       ProxyPullSupplierImpl p = new ProxyPullSupplierImpl ( this, _orb(), myPoa );
194       pullSuppliers.addElement( p );
195       return p._this( myOrb );
196     }
197   }
198
199   /**
200    * Return a ProxyPushConsumer reference to be used to connect to a
201    * PushSupplier.
202    */

203   public ProxyPushConsumer obtain_push_consumer()
204   {
205     synchronized( pushConsumers )
206     {
207       ProxyPushConsumerImpl p = new ProxyPushConsumerImpl( this, _orb(), myPoa );
208       pushConsumers.addElement( p );
209       return p._this( myOrb );
210     }
211   }
212
213   /**
214    * Return a ProxyPushSupplier reference to be used to connect to a
215    * PushConsumer.
216    */

217   public ProxyPushSupplier obtain_push_supplier()
218   {
219     synchronized( pushSuppliers )
220     {
221       ProxyPushSupplierImpl p = new ProxyPushSupplierImpl( this, _orb(), myPoa );
222       pushSuppliers.addElement( p );
223       return p._this( myOrb );
224     }
225   }
226
227
228   /**
229    * Send event to all registered consumers.
230    */

231   protected void push_event( org.omg.CORBA.Any JavaDoc event )
232   {
233     ProxyPushSupplierImpl push = null;
234     ProxyPullSupplierImpl pull = null;
235     synchronized( pushSuppliers )
236     {
237       for(int i = (pushSuppliers.size() - 1); i >= 0; --i )
238       {
239         push = (ProxyPushSupplierImpl) pushSuppliers.elementAt( i );
240         try
241         {
242             push.push_to_consumer( event );
243         }
244         catch( org.omg.CORBA.COMM_FAILURE JavaDoc comm )
245         {
246             pushSuppliers.removeElementAt( i );
247         }
248       }
249     }
250
251     synchronized( pullSuppliers )
252     {
253       for (int i = (pullSuppliers.size() - 1); i >= 0; --i )
254       {
255         pull = (ProxyPullSupplierImpl)pullSuppliers.elementAt( i );
256
257         try
258         {
259             pull.push_to_supplier( event );
260         }
261         catch( org.omg.CORBA.COMM_FAILURE JavaDoc comm )
262         {
263             pullSuppliers.removeElementAt( i );
264         }
265       }
266     }
267   }
268
269   static public void main( String JavaDoc[] args )
270   {
271     org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args, null);
272     try
273     {
274       org.omg.PortableServer.POA JavaDoc poa =
275           org.omg.PortableServer.POAHelper.narrow(
276               orb.resolve_initial_references("RootPOA"));
277
278       EventChannelImpl channel = new EventChannelImpl(orb,poa);
279
280       poa.the_POAManager().activate();
281
282       org.omg.CORBA.Object JavaDoc o = poa.servant_to_reference(channel);
283
284       NamingContextExt nc =
285           NamingContextExtHelper.narrow(
286               orb.resolve_initial_references("NameService"));
287
288       String JavaDoc channelName = ( args.length > 0 ? args[0] : "Generic.channel" );
289
290       nc.bind(nc.to_name( channelName ), o);
291       orb.run();
292     }
293     catch( Exception JavaDoc e )
294     {
295       e.printStackTrace();
296     }
297   }
298
299   /**
300    * Override this method from the Servant baseclass. Fintan Bolton
301    * in his book "Pure CORBA" suggests that you override this method to
302    * avoid the risk that a servant object (like this one) could be
303    * activated by the <b>wrong</b> POA object.
304    */

305   public org.omg.PortableServer.POA JavaDoc _default_POA()
306   {
307     return myPoa;
308   }
309 }
310
Popular Tags