4
|
1 |
/*
|
5555
|
2 |
* Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
|
4
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5555
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
4
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5555
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
4
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5555
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
4
|
24 |
*/
|
|
25 |
package com.sun.corba.se.spi.orb ;
|
|
26 |
|
|
27 |
import java.util.StringTokenizer ;
|
|
28 |
|
|
29 |
import java.lang.reflect.Array ;
|
|
30 |
|
|
31 |
import java.net.URL ;
|
|
32 |
import java.net.MalformedURLException ;
|
|
33 |
|
|
34 |
import com.sun.corba.se.spi.logging.CORBALogDomains ;
|
|
35 |
|
|
36 |
import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
|
|
37 |
import com.sun.corba.se.impl.orbutil.ORBClassLoader ;
|
|
38 |
import com.sun.corba.se.impl.orbutil.ObjectUtility ;
|
|
39 |
|
|
40 |
/** This is a static factory class for commonly used operations
|
|
41 |
* for property parsing. The following operations are supported:
|
|
42 |
* <ul>
|
|
43 |
* <li>maskErrorAction( Operation op ) executes op and returns the result. If op throws an
|
|
44 |
* exception, the result is null.
|
|
45 |
* <li>indexAction( int arg ) returns the [arg] element of value, which must be an Object[]</li>
|
|
46 |
* <li>identityAction() return the value</li>
|
|
47 |
* <li>booleanAction() return a Boolean representing true or false values of the String value</li>
|
|
48 |
* <li>integerAction() returns an Integer for the String value, which must be a decimal integer</li>
|
|
49 |
* <li>stringAction() returns the String value</li>
|
|
50 |
* <li>classAction() returns a class for the String value, as loaded by the ORB classloader</li>
|
|
51 |
* <li>setFlagAction() always return Boolean.TRUE</li>
|
|
52 |
* <li>URLAction() returns a java.net.URL for the String value, which must be a valid URL</li>
|
|
53 |
* <li>integerRangeAction( int min, int max ) returns an Integer for the String value, which must be a
|
|
54 |
* decimal integer in the range min to max inclusive</li>
|
|
55 |
* <li>listAction( String sep, Operation ) tokenizes the String value with sep as separator, then
|
|
56 |
* applies the Operation to each token, and returns an array of the result</li>
|
|
57 |
* <li>sequenceAction( String, Operation[] ) tokenizes the String value with sep as separator, then
|
|
58 |
* applies each Operation in the Operation array to successive tokens, and returns an array of the results</li>
|
|
59 |
* <li>compose( Operation op1, Operation op2 ) is the operation that applies op2 to the result of applying
|
|
60 |
* op1 to the value</li>
|
|
61 |
* <li>mapAction( Operation ) applies the Operation to each element of an array of objects, and returns
|
|
62 |
* an array of the results</li>
|
|
63 |
* <li>mapSequenceAction( Operation[] ) applies the corresponding element of the Operation array to an
|
|
64 |
* element of the Object[] value, and returns an array of the results</li>
|
|
65 |
* <li>convertIntegerToShort coerces an Integer into a Short.</li>
|
|
66 |
* </ul>
|
|
67 |
* Other operations can be directly defined, and combined using these basic operations.
|
|
68 |
*/
|
|
69 |
public abstract class OperationFactory {
|
|
70 |
private OperationFactory() {}
|
|
71 |
|
|
72 |
private static String getString( Object obj )
|
|
73 |
{
|
|
74 |
if (obj instanceof String)
|
|
75 |
return (String)obj ;
|
|
76 |
else
|
|
77 |
throw new Error( "String expected" ) ;
|
|
78 |
}
|
|
79 |
|
|
80 |
private static Object[] getObjectArray( Object obj )
|
|
81 |
{
|
|
82 |
if (obj instanceof Object[])
|
|
83 |
return (Object[])obj ;
|
|
84 |
else
|
|
85 |
throw new Error( "Object[] expected" ) ;
|
|
86 |
}
|
|
87 |
|
|
88 |
private static StringPair getStringPair( Object obj )
|
|
89 |
{
|
|
90 |
if (obj instanceof StringPair)
|
|
91 |
return (StringPair)obj ;
|
|
92 |
else
|
|
93 |
throw new Error( "StringPair expected" ) ;
|
|
94 |
}
|
|
95 |
|
|
96 |
private static abstract class OperationBase implements Operation{
|
|
97 |
public boolean equals( Object obj )
|
|
98 |
{
|
|
99 |
if (this==obj)
|
|
100 |
return true ;
|
|
101 |
|
|
102 |
if (!(obj instanceof OperationBase))
|
|
103 |
return false ;
|
|
104 |
|
|
105 |
OperationBase other = (OperationBase)obj ;
|
|
106 |
|
|
107 |
return toString().equals( other.toString() ) ;
|
|
108 |
}
|
|
109 |
|
|
110 |
public int hashCode()
|
|
111 |
{
|
|
112 |
return toString().hashCode() ;
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
private static class MaskErrorAction extends OperationBase
|
|
117 |
{
|
|
118 |
private Operation op ;
|
|
119 |
|
|
120 |
public MaskErrorAction( Operation op )
|
|
121 |
{
|
|
122 |
this.op = op ;
|
|
123 |
}
|
|
124 |
|
|
125 |
public Object operate( Object arg )
|
|
126 |
{
|
|
127 |
try {
|
|
128 |
return op.operate( arg ) ;
|
|
129 |
} catch (java.lang.Exception exc) {
|
|
130 |
return null ;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
public String toString()
|
|
135 |
{
|
|
136 |
return "maskErrorAction(" + op + ")" ;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
public static Operation maskErrorAction( Operation op )
|
|
141 |
{
|
|
142 |
return new MaskErrorAction( op ) ;
|
|
143 |
}
|
|
144 |
|
|
145 |
private static class IndexAction extends OperationBase
|
|
146 |
{
|
|
147 |
private int index ;
|
|
148 |
|
|
149 |
public IndexAction( int index )
|
|
150 |
{
|
|
151 |
this.index = index ;
|
|
152 |
}
|
|
153 |
|
|
154 |
public Object operate( Object value )
|
|
155 |
{
|
|
156 |
return getObjectArray( value )[ index ] ;
|
|
157 |
}
|
|
158 |
|
|
159 |
public String toString()
|
|
160 |
{
|
|
161 |
return "indexAction(" + index + ")" ;
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
public static Operation indexAction( int index )
|
|
166 |
{
|
|
167 |
return new IndexAction( index ) ;
|
|
168 |
}
|
|
169 |
|
|
170 |
private static class SuffixAction extends OperationBase
|
|
171 |
{
|
|
172 |
public Object operate( Object value )
|
|
173 |
{
|
|
174 |
return getStringPair( value ).getFirst() ;
|
|
175 |
}
|
|
176 |
|
|
177 |
public String toString() { return "suffixAction" ; }
|
|
178 |
}
|
|
179 |
|
|
180 |
private static Operation suffixActionImpl = new SuffixAction() ;
|
|
181 |
|
|
182 |
private static class ValueAction extends OperationBase
|
|
183 |
{
|
|
184 |
public Object operate( Object value )
|
|
185 |
{
|
|
186 |
return getStringPair( value ).getSecond() ;
|
|
187 |
}
|
|
188 |
|
|
189 |
public String toString() { return "valueAction" ; }
|
|
190 |
}
|
|
191 |
|
|
192 |
private static Operation valueActionImpl = new ValueAction() ;
|
|
193 |
|
|
194 |
private static class IdentityAction extends OperationBase
|
|
195 |
{
|
|
196 |
public Object operate( Object value )
|
|
197 |
{
|
|
198 |
return value ;
|
|
199 |
}
|
|
200 |
|
|
201 |
public String toString() { return "identityAction" ; }
|
|
202 |
}
|
|
203 |
|
|
204 |
private static Operation identityActionImpl = new IdentityAction() ;
|
|
205 |
|
|
206 |
private static class BooleanAction extends OperationBase
|
|
207 |
{
|
|
208 |
public Object operate( Object value )
|
|
209 |
{
|
|
210 |
return new Boolean( getString( value ) ) ;
|
|
211 |
}
|
|
212 |
|
|
213 |
public String toString() { return "booleanAction" ; }
|
|
214 |
}
|
|
215 |
|
|
216 |
private static Operation booleanActionImpl = new BooleanAction() ;
|
|
217 |
|
|
218 |
private static class IntegerAction extends OperationBase
|
|
219 |
{
|
|
220 |
public Object operate( Object value )
|
|
221 |
{
|
|
222 |
return new Integer( getString( value ) ) ;
|
|
223 |
}
|
|
224 |
|
|
225 |
public String toString() { return "integerAction" ; }
|
|
226 |
}
|
|
227 |
|
|
228 |
private static Operation integerActionImpl = new IntegerAction() ;
|
|
229 |
|
|
230 |
private static class StringAction extends OperationBase
|
|
231 |
{
|
|
232 |
public Object operate( Object value )
|
|
233 |
{
|
|
234 |
return value ;
|
|
235 |
}
|
|
236 |
|
|
237 |
public String toString() { return "stringAction" ; }
|
|
238 |
}
|
|
239 |
|
|
240 |
private static Operation stringActionImpl = new StringAction() ;
|
|
241 |
|
|
242 |
private static class ClassAction extends OperationBase
|
|
243 |
{
|
|
244 |
public Object operate( Object value )
|
|
245 |
{
|
|
246 |
String className = getString( value ) ;
|
|
247 |
|
|
248 |
try {
|
|
249 |
Class result = ORBClassLoader.loadClass( className ) ;
|
|
250 |
return result ;
|
|
251 |
} catch (Exception exc) {
|
|
252 |
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
|
|
253 |
CORBALogDomains.ORB_LIFECYCLE ) ;
|
|
254 |
throw wrapper.couldNotLoadClass( exc, className ) ;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
public String toString() { return "classAction" ; }
|
|
259 |
}
|
|
260 |
|
|
261 |
private static Operation classActionImpl = new ClassAction() ;
|
|
262 |
|
|
263 |
private static class SetFlagAction extends OperationBase
|
|
264 |
{
|
|
265 |
public Object operate( Object value )
|
|
266 |
{
|
|
267 |
return Boolean.TRUE ;
|
|
268 |
}
|
|
269 |
|
|
270 |
public String toString() { return "setFlagAction" ; }
|
|
271 |
}
|
|
272 |
|
|
273 |
private static Operation setFlagActionImpl = new SetFlagAction() ;
|
|
274 |
|
|
275 |
private static class URLAction extends OperationBase
|
|
276 |
{
|
|
277 |
public Object operate( Object value )
|
|
278 |
{
|
|
279 |
String val = (String)value ;
|
|
280 |
try {
|
|
281 |
return new URL( val ) ;
|
|
282 |
} catch (MalformedURLException exc) {
|
|
283 |
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
|
|
284 |
CORBALogDomains.ORB_LIFECYCLE ) ;
|
|
285 |
throw wrapper.badUrl( exc, val ) ;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
public String toString() { return "URLAction" ; }
|
|
290 |
}
|
|
291 |
|
|
292 |
private static Operation URLActionImpl = new URLAction() ;
|
|
293 |
|
|
294 |
public static Operation identityAction()
|
|
295 |
{
|
|
296 |
return identityActionImpl ;
|
|
297 |
}
|
|
298 |
|
|
299 |
public static Operation suffixAction()
|
|
300 |
{
|
|
301 |
return suffixActionImpl ;
|
|
302 |
}
|
|
303 |
|
|
304 |
public static Operation valueAction()
|
|
305 |
{
|
|
306 |
return valueActionImpl ;
|
|
307 |
}
|
|
308 |
|
|
309 |
public static Operation booleanAction()
|
|
310 |
{
|
|
311 |
return booleanActionImpl ;
|
|
312 |
}
|
|
313 |
|
|
314 |
public static Operation integerAction()
|
|
315 |
{
|
|
316 |
return integerActionImpl ;
|
|
317 |
}
|
|
318 |
|
|
319 |
public static Operation stringAction()
|
|
320 |
{
|
|
321 |
return stringActionImpl ;
|
|
322 |
}
|
|
323 |
|
|
324 |
public static Operation classAction()
|
|
325 |
{
|
|
326 |
return classActionImpl ;
|
|
327 |
}
|
|
328 |
|
|
329 |
public static Operation setFlagAction()
|
|
330 |
{
|
|
331 |
return setFlagActionImpl ;
|
|
332 |
}
|
|
333 |
|
|
334 |
public static Operation URLAction()
|
|
335 |
{
|
|
336 |
return URLActionImpl ;
|
|
337 |
}
|
|
338 |
|
|
339 |
private static class IntegerRangeAction extends OperationBase
|
|
340 |
{
|
|
341 |
private int min ;
|
|
342 |
private int max ;
|
|
343 |
|
|
344 |
IntegerRangeAction( int min, int max )
|
|
345 |
{
|
|
346 |
this.min = min ;
|
|
347 |
this.max = max ;
|
|
348 |
}
|
|
349 |
|
|
350 |
public Object operate( Object value )
|
|
351 |
{
|
|
352 |
int result = Integer.parseInt( getString( value ) ) ;
|
|
353 |
if ((result >= min) && (result <= max))
|
|
354 |
return new Integer( result ) ;
|
|
355 |
else
|
|
356 |
throw new IllegalArgumentException(
|
|
357 |
"Property value " + result + " is not in the range " +
|
|
358 |
min + " to " + max ) ;
|
|
359 |
}
|
|
360 |
|
|
361 |
public String toString() {
|
|
362 |
return "integerRangeAction(" + min + "," + max + ")" ;
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
public static Operation integerRangeAction( int min, int max )
|
|
367 |
{
|
|
368 |
return new IntegerRangeAction( min, max ) ;
|
|
369 |
}
|
|
370 |
|
|
371 |
private static class ListAction extends OperationBase {
|
|
372 |
private String sep ;
|
|
373 |
private Operation act ;
|
|
374 |
|
|
375 |
ListAction( String sep, Operation act )
|
|
376 |
{
|
|
377 |
this.sep = sep ;
|
|
378 |
this.act = act ;
|
|
379 |
}
|
|
380 |
|
|
381 |
// Note that this method carefully constructs an array of the type
|
|
382 |
// of the first result, rather than just using Object[], which is
|
|
383 |
// not convertible into the correct type. Also note that no tokens
|
|
384 |
// results in a null result.
|
|
385 |
public Object operate( Object value )
|
|
386 |
{
|
|
387 |
StringTokenizer st = new StringTokenizer( getString( value ),
|
|
388 |
sep ) ;
|
|
389 |
int length = st.countTokens() ;
|
|
390 |
Object result = null ;
|
|
391 |
int ctr = 0 ;
|
|
392 |
while (st.hasMoreTokens()) {
|
|
393 |
String next = st.nextToken() ;
|
|
394 |
Object val = act.operate( next ) ;
|
|
395 |
if (result == null)
|
|
396 |
result = Array.newInstance( val.getClass(), length ) ;
|
|
397 |
Array.set( result, ctr++, val ) ;
|
|
398 |
}
|
|
399 |
|
|
400 |
return result ;
|
|
401 |
}
|
|
402 |
|
|
403 |
public String toString() {
|
|
404 |
return "listAction(separator=\"" + sep +
|
|
405 |
"\",action=" + act + ")" ;
|
|
406 |
}
|
|
407 |
}
|
|
408 |
|
|
409 |
public static Operation listAction( String sep, Operation act )
|
|
410 |
{
|
|
411 |
return new ListAction( sep, act ) ;
|
|
412 |
}
|
|
413 |
|
|
414 |
private static class SequenceAction extends OperationBase
|
|
415 |
{
|
|
416 |
private String sep ;
|
|
417 |
private Operation[] actions ;
|
|
418 |
|
|
419 |
SequenceAction( String sep, Operation[] actions )
|
|
420 |
{
|
|
421 |
this.sep = sep ;
|
|
422 |
this.actions = actions ;
|
|
423 |
}
|
|
424 |
|
|
425 |
public Object operate( Object value )
|
|
426 |
{
|
|
427 |
StringTokenizer st = new StringTokenizer( getString( value ),
|
|
428 |
sep ) ;
|
|
429 |
|
|
430 |
int numTokens = st.countTokens() ;
|
|
431 |
if (numTokens != actions.length)
|
|
432 |
throw new Error(
|
|
433 |
"Number of tokens and number of actions do not match" ) ;
|
|
434 |
|
|
435 |
int ctr = 0 ;
|
|
436 |
Object[] result = new Object[ numTokens ] ;
|
|
437 |
while (st.hasMoreTokens()) {
|
|
438 |
Operation act = actions[ctr] ;
|
|
439 |
String next = st.nextToken() ;
|
|
440 |
result[ctr++] = act.operate( next ) ;
|
|
441 |
}
|
|
442 |
|
|
443 |
return result ;
|
|
444 |
}
|
|
445 |
|
|
446 |
public String toString() {
|
|
447 |
return "sequenceAction(separator=\"" + sep +
|
|
448 |
"\",actions=" +
|
|
449 |
ObjectUtility.compactObjectToString(actions) + ")" ;
|
|
450 |
}
|
|
451 |
}
|
|
452 |
|
|
453 |
public static Operation sequenceAction( String sep,
|
|
454 |
Operation[] actions )
|
|
455 |
{
|
|
456 |
return new SequenceAction( sep, actions ) ;
|
|
457 |
}
|
|
458 |
|
|
459 |
private static class ComposeAction extends OperationBase
|
|
460 |
{
|
|
461 |
private Operation op1 ;
|
|
462 |
private Operation op2 ;
|
|
463 |
|
|
464 |
ComposeAction( Operation op1, Operation op2 )
|
|
465 |
{
|
|
466 |
this.op1 = op1 ;
|
|
467 |
this.op2 = op2 ;
|
|
468 |
}
|
|
469 |
|
|
470 |
public Object operate( Object value )
|
|
471 |
{
|
|
472 |
return op2.operate( op1.operate( value ) ) ;
|
|
473 |
}
|
|
474 |
|
|
475 |
public String toString() {
|
|
476 |
return "composition(" + op1 + "," + op2 + ")" ;
|
|
477 |
}
|
|
478 |
}
|
|
479 |
|
|
480 |
public static Operation compose( Operation op1, Operation op2 )
|
|
481 |
{
|
|
482 |
return new ComposeAction( op1, op2 ) ;
|
|
483 |
}
|
|
484 |
|
|
485 |
private static class MapAction extends OperationBase
|
|
486 |
{
|
|
487 |
Operation op ;
|
|
488 |
|
|
489 |
MapAction( Operation op )
|
|
490 |
{
|
|
491 |
this.op = op ;
|
|
492 |
}
|
|
493 |
|
|
494 |
public Object operate( Object value )
|
|
495 |
{
|
|
496 |
Object[] values = (Object[])value ;
|
|
497 |
Object[] result = new Object[ values.length ] ;
|
|
498 |
for (int ctr=0; ctr<values.length; ctr++ )
|
|
499 |
result[ctr] = op.operate( values[ctr] ) ;
|
|
500 |
return result ;
|
|
501 |
}
|
|
502 |
|
|
503 |
public String toString() {
|
|
504 |
return "mapAction(" + op + ")" ;
|
|
505 |
}
|
|
506 |
}
|
|
507 |
|
|
508 |
public static Operation mapAction( Operation op )
|
|
509 |
{
|
|
510 |
return new MapAction( op ) ;
|
|
511 |
}
|
|
512 |
|
|
513 |
private static class MapSequenceAction extends OperationBase
|
|
514 |
{
|
|
515 |
private Operation[] op ;
|
|
516 |
|
|
517 |
public MapSequenceAction( Operation[] op )
|
|
518 |
{
|
|
519 |
this.op = op ;
|
|
520 |
}
|
|
521 |
|
|
522 |
// XXX Does this correctly handle array types? It seems
|
|
523 |
// that hetereogeneous arrays work this way, while
|
|
524 |
// homogeneous arrays need to use Array.newInstance tricks.
|
|
525 |
public Object operate( Object value )
|
|
526 |
{
|
|
527 |
Object[] values = (Object[])value ;
|
|
528 |
Object[] result = new Object[ values.length ] ;
|
|
529 |
for (int ctr=0; ctr<values.length; ctr++ )
|
|
530 |
result[ctr] = op[ctr].operate( values[ctr] ) ;
|
|
531 |
return result ;
|
|
532 |
}
|
|
533 |
|
|
534 |
public String toString() {
|
|
535 |
return "mapSequenceAction(" +
|
|
536 |
ObjectUtility.compactObjectToString(op) + ")" ;
|
|
537 |
}
|
|
538 |
}
|
|
539 |
|
|
540 |
public static Operation mapSequenceAction( Operation[] op )
|
|
541 |
{
|
|
542 |
return new MapSequenceAction( op ) ;
|
|
543 |
}
|
|
544 |
|
|
545 |
private static class ConvertIntegerToShort extends OperationBase
|
|
546 |
{
|
|
547 |
public Object operate( Object value )
|
|
548 |
{
|
|
549 |
Integer val = (Integer)value ;
|
|
550 |
return new Short( val.shortValue() ) ;
|
|
551 |
}
|
|
552 |
|
|
553 |
public String toString() {
|
|
554 |
return "ConvertIntegerToShort" ;
|
|
555 |
}
|
|
556 |
}
|
|
557 |
|
|
558 |
private static Operation convertIntegerToShortImpl = new ConvertIntegerToShort() ;
|
|
559 |
|
|
560 |
public static Operation convertIntegerToShort()
|
|
561 |
{
|
|
562 |
return convertIntegerToShortImpl ;
|
|
563 |
}
|
|
564 |
}
|