KickJava   Java API By Example, From Geeks To Geeks.

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


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.jacorb.orb.*;
24
25 /**
26  * Implementation of COSEventChannelAdmin interface; ProxyPullConsumer.
27  * This defines connect_pull_supplier() and disconnect_pull_consumer().
28  *
29  * 2002/23/08 JFC OMG EventService Specification 1.1 page 2-7 states:
30  * "Registration is a two step process. An event-generating application
31  * first obtains a proxy consumer from a channel, then 'connects' to the
32  * proxy consumer by providing it with a supplier. ... The reason for
33  * the two step registration process..."
34  * Modifications to support the above have been made as well as to support
35  * section 2.1.5 "Disconnection Behavior" on page 2-4.
36  *
37  * @author Jeff Carlson
38  * @author Joerg v. Frantzius
39  * @author Rainer Lischetzki
40  * @author Gerald Brose
41  * $Id: ProxyPullConsumerImpl.java,v 1.10 2004/05/06 12:39:58 nicolas Exp $
42  */

43 public class ProxyPullConsumerImpl
44     extends org.omg.CosEventChannelAdmin.ProxyPullConsumerPOA
45     implements Runnable JavaDoc
46 {
47     private EventChannelImpl myEventChannel;
48     private org.omg.CosEventComm.PullSupplier myPullSupplier;
49     private org.omg.PortableServer.POA JavaDoc myPoa = null;
50     private boolean connected = false;
51
52     /**
53      * Constructor - gets called by the EventChannel
54      */

55     protected ProxyPullConsumerImpl ( EventChannelImpl ec,
56                                       org.omg.CORBA.ORB JavaDoc orb,
57                                       org.omg.PortableServer.POA JavaDoc poa )
58     {
59         myEventChannel = ec;
60         myPoa = poa;
61         connected = false;
62         _this_object( orb );
63     }
64
65     /**
66      * ProxyPullConsumer Interface:
67      * As stated by the EventService specification 1.1 section 2.3.6:
68      * "If a <b>ProxyPullSupplier</b> is already connected to a <b>PullConsumer</b>,
69      * then the <b>AlreadyConnected</b> exception is raised."
70      * and
71      * "Implementations shall raise the CORBA standard <b>BAD_PARAM</b> exception if
72      * a nil object reference is passed to the <b>connect_pull_supplier</b> operation.
73      * and
74      * "An implementation of a <b>ProxyPullConsumer</b> may put additional
75      * requirements on the interface supported by the pull supplier. If t he pull
76      * supplier does not meet those requirements, the <b>ProxyPullConsumer</b>
77      * raises the <b>TypeError</b> exception. (See section 2.5.2 on page 2-15
78      * for an example)"
79      */

80
81     public void connect_pull_supplier ( org.omg.CosEventComm.PullSupplier pullSupplier )
82         throws org.omg.CosEventChannelAdmin.AlreadyConnected,
83         org.omg.CosEventChannelAdmin.TypeError
84     {
85         if ( connected ) { throw new org.omg.CosEventChannelAdmin.AlreadyConnected(); }
86         if ( pullSupplier == null ) { throw new org.omg.CORBA.BAD_PARAM JavaDoc(); }
87
88         myPullSupplier = pullSupplier;
89         connected = true;
90         new Thread JavaDoc(this).start();
91     }
92
93     /**
94      * See EventService v 1.1 specification section 2.1.4.
95      * 'disconnect_pull_consumer terminates the event communication; it releases
96      * resources used at the consumer to support event communication. Calling
97      * this causes the implementation to call disconnect_pull_supplier operation
98      * on the corresponding PullSupplier interface (if that iterface is known).'
99      * See EventService v 1.1 specification section 2.1.5. This method should
100      * adhere to the spec as it a) causes a call to the corresponding disconnect
101      * on the connected supplier, b) 'If a consumer or supplier has received a
102      * disconnect call and subsequently receives another disconnect call, it
103      * shall raise a CORBA::OBJECT_NOT_EXIST exception.
104      */

105
106     public void disconnect_pull_consumer()
107     {
108
109         if ( connected )
110         {
111             if ( myPullSupplier != null )
112             {
113                 myPullSupplier.disconnect_pull_supplier();
114                 myPullSupplier = null;
115             }
116             connected = false;
117         }
118         else
119         {
120             throw new org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc();
121         }
122     }
123
124     /**
125      * Start being a good PullConsumer and ask for loads of events.
126      */

127
128     public void run()
129     {
130         org.omg.CORBA.BooleanHolder JavaDoc hasEvent = new org.omg.CORBA.BooleanHolder JavaDoc();
131         org.omg.CORBA.Any JavaDoc event = null;
132         while( connected )
133         {
134             synchronized(this)
135             {
136                 try
137                 {
138                     event = myPullSupplier.try_pull( hasEvent );
139                 }
140                 catch( org.omg.CORBA.UserException JavaDoc userEx )
141                 {
142                     connected = false;
143                     // userEx.printStackTrace();
144
return;
145                 }
146                 catch( org.omg.CORBA.SystemException JavaDoc sysEx )
147                 {
148                     connected = false;
149                     // sysEx.printStackTrace();
150
return;
151                 }
152
153                 if ( hasEvent.value )
154                 {
155                     myEventChannel.push_event( event );
156                 }
157                 // Let other threads get some time on the CPU in case we're
158
// in a cooperative environment.
159
Thread.yield();
160             }
161         }
162     }
163
164
165     /**
166      * Override this method from the Servant baseclass. Fintan Bolton
167      * in his book "Pure CORBA" suggests that you override this method to
168      * avoid the risk that a servant object (like this one) could be
169      * activated by the <b>wrong</b> POA object.
170      */

171     public org.omg.PortableServer.POA JavaDoc _default_POA()
172     {
173         return myPoa;
174     }
175 }
176
Popular Tags