KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > servant > TypedProxyPullConsumerImpl


1 package org.jacorb.notification.servant;
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 java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.jacorb.notification.MessageFactory;
28 import org.jacorb.notification.OfferManager;
29 import org.jacorb.notification.SubscriptionManager;
30 import org.jacorb.notification.engine.TaskProcessor;
31 import org.jacorb.notification.interfaces.Message;
32 import org.jacorb.notification.interfaces.MessageSupplier;
33 import org.omg.CORBA.ARG_OUT JavaDoc;
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.InterfaceDef JavaDoc;
36 import org.omg.CORBA.InterfaceDefHelper;
37 import org.omg.CORBA.NVList JavaDoc;
38 import org.omg.CORBA.ORB JavaDoc;
39 import org.omg.CORBA.OperationDescription JavaDoc;
40 import org.omg.CORBA.Request JavaDoc;
41 import org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription;
42 import org.omg.CosEventChannelAdmin.AlreadyConnected;
43 import org.omg.CosEventChannelAdmin.TypeError;
44 import org.omg.CosNotifyChannelAdmin.ProxyType;
45 import org.omg.CosNotifyChannelAdmin.SupplierAdmin;
46 import org.omg.CosTypedEventComm.TypedPullSupplier;
47 import org.omg.CosTypedNotifyChannelAdmin.TypedProxyPullConsumerHelper;
48 import org.omg.CosTypedNotifyChannelAdmin.TypedProxyPullConsumerOperations;
49 import org.omg.CosTypedNotifyChannelAdmin.TypedProxyPullConsumerPOATie;
50 import org.omg.PortableServer.POA JavaDoc;
51 import org.omg.PortableServer.Servant JavaDoc;
52
53 /**
54  * @Author Alphonse Bendt
55  * @version $Id: TypedProxyPullConsumerImpl.java,v 1.9 2005/05/04 17:32:29 alphonse.bendt Exp $
56  */

