1 package org.jacorb.ir; 2 3 22 23 import java.lang.reflect.*; 24 import java.util.*; 25 26 import org.omg.CORBA.ExceptionDefPOATie; 27 import org.omg.CORBA.INTF_REPOS ; 28 import org.omg.PortableServer.POA ; 29 30 import org.apache.avalon.framework.logger.Logger; 31 32 public class OperationDef 33 extends Contained 34 implements org.omg.CORBA.OperationDefOperations 35 { 36 private org.omg.CORBA.TypeCode result = null; 37 private org.omg.CORBA.IDLType result_def = null; 38 private org.omg.CORBA.ExceptionDef [] exceptions = null; 39 private org.omg.CORBA.ParameterDescription [] params = null; 40 41 private String [] contexts = new String [0]; 42 private org.omg.CORBA.OperationMode mode; 43 44 private Method method; 45 46 48 private String opInfo; 49 private String returnTypeName; 50 private String [] paramTypeNames = new String [0]; 51 52 private boolean defined = false; 53 54 private Logger logger; 55 private ClassLoader loader; 56 private POA poa; 57 58 public OperationDef( Method m, 59 Class def_in, 60 Class irHelper, 61 org.omg.CORBA.InterfaceDef i_def, 62 Logger logger, 63 ClassLoader loader, 64 POA poa) 65 { 66 this.logger = logger; 67 this.loader = loader; 68 this.poa = poa; 69 70 def_kind = org.omg.CORBA.DefinitionKind.dk_Operation; 71 name( m.getName()); 72 73 if (def_in == null) 74 { 75 throw new INTF_REPOS ("Class argument null"); 76 } 77 if (i_def == null) 78 { 79 throw new INTF_REPOS ("Idef argument null" ); 80 } 81 82 id( RepositoryID.toRepositoryID( 83 RepositoryID.className( i_def.id(), loader) + "/" + name(), 84 false, 85 loader)); 86 87 version(id().substring( id().lastIndexOf(':'))); 88 defined_in = i_def; 89 containing_repository = i_def.containing_repository(); 90 String className = def_in.getName(); 91 absolute_name = i_def.absolute_name() + "::" + name; 92 method = m; 93 94 if (this.logger.isDebugEnabled()) 95 { 96 this.logger.debug("New OperationDef, name: " + name + 97 " " + absolute_name); 98 } 99 100 Hashtable irInfo = null; 101 opInfo = null; 102 try 103 { 104 irInfo = (Hashtable)irHelper.getDeclaredField("irInfo").get(null); 105 opInfo = (String )irInfo.get( name() ); 106 } 107 catch( Exception e ) 108 { 109 logger.error("Caught Exception", e); 110 } 111 112 113 114 if( opInfo != null ) 115 { 116 if( opInfo.endsWith("-oneway")) 117 { 118 mode = org.omg.CORBA.OperationMode.OP_ONEWAY; 119 } 120 121 if( opInfo.indexOf("(") > 0 ) 122 returnTypeName = opInfo.substring(0,opInfo.indexOf("(")); 123 124 125 StringTokenizer strtok = 126 new StringTokenizer( opInfo.substring( opInfo.indexOf("(") + 1, 127 opInfo.lastIndexOf(")")), ","); 128 129 paramTypeNames = new String [strtok.countTokens()]; 130 for( int i = 0; i < paramTypeNames.length; i++ ) 131 { 132 String token = strtok.nextToken(); 133 134 paramTypeNames[i] = ( !token.equals(",") ? token : null ); 135 } 136 } 137 138 if( mode == null ) 139 { 140 mode = org.omg.CORBA.OperationMode.OP_NORMAL; 141 } 142 143 144 contexts = new String [0]; 145 } 146 147 void define() 148 { 149 try 150 { 151 result = 152 TypeCodeUtil.getTypeCode( method.getReturnType(), 153 this.loader, 154 null, 155 returnTypeName, 156 this.logger); 157 158 result_def = org.jacorb.ir.IDLType.create( result, 159 containing_repository, 160 this.logger, 161 this.poa); 162 } 163 catch( Exception e ) 164 { 165 logger.error("Caught Exception", e); 166 } 167 168 params = getParameterDescriptions(); 169 170 Class [] ex_classes = method.getExceptionTypes(); 171 Class uexc = null; 172 try 173 { 174 uexc = this.loader.loadClass("org.omg.CORBA.UserException"); 175 } 176 catch ( ClassNotFoundException e1) 177 { 178 throw new INTF_REPOS (ErrorMsg.IR_Definition_Not_Found, 179 org.omg.CORBA.CompletionStatus.COMPLETED_NO); 180 } 181 182 Vector v = new Vector(); 183 for( int ix = 0; ix < ex_classes.length; ix++ ) 184 { 185 if( uexc.isAssignableFrom(ex_classes[ix])) 186 { 187 try 188 { 189 ExceptionDef ex = new ExceptionDef( ex_classes[ ix ], 190 defined_in, 191 containing_repository, 192 this.loader, 193 this.poa, 194 this.logger); 195 org.omg.CORBA.ExceptionDef exRef = 196 org.omg.CORBA.ExceptionDefHelper.narrow( 197 this.poa.servant_to_reference( 198 new ExceptionDefPOATie ( ex ) 199 ) 200 ); 201 202 v.addElement( exRef ); 203 ex.setReference( exRef ); 204 } 205 catch( Exception e ) 206 { 207 logger.error("Caught Exception", e); 208 } 209 } 210 } 211 212 exceptions = new org.omg.CORBA.ExceptionDef [ v.size() ]; 213 v.copyInto( exceptions ); 214 215 defined = true; 216 } 217 218 219 org.omg.CORBA.ParameterDescription [] getParameterDescriptions() 220 { 221 org.omg.CORBA.TypeCode tc = null; 222 Class m_params[] = method.getParameterTypes(); 223 224 org.omg.CORBA.ParameterDescription [] params = 225 new org.omg.CORBA.ParameterDescription [m_params.length]; 226 227 if( paramTypeNames.length > 0 ) 228 { 229 if (paramTypeNames.length != m_params.length) 230 { 231 throw new INTF_REPOS ("Different parameter type numbers! " + 232 paramTypeNames.length + " vs. " + m_params.length + 233 " inforString: " + opInfo); 234 } 235 } 236 237 238 for( int i = 0; i < params.length; i++) 239 { 240 String name = "arg_" + i; 241 String paramInfo = null; 242 org.omg.CORBA.ParameterMode mode = null; 243 try 244 { 245 String parameterTypeName = m_params[i].getName(); 246 247 if( paramTypeNames.length != 0 ) 248 paramInfo = paramTypeNames[i]; 249 250 if( ! parameterTypeName.endsWith("Holder") ) 251 { 252 mode = org.omg.CORBA.ParameterMode.PARAM_IN; 253 if( paramInfo != null && paramInfo.indexOf(' ') > 0 ) 254 { 255 parameterTypeName = 256 paramInfo.substring( paramInfo.indexOf(' ')+1); 257 name = 258 paramInfo.substring( paramInfo.indexOf(':')+1, 259 paramInfo.indexOf(' ')); 260 } 261 } 262 else 263 { 264 if ( ! (paramInfo != null && (paramInfo.indexOf(' ') > 0))) 265 { 266 throw new INTF_REPOS ("No param info for " + parameterTypeName); 267 } 268 269 if( paramInfo.substring(0, (paramInfo.indexOf(' ')-1)).startsWith("inout:")) 270 mode = org.omg.CORBA.ParameterMode.PARAM_INOUT; 271 else 272 mode = org.omg.CORBA.ParameterMode.PARAM_OUT; 273 274 name = paramInfo.substring( paramInfo.indexOf(':')+1, 275 paramInfo.indexOf(' ')); 276 277 parameterTypeName = 278 parameterTypeName.substring(0, parameterTypeName.indexOf("Holder")); 279 } 280 281 282 if (this.logger.isDebugEnabled()) 283 { 284 this.logger.debug("Operation " + name() + ", param #"+ i + 285 "name: " + name + 286 ", paramTypeName " + parameterTypeName + 287 paramInfo); 288 } 289 290 tc = TypeCodeUtil.getTypeCode( m_params[i], 291 this.loader, 292 null, 293 parameterTypeName, 294 this.logger); 295 } 296 catch ( Exception e ) 297 { 298 logger.error("Caught Exception", e); 299 throw new INTF_REPOS ( ErrorMsg.IR_Definition_Not_Found, 300 org.omg.CORBA.CompletionStatus.COMPLETED_NO); 301 } 302 org.omg.CORBA.IDLType type_def = 303 IDLType.create( tc, containing_repository, 304 this.logger, this.poa ); 305 params[i] = 306 new org.omg.CORBA.ParameterDescription ( name, tc, type_def, mode); 307 } 308 return params; 309 } 310 311 public org.omg.CORBA.IDLType result_def() 312 { 313 if ( ! defined) 314 { 315 throw new INTF_REPOS ("OperationDef undefined"); 316 } 317 if (result_def == null) 318 { 319 throw new INTF_REPOS ("Result def for op " + name () + " null"); 320 } 321 return result_def; 322 } 323 324 public void result_def(org.omg.CORBA.IDLType a) 325 { 326 result_def = a; 327 } 328 329 public org.omg.CORBA.OperationMode mode() 330 { 331 return mode; 332 } 333 334 public void mode( org.omg.CORBA.OperationMode a) 335 { 336 mode = a; 337 } 338 339 public org.omg.CORBA.TypeCode result() 340 { 341 if ( ! defined) 342 { 343 throw new INTF_REPOS ("OperationDeg undefined"); 344 } 345 346 return result; 347 } 348 349 public org.omg.CORBA.ParameterDescription [] params() 350 { 351 if( !defined ) 352 define(); 353 return params; 354 } 355 356 public void params(org.omg.CORBA.ParameterDescription [] a) 357 { 358 params = a; 359 } 360 361 public java.lang.String [] contexts() 362 { 363 if( !defined ) 364 define(); 365 return contexts; 366 } 367 368 public void contexts(java.lang.String [] a) 369 { 370 contexts = a; 371 } 372 373 public org.omg.CORBA.ExceptionDef [] exceptions() 374 { 375 if( !defined ) 376 define(); 377 return exceptions; 378 } 379 380 public void exceptions(org.omg.CORBA.ExceptionDef [] a) 381 { 382 exceptions = a; 383 } 384 385 public org.omg.CORBA.OperationDescription describe_operation() 386 { 387 if( !defined ) 388 define(); 389 390 org.omg.CORBA.ExceptionDescription ex_des[] = 391 new org.omg.CORBA.ExceptionDescription [exceptions.length]; 392 393 for( int i = 0; i < exceptions.length; i++ ) 394 { 395 org.omg.CORBA.ContainedPackage.Description cd = exceptions[i].describe(); 396 397 if( cd.kind != org.omg.CORBA.DefinitionKind.dk_Exception ) 398 { 399 throw new INTF_REPOS ( ErrorMsg.IR_Unexpected_Definition_Kind, 400 org.omg.CORBA.CompletionStatus.COMPLETED_NO); 401 } 402 ex_des[i] = org.omg.CORBA.ExceptionDescriptionHelper.extract( cd.value ); 403 } 404 return new org.omg.CORBA.OperationDescription (name, 405 id, 406 org.omg.CORBA.ContainedHelper.narrow(defined_in).id(), 407 version, 408 result, mode, contexts, params, ex_des); 409 } 410 411 413 public void destroy() 414 { 415 throw new INTF_REPOS ( 416 ErrorMsg.IR_Not_Implemented, 417 org.omg.CORBA.CompletionStatus.COMPLETED_NO); 418 } 419 420 421 423 public org.omg.CORBA.ContainedPackage.Description describe() 424 { 425 if ( ! defined) 426 { 427 throw new INTF_REPOS ("OperationDeg undefined"); 428 } 429 430 431 org.omg.CORBA.Any a = orb.create_any(); 432 org.omg.CORBA.OperationDescriptionHelper.insert( a, describe_operation() ); 433 return new org.omg.CORBA.ContainedPackage.Description( org.omg.CORBA.DefinitionKind.dk_Operation, a); 434 } 435 436 437 } 438 | Popular Tags |