KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > orb > OperationFactory


1 /*
2  * @(#)OperationFactory.java 1.24 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.corba.se.spi.orb ;
8
9 import java.util.StringTokenizer JavaDoc ;
10
11 import java.lang.reflect.Array JavaDoc ;
12
13 import java.net.URL JavaDoc ;
14 import java.net.MalformedURLException JavaDoc ;
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 /** This is a static factory class for commonly used operations
23 * for property parsing. The following operations are supported:
24 * <ul>
25 * <li>maskErrorAction( Operation op ) executes op and returns the result. If op throws an
26 * exception, the result is null.
27 * <li>indexAction( int arg ) returns the [arg] element of value, which must be an Object[]</li>
28 * <li>identityAction() return the value</li>
29 * <li>booleanAction() return a Boolean representing true or false values of the String value</li>
30 * <li>integerAction() returns an Integer for the String value, which must be a decimal integer</li>
31 * <li>stringAction() returns the String value</li>
32 * <li>classAction() returns a class for the String value, as loaded by the ORB classloader</li>
33 * <li>setFlagAction() always return Boolean.TRUE</li>
34 * <li>URLAction() returns a java.net.URL for the String value, which must be a valid URL</li>
35 * <li>integerRangeAction( int min, int max ) returns an Integer for the String value, which must be a
36 * decimal integer in the range min to max inclusive</li>
37 * <li>listAction( String sep, Operation ) tokenizes the String value with sep as separator, then
38 * applies the Operation to each token, and returns an array of the result</li>
39 * <li>sequenceAction( String, Operation[] ) tokenizes the String value with sep as separator, then
40 * applies each Operation in the Operation array to successive tokens, and returns an array of the results</li>
41 * <li>compose( Operation op1, Operation op2 ) is the operation that applies op2 to the result of applying
42 * op1 to the value</li>
43 * <li>mapAction( Operation ) applies the Operation to each element of an array of objects, and returns
44 * an array of the results</li>
45 * <li>mapSequenceAction( Operation[] ) applies the corresponding element of the Operation array to an
46 * element of the Object[] value, and returns an array of the results</li>
47 * <li>convertIntegerToShort coerces an Integer into a Short.</li>
48 * </ul>
49 * Other operations can be directly defined, and combined using these basic operations.
50 */

