1 package org.jacorb.notification.servant; 2 3 22 23 import java.util.HashMap ; 24 import java.util.Map ; 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 ; 34 import org.omg.CORBA.Any ; 35 import org.omg.CORBA.InterfaceDef ; 36 import org.omg.CORBA.InterfaceDefHelper; 37 import org.omg.CORBA.NVList ; 38 import org.omg.CORBA.ORB ; 39 import org.omg.CORBA.OperationDescription ; 40 import org.omg.CORBA.Request ; 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 ; 51 import org.omg.PortableServer.Servant ; 52 53 57 58 public class TypedProxyPullConsumerImpl extends AbstractProxyConsumer implements 59 TypedProxyPullConsumerOperations, MessageSupplier, ITypedProxy 60 { 61 private String [] tryPullOperations_; 62 63 private TypedPullSupplier pullSupplier_; 64 65 private org.omg.CORBA.Object typedPullSupplier_; 66 67 private InterfaceDef interfaceDef_; 68 69 private final static Object [] STRING_ARRAY_TEMPLATE = new String [0]; 70 71 private final String expectedInterface_; 72 73 private final Map operationDescriptions_ = new HashMap (); 74 75 private final Map fullQualifiedOperationNames_ = new HashMap (); 76 77 79 public TypedProxyPullConsumerImpl(ITypedAdmin admin, SupplierAdmin supplierAdmin, ORB orb, 80 POA 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 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 (); 109 } 110 111 if (!typedPullSupplier_._is_a(expectedInterface_)) 112 { 113 throw new TypeError(); 114 } 115 } 116 117 private String [] 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 []) 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 getOperationDescription(String operation) 145 { 146 return (OperationDescription ) operationDescriptions_.get(operation); 147 } 148 149 private String getFullQualifiedName(String operation) 150 { 151 String _fullQualifiedName = (String ) 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 prepareRequest(String operation) 161 { 162 Request _request = typedPullSupplier_._request(operation); 163 164 NVList _args = _request.arguments(); 165 166 OperationDescription _operationDescription = getOperationDescription(operation); 167 168 for (int x = 0; x < _operationDescription.parameters.length; ++x) 169 { 170 Any _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 [] _tryPullOperations = getTryPullOperations(); 185 186 for (int x = 0; x < _tryPullOperations.length; ++x) 187 { 188 Request _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 _result = _request.result().value(); 200 201 boolean _success = _result.extract_boolean(); 202 203 if (_success) 204 { 205 String _operationNameWithoutTry = _tryPullOperations[x].substring(4); 206 String _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 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 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 getServant() 247 { 248 if (thisServant_ == null) 249 { 250 thisServant_ = new TypedProxyPullConsumerPOATie(this); 251 } 252 return thisServant_; 253 } 254 } | Popular Tags |