1 7 package com.sun.corba.se.spi.orb ; 8 9 import java.util.StringTokenizer ; 10 11 import java.lang.reflect.Array ; 12 13 import java.net.URL ; 14 import java.net.MalformedURLException ; 15 16 import com.sun.corba.se.spi.logging.CORBALogDomains ; 17 18 import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 19 import com.sun.corba.se.impl.orbutil.ORBClassLoader ; 20 import com.sun.corba.se.impl.orbutil.ObjectUtility ; 21 22 51 public abstract class OperationFactory { 52 private OperationFactory() {} 53 54 private static String getString( Object obj ) 55 { 56 if (obj instanceof String ) 57 return (String )obj ; 58 else 59 throw new Error ( "String expected" ) ; 60 } 61 62 private static Object [] getObjectArray( Object obj ) 63 { 64 if (obj instanceof Object []) 65 return (Object [])obj ; 66 else 67 throw new Error ( "Object[] expected" ) ; 68 } 69 70 private static StringPair getStringPair( Object obj ) 71 { 72 if (obj instanceof StringPair) 73 return (StringPair)obj ; 74 else 75 throw new Error ( "StringPair expected" ) ; 76 } 77 78 private static abstract class OperationBase implements Operation{ 79 public boolean equals( Object obj ) 80 { 81 if (this==obj) 82 return true ; 83 84 if (!(obj instanceof OperationBase)) 85 return false ; 86 87 OperationBase other = (OperationBase)obj ; 88 89 return toString().equals( other.toString() ) ; 90 } 91 92 public int hashCode() 93 { 94 return toString().hashCode() ; 95 } 96 } 97 98 private static class MaskErrorAction extends OperationBase 99 { 100 private Operation op ; 101 102 public MaskErrorAction( Operation op ) 103 { 104 this.op = op ; 105 } 106 107 public Object operate( Object arg ) 108 { 109 try { 110 return op.operate( arg ) ; 111 } catch (java.lang.Exception exc) { 112 return null ; 113 } 114 } 115 116 public String toString() 117 { 118 return "maskErrorAction(" + op + ")" ; 119 } 120 } 121 122 public static Operation maskErrorAction( Operation op ) 123 { 124 return new MaskErrorAction( op ) ; 125 } 126 127 private static class IndexAction extends OperationBase 128 { 129 private int index ; 130 131 public IndexAction( int index ) 132 { 133 this.index = index ; 134 } 135 136 public Object operate( Object value ) 137 { 138 return getObjectArray( value )[ index ] ; 139 } 140 141 public String toString() 142 { 143 return "indexAction(" + index + ")" ; 144 } 145 } 146 147 public static Operation indexAction( int index ) 148 { 149 return new IndexAction( index ) ; 150 } 151 152 private static class SuffixAction extends OperationBase 153 { 154 public Object operate( Object value ) 155 { 156 return getStringPair( value ).getFirst() ; 157 } 158 159 public String toString() { return "suffixAction" ; } 160 } 161 162 private static Operation suffixActionImpl = new SuffixAction() ; 163 164 private static class ValueAction extends OperationBase 165 { 166 public Object operate( Object value ) 167 { 168 return getStringPair( value ).getSecond() ; 169 } 170 171 public String toString() { return "valueAction" ; } 172 } 173 174 private static Operation valueActionImpl = new ValueAction() ; 175 176 private static class IdentityAction extends OperationBase 177 { 178 public Object operate( Object value ) 179 { 180 return value ; 181 } 182 183 public String toString() { return "identityAction" ; } 184 } 185 186 private static Operation identityActionImpl = new IdentityAction() ; 187 188 private static class BooleanAction extends OperationBase 189 { 190 public Object operate( Object value ) 191 { 192 return new Boolean ( getString( value ) ) ; 193 } 194 195 public String toString() { return "booleanAction" ; } 196 } 197 198 private static Operation booleanActionImpl = new BooleanAction() ; 199 200 private static class IntegerAction extends OperationBase 201 { 202 public Object operate( Object value ) 203 { 204 return new Integer ( getString( value ) ) ; 205 } 206 207 public String toString() { return "integerAction" ; } 208 } 209 210 private static Operation integerActionImpl = new IntegerAction() ; 211 212 private static class StringAction extends OperationBase 213 { 214 public Object operate( Object value ) 215 { 216 return value ; 217 } 218 219 public String toString() { return "stringAction" ; } 220 } 221 222 private static Operation stringActionImpl = new StringAction() ; 223 224 private static class ClassAction extends OperationBase 225 { 226 public Object operate( Object value ) 227 { 228 String className = getString( value ) ; 229 230 try { 231 Class result = ORBClassLoader.loadClass( className ) ; 232 return result ; 233 } catch (Exception exc) { 234 ORBUtilSystemException wrapper = ORBUtilSystemException.get( 235 CORBALogDomains.ORB_LIFECYCLE ) ; 236 throw wrapper.couldNotLoadClass( exc, className ) ; 237 } 238 } 239 240 public String toString() { return "classAction" ; } 241 } 242 243 private static Operation classActionImpl = new ClassAction() ; 244 245 private static class SetFlagAction extends OperationBase 246 { 247 public Object operate( Object value ) 248 { 249 return Boolean.TRUE ; 250 } 251 252 public String toString() { return "setFlagAction" ; } 253 } 254 255 private static Operation setFlagActionImpl = new SetFlagAction() ; 256 257 private static class URLAction extends OperationBase 258 { 259 public Object operate( Object value ) 260 { 261 String val = (String )value ; 262 try { 263 return new URL ( val ) ; 264 } catch (MalformedURLException exc) { 265 ORBUtilSystemException wrapper = ORBUtilSystemException.get( 266 CORBALogDomains.ORB_LIFECYCLE ) ; 267 throw wrapper.badUrl( exc, val ) ; 268 } 269 } 270 271 public String toString() { return "URLAction" ; } 272 } 273 274 private static Operation URLActionImpl = new URLAction() ; 275 276 public static Operation identityAction() 277 { 278 return identityActionImpl ; 279 } 280 281 public static Operation suffixAction() 282 { 283 return suffixActionImpl ; 284 } 285 286 public static Operation valueAction() 287 { 288 return valueActionImpl ; 289 } 290 291 public static Operation booleanAction() 292 { 293 return booleanActionImpl ; 294 } 295 296 public static Operation integerAction() 297 { 298 return integerActionImpl ; 299 } 300 301 public static Operation stringAction() 302 { 303 return stringActionImpl ; 304 } 305 306 public static Operation classAction() 307 { 308 return classActionImpl ; 309 } 310 311 public static Operation setFlagAction() 312 { 313 return setFlagActionImpl ; 314 } 315 316 public static Operation URLAction() 317 { 318 return URLActionImpl ; 319 } 320 321 private static class IntegerRangeAction extends OperationBase 322 { 323 private int min ; 324 private int max ; 325 326 IntegerRangeAction( int min, int max ) 327 { 328 this.min = min ; 329 this.max = max ; 330 } 331 332 public Object operate( Object value ) 333 { 334 int result = Integer.parseInt( getString( value ) ) ; 335 if ((result >= min) && (result <= max)) 336 return new Integer ( result ) ; 337 else 338 throw new IllegalArgumentException ( 339 "Property value " + result + " is not in the range " + 340 min + " to " + max ) ; 341 } 342 343 public String toString() { 344 return "integerRangeAction(" + min + "," + max + ")" ; 345 } 346 } 347 348 public static Operation integerRangeAction( int min, int max ) 349 { 350 return new IntegerRangeAction( min, max ) ; 351 } 352 353 private static class ListAction extends OperationBase { 354 private String sep ; 355 private Operation act ; 356 357 ListAction( String sep, Operation act ) 358 { 359 this.sep = sep ; 360 this.act = act ; 361 } 362 363 public Object operate( Object value ) 368 { 369 StringTokenizer st = new StringTokenizer ( getString( value ), 370 sep ) ; 371 int length = st.countTokens() ; 372 Object result = null ; 373 int ctr = 0 ; 374 while (st.hasMoreTokens()) { 375 String next = st.nextToken() ; 376 Object val = act.operate( next ) ; 377 if (result == null) 378 result = Array.newInstance( val.getClass(), length ) ; 379 Array.set( result, ctr++, val ) ; 380 } 381 382 return result ; 383 } 384 385 public String toString() { 386 return "listAction(separator=\"" + sep + 387 "\",action=" + act + ")" ; 388 } 389 } 390 391 public static Operation listAction( String sep, Operation act ) 392 { 393 return new ListAction( sep, act ) ; 394 } 395 396 private static class SequenceAction extends OperationBase 397 { 398 private String sep ; 399 private Operation[] actions ; 400 401 SequenceAction( String sep, Operation[] actions ) 402 { 403 this.sep = sep ; 404 this.actions = actions ; 405 } 406 407 public Object operate( Object value ) 408 { 409 StringTokenizer st = new StringTokenizer ( getString( value ), 410 sep ) ; 411 412 int numTokens = st.countTokens() ; 413 if (numTokens != actions.length) 414 throw new Error ( 415 "Number of tokens and number of actions do not match" ) ; 416 417 int ctr = 0 ; 418 Object [] result = new Object [ numTokens ] ; 419 while (st.hasMoreTokens()) { 420 Operation act = actions[ctr] ; 421 String next = st.nextToken() ; 422 result[ctr++] = act.operate( next ) ; 423 } 424 425 return result ; 426 } 427 428 public String toString() { 429 return "sequenceAction(separator=\"" + sep + 430 "\",actions=" + 431 ObjectUtility.compactObjectToString(actions) + ")" ; 432 } 433 } 434 435 public static Operation sequenceAction( String sep, 436 Operation[] actions ) 437 { 438 return new SequenceAction( sep, actions ) ; 439 } 440 441 private static class ComposeAction extends OperationBase 442 { 443 private Operation op1 ; 444 private Operation op2 ; 445 446 ComposeAction( Operation op1, Operation op2 ) 447 { 448 this.op1 = op1 ; 449 this.op2 = op2 ; 450 } 451 452 public Object operate( Object value ) 453 { 454 return op2.operate( op1.operate( value ) ) ; 455 } 456 457 public String toString() { 458 return "composition(" + op1 + "," + op2 + ")" ; 459 } 460 } 461 462 public static Operation compose( Operation op1, Operation op2 ) 463 { 464 return new ComposeAction( op1, op2 ) ; 465 } 466 467 private static class MapAction extends OperationBase 468 { 469 Operation op ; 470 471 MapAction( Operation op ) 472 { 473 this.op = op ; 474 } 475 476 public Object operate( Object value ) 477 { 478 Object [] values = (Object [])value ; 479 Object [] result = new Object [ values.length ] ; 480 for (int ctr=0; ctr<values.length; ctr++ ) 481 result[ctr] = op.operate( values[ctr] ) ; 482 return result ; 483 } 484 485 public String toString() { 486 return "mapAction(" + op + ")" ; 487 } 488 } 489 490 public static Operation mapAction( Operation op ) 491 { 492 return new MapAction( op ) ; 493 } 494 495 private static class MapSequenceAction extends OperationBase 496 { 497 private Operation[] op ; 498 499 public MapSequenceAction( Operation[] op ) 500 { 501 this.op = op ; 502 } 503 504 public Object operate( Object value ) 508 { 509 Object [] values = (Object [])value ; 510 Object [] result = new Object [ values.length ] ; 511 for (int ctr=0; ctr<values.length; ctr++ ) 512 result[ctr] = op[ctr].operate( values[ctr] ) ; 513 return result ; 514 } 515 516 public String toString() { 517 return "mapSequenceAction(" + 518 ObjectUtility.compactObjectToString(op) + ")" ; 519 } 520 } 521 522 public static Operation mapSequenceAction( Operation[] op ) 523 { 524 return new MapSequenceAction( op ) ; 525 } 526 527 private static class ConvertIntegerToShort extends OperationBase 528 { 529 public Object operate( Object value ) 530 { 531 Integer val = (Integer )value ; 532 return new Short ( val.shortValue() ) ; 533 } 534 535 public String toString() { 536 return "ConvertIntegerToShort" ; 537 } 538 } 539 540 private static Operation convertIntegerToShortImpl = new ConvertIntegerToShort() ; 541 542 public static Operation convertIntegerToShort() 543 { 544 return convertIntegerToShortImpl ; 545 } 546 } 547 | Popular Tags |