57
58 public class TypedProxyPullConsumerImpl extends AbstractProxyConsumer implements
59         TypedProxyPullConsumerOperations, MessageSupplier, ITypedProxy
60 {
61     private String JavaDoc[] tryPullOperations_;
62
63     private TypedPullSupplier pullSupplier_;
64
65     private org.omg.CORBA.Object JavaDoc typedPullSupplier_;
66
67     private InterfaceDef JavaDoc interfaceDef_;
68
69     private final static Object JavaDoc[] STRING_ARRAY_TEMPLATE = new String JavaDoc[0];
70
71     private final String JavaDoc expectedInterface_;
72
73     private final Map JavaDoc operationDescriptions_ = new HashMap JavaDoc();
74
75     private final Map JavaDoc fullQualifiedOperationNames_ = new HashMap JavaDoc();
76
77     // ////////////////////////////
78

79     public TypedProxyPullConsumerImpl(ITypedAdmin admin, SupplierAdmin supplierAdmin, ORB JavaDoc orb,
80             POA JavaDoc poa, Configuration conf, TaskProcessor taskProcessor, MessageFactory messageFactory,
81             OfferManager offerManager, SubscriptionManager subscriptionManager)
82     {
83         super(admin, orb, poa, conf, taskProcessor, messageFactory, supplierAdmin, offerManager,
84                 subscriptionManager);
85
86         expectedInterface_ = admin.getSupportedInterface();
87     }
88
89     // ////////////////////////////
90

91     public void connect_typed_pull_supplier(TypedPullSupplier typedPullSupplier)
92             throws AlreadyConnected, TypeError
93     {
94         logger_.info("connect typed_pull_supplier");
95
96         checkIsNotConnected();
97
98         connectClient(typedPullSupplier);
99
100         pullSupplier_ = typedPullSupplier;
101
102         typedPullSupplier_ = pullSupplier_.get_typed_supplier();
103
104         interfaceDef_ = InterfaceDefHelper.narrow(typedPullSupplier_._get_interface_def());
105
106         if (interfaceDef_ == null)
107         {
108             throw new NullPointerException JavaDoc();
109         }
110
111         if (!typedPullSupplier_._is_a(expectedInterface_))
112         {
113             throw new TypeError();
114         }
115     }
116
117     private String JavaDoc[] getTryPullOperations()
118     {
119         if (tryPullOperations_ == null)
120         {
121             FullInterfaceDescription _fullIfDescription = interfaceDef_.describe_interface();
122
123             for (int x = 0; x < _fullIfDescription.operations.length; ++x)
124             {
125                 if (_fullIfDescription.operations[x].name.startsWith("try_"))
126                 {
127                     operationDescriptions_.put(_fullIfDescription.operations[x].name,
128                             _fullIfDescription.operations[x]);
129                 }
130             }
131             
132             tryPullOperations_ = (String JavaDoc[]) operationDescriptions_.keySet().toArray(
133                     STRING_ARRAY_TEMPLATE);
134         }
135         
136         return tryPullOperations_;
137     }
138
139     public void runPullMessage()
140     {
141         runPullMessageInternal();
142     }
143
144     private OperationDescription JavaDoc getOperationDescription(String JavaDoc operation)
145     {
146         return (OperationDescription JavaDoc) operationDescriptions_.get(operation);
147     }
148
149     private String JavaDoc getFullQualifiedName(String JavaDoc operation)
150     {
151         String JavaDoc _fullQualifiedName = (String JavaDoc) fullQualifiedOperationNames_.get(operation);
152         if (_fullQualifiedName == null)
153         {
154             _fullQualifiedName = interfaceDef_.lookup(operation).absolute_name();
155             fullQualifiedOperationNames_.put(operation, _fullQualifiedName);
156         }
157         return _fullQualifiedName;
158     }
159
160     private Request JavaDoc prepareRequest(String JavaDoc operation)
161     {
162         Request JavaDoc _request = typedPullSupplier_._request(operation);
163
164         NVList JavaDoc _args = _request.arguments();
165
166         OperationDescription JavaDoc _operationDescription = getOperationDescription(operation);
167
168         for (int x = 0; x < _operationDescription.parameters.length; ++x)
169         {
170             Any JavaDoc _any = getORB().create_any();
171
172             _any.type(_operationDescription.parameters[x].type);
173
174             _args.add_value(_operationDescription.parameters[x].name, _any, ARG_OUT.value);
175         }
176
177         _request.set_return_type(_operationDescription.result);
178
179         return _request;
180     }
181
182     private void runPullMessageInternal()
183     {
184         String JavaDoc[] _tryPullOperations = getTryPullOperations();
185
186         for (int x = 0; x < _tryPullOperations.length; ++x)
187         {
188             Request JavaDoc _request = prepareRequest(_tryPullOperations[x]);
189
190             if (logger_.isDebugEnabled())
191             {
192                 logger_.debug("invoke " + _tryPullOperations[x]);
193             }
194
195             try
196             {
197                 _request.invoke();
198
199                 Any JavaDoc _result = _request.result().value();
200
201                 boolean _success = _result.extract_boolean();
202
203                 if (_success)
204                 {
205                     String JavaDoc _operationNameWithoutTry = _tryPullOperations[x].substring(4);
206                     String JavaDoc _operationName = getFullQualifiedName(_operationNameWithoutTry);
207
208                     Message _mesg = getMessageFactory().newMessage(expectedInterface_,
209                             _operationName, _request.arguments(), this);
210
211                     checkMessageProperties(_mesg);
212
213                     processMessage(_mesg);
214                 }
215             } catch (Exception JavaDoc e)
216             {
217                 logger_.error("An error occured while invoking " + _tryPullOperations[x], e);
218             }
219         }
220     }
221
222     public void disconnect_pull_consumer()
223     {
224         destroy();
225     }
226
227     public ProxyType MyType()
228     {
229         return ProxyType.PULL_TYPED;
230     }
231
232     public org.omg.CORBA.Object JavaDoc activate()
233     {
234         return TypedProxyPullConsumerHelper.narrow(getServant()._this_object(getORB()));
235     }
236
237     public void disconnectClient()
238     {
239         if (pullSupplier_ != null)
240         {
241             pullSupplier_.disconnect_pull_supplier();
242             pullSupplier_ = null;
243         }
244     }
245
246     public Servant JavaDoc getServant()
247     {
248         if (thisServant_ == null)
249         {
250             thisServant_ = new TypedProxyPullConsumerPOATie(this);
251         }
252         return thisServant_;
253     }
254 }
Popular Tags