51 public abstract class OperationFactory {
52     private OperationFactory() {}
53
54     private static String JavaDoc getString( Object JavaDoc obj )
55     {
56     if (obj instanceof String JavaDoc)
57         return (String JavaDoc)obj ;
58     else
59         throw new Error JavaDoc( "String expected" ) ;
60     }
61
62     private static Object JavaDoc[] getObjectArray( Object JavaDoc obj )
63     {
64     if (obj instanceof Object JavaDoc[])
65         return (Object JavaDoc[])obj ;
66     else
67         throw new Error JavaDoc( "Object[] expected" ) ;
68     }
69
70     private static StringPair getStringPair( Object JavaDoc obj )
71     {
72     if (obj instanceof StringPair)
73         return (StringPair)obj ;
74     else
75         throw new Error JavaDoc( "StringPair expected" ) ;
76     }
77
78     private static abstract class OperationBase implements Operation{
79     public boolean equals( Object JavaDoc 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 JavaDoc operate( Object JavaDoc arg )
108     {
109         try {
110         return op.operate( arg ) ;
111         } catch (java.lang.Exception JavaDoc exc) {
112         return null ;
113         }
114     }
115
116     public String JavaDoc 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 JavaDoc operate( Object JavaDoc value )
137     {
138         return getObjectArray( value )[ index ] ;
139     }
140
141     public String JavaDoc 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 JavaDoc operate( Object JavaDoc value )
155     {
156         return getStringPair( value ).getFirst() ;
157     }
158
159     public String JavaDoc toString() { return "suffixAction" ; }
160     }
161
162     private static Operation suffixActionImpl = new SuffixAction() ;
163
164     private static class ValueAction extends OperationBase
165     {
166     public Object JavaDoc operate( Object JavaDoc value )
167     {
168         return getStringPair( value ).getSecond() ;
169     }
170
171     public String JavaDoc toString() { return "valueAction" ; }
172     }
173
174     private static Operation valueActionImpl = new ValueAction() ;
175
176     private static class IdentityAction extends OperationBase
177     {
178     public Object JavaDoc operate( Object JavaDoc value )
179     {
180         return value ;
181     }
182
183     public String JavaDoc toString() { return "identityAction" ; }
184     }
185
186     private static Operation identityActionImpl = new IdentityAction() ;
187
188     private static class BooleanAction extends OperationBase
189     {
190     public Object JavaDoc operate( Object JavaDoc value )
191     {
192         return new Boolean JavaDoc( getString( value ) ) ;
193     }
194
195     public String JavaDoc toString() { return "booleanAction" ; }
196     }
197
198     private static Operation booleanActionImpl = new BooleanAction() ;
199
200     private static class IntegerAction extends OperationBase
201     {
202     public Object JavaDoc operate( Object JavaDoc value )
203     {
204         return new Integer JavaDoc( getString( value ) ) ;
205     }
206
207     public String JavaDoc toString() { return "integerAction" ; }
208     }
209
210     private static Operation integerActionImpl = new IntegerAction() ;
211
212     private static class StringAction extends OperationBase
213     {
214     public Object JavaDoc operate( Object JavaDoc value )
215     {
216         return value ;
217     }
218
219     public String JavaDoc toString() { return "stringAction" ; }
220     }
221
222     private static Operation stringActionImpl = new StringAction() ;
223
224     private static class ClassAction extends OperationBase
225     {
226     public Object JavaDoc operate( Object JavaDoc value )
227     {
228         String JavaDoc className = getString( value ) ;
229
230         try {
231         Class JavaDoc result = ORBClassLoader.loadClass( className ) ;
232         return result ;
233         } catch (Exception JavaDoc exc) {
234         ORBUtilSystemException wrapper = ORBUtilSystemException.get(
235             CORBALogDomains.ORB_LIFECYCLE ) ;
236         throw wrapper.couldNotLoadClass( exc, className ) ;
237         }
238     }
239
240     public String JavaDoc toString() { return "classAction" ; }
241     }
242
243     private static Operation classActionImpl = new ClassAction() ;
244
245     private static class SetFlagAction extends OperationBase
246     {
247     public Object JavaDoc operate( Object JavaDoc value )
248     {
249         return Boolean.TRUE ;
250     }
251
252     public String JavaDoc toString() { return "setFlagAction" ; }
253     }
254
255     private static Operation setFlagActionImpl = new SetFlagAction() ;
256
257     private static class URLAction extends OperationBase
258     {
259     public Object JavaDoc operate( Object JavaDoc value )
260     {
261         String JavaDoc val = (String JavaDoc)value ;
262         try {
263         return new URL JavaDoc( val ) ;
264         } catch (MalformedURLException JavaDoc exc) {
265         ORBUtilSystemException wrapper = ORBUtilSystemException.get(
266             CORBALogDomains.ORB_LIFECYCLE ) ;
267         throw wrapper.badUrl( exc, val ) ;
268         }
269     }
270
271     public String JavaDoc 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 JavaDoc operate( Object JavaDoc value )
333     {
334         int result = Integer.parseInt( getString( value ) ) ;
335         if ((result >= min) && (result <= max))
336         return new Integer JavaDoc( result ) ;
337         else
338         throw new IllegalArgumentException JavaDoc(
339             "Property value " + result + " is not in the range " +
340             min + " to " + max ) ;
341     }
342
343     public String JavaDoc 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 JavaDoc sep ;
355     private Operation act ;
356
357     ListAction( String JavaDoc sep, Operation act )
358     {
359         this.sep = sep ;
360         this.act = act ;
361     }
362
363     // Note that this method carefully constructs an array of the type
364
// of the first result, rather than just using Object[], which is
365
// not convertible into the correct type. Also note that no tokens
366
// results in a null result.
367
public Object JavaDoc operate( Object JavaDoc value )
368     {
369         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( getString( value ),
370         sep ) ;
371         int length = st.countTokens() ;
372         Object JavaDoc result = null ;
373         int ctr = 0 ;
374         while (st.hasMoreTokens()) {
375         String JavaDoc next = st.nextToken() ;
376         Object JavaDoc 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 JavaDoc toString() {
386         return "listAction(separator=\"" + sep +
387         "\",action=" + act + ")" ;
388     }
389     }
390
391     public static Operation listAction( String JavaDoc sep, Operation act )
392     {
393     return new ListAction( sep, act ) ;
394     }
395
396     private static class SequenceAction extends OperationBase
397     {
398     private String JavaDoc sep ;
399     private Operation[] actions ;
400
401     SequenceAction( String JavaDoc sep, Operation[] actions )
402     {
403         this.sep = sep ;
404         this.actions = actions ;
405     }
406
407     public Object JavaDoc operate( Object JavaDoc value )
408     {
409         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( getString( value ),
410         sep ) ;
411
412         int numTokens = st.countTokens() ;
413         if (numTokens != actions.length)
414         throw new Error JavaDoc(
415             "Number of tokens and number of actions do not match" ) ;
416
417         int ctr = 0 ;
418         Object JavaDoc[] result = new Object JavaDoc[ numTokens ] ;
419         while (st.hasMoreTokens()) {
420         Operation act = actions[ctr] ;
421         String JavaDoc next = st.nextToken() ;
422         result[ctr++] = act.operate( next ) ;
423         }
424
425         return result ;
426     }
427
428     public String JavaDoc toString() {
429         return "sequenceAction(separator=\"" + sep +
430         "\",actions=" +
431         ObjectUtility.compactObjectToString(actions) + ")" ;
432     }
433     }
434
435     public static Operation sequenceAction( String JavaDoc 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 JavaDoc operate( Object JavaDoc value )
453     {
454         return op2.operate( op1.operate( value ) ) ;
455     }
456
457     public String JavaDoc 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 JavaDoc operate( Object JavaDoc value )
477     {
478         Object JavaDoc[] values = (Object JavaDoc[])value ;
479         Object JavaDoc[] result = new Object JavaDoc[ 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 JavaDoc 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     // XXX Does this correctly handle array types? It seems
505
// that hetereogeneous arrays work this way, while
506
// homogeneous arrays need to use Array.newInstance tricks.
507
public Object JavaDoc operate( Object JavaDoc value )
508     {
509         Object JavaDoc[] values = (Object JavaDoc[])value ;
510         Object JavaDoc[] result = new Object JavaDoc[ 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 JavaDoc 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 JavaDoc operate( Object JavaDoc value )
530     {
531         Integer JavaDoc val = (Integer JavaDoc)value ;
532         return new Short JavaDoc( val.shortValue() ) ;
533     }
534
535     public String JavaDoc 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