KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > orb > PrefixParserAction


1 /*
2  * @(#)PrefixParserAction.java 1.17 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
8 package com.sun.corba.se.impl.orb ;
9
10 import org.omg.CORBA.INITIALIZE JavaDoc ;
11
12 import java.util.Properties JavaDoc ;
13 import java.util.List JavaDoc ;
14 import java.util.LinkedList JavaDoc ;
15 import java.util.Iterator JavaDoc ;
16
17 import java.lang.reflect.Array JavaDoc ;
18
19 import com.sun.corba.se.spi.orb.Operation ;
20 import com.sun.corba.se.spi.orb.StringPair ;
21 import com.sun.corba.se.spi.logging.CORBALogDomains ;
22
23 import com.sun.corba.se.impl.orbutil.ObjectUtility ;
24 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
25
26 public class PrefixParserAction extends ParserActionBase {
27     private Class JavaDoc componentType ;
28     private ORBUtilSystemException wrapper ;
29
30     public PrefixParserAction( String JavaDoc propertyName,
31     Operation operation, String JavaDoc fieldName, Class JavaDoc componentType )
32     {
33     super( propertyName, true, operation, fieldName ) ;
34     this.componentType = componentType ;
35     this.wrapper = ORBUtilSystemException.get(
36         CORBALogDomains.ORB_LIFECYCLE ) ;
37     }
38
39     /** For each String s that matches the prefix given by getPropertyName(),
40      * apply getOperation() to { suffix( s ), value }
41      * and add the result to an Object[]
42      * which forms the result of apply. Returns null if there are no
43      * matches.
44      */

45     public Object JavaDoc apply( Properties JavaDoc props )
46     {
47     String JavaDoc prefix = getPropertyName() ;
48     int prefixLength = prefix.length() ;
49     if (prefix.charAt( prefixLength - 1 ) != '.') {
50         prefix += '.' ;
51         prefixLength++ ;
52     }
53         
54     List JavaDoc matches = new LinkedList JavaDoc() ;
55
56     // Find all keys in props that start with propertyName
57
Iterator JavaDoc iter = props.keySet().iterator() ;
58     while (iter.hasNext()) {
59         String JavaDoc key = (String JavaDoc)(iter.next()) ;
60         if (key.startsWith( prefix )) {
61         String JavaDoc suffix = key.substring( prefixLength ) ;
62         String JavaDoc value = props.getProperty( key ) ;
63         StringPair data = new StringPair( suffix, value ) ;
64         Object JavaDoc result = getOperation().operate( data ) ;
65         matches.add( result ) ;
66         }
67     }
68
69     int size = matches.size() ;
70     if (size > 0) {
71         // Convert the list into an array of the proper type.
72
// An Object[] as a result does NOT work. Also report
73
// any errors carefully, as errors here or in parsers that
74
// use this Operation often show up at ORB.init().
75
Object JavaDoc result = null ;
76         try {
77         result = Array.newInstance( componentType, size ) ;
78         } catch (Throwable JavaDoc thr) {
79         throw wrapper.couldNotCreateArray( thr,
80             getPropertyName(), componentType,
81             new Integer JavaDoc( size ) ) ;
82         }
83
84         Iterator JavaDoc iter2 = matches.iterator() ;
85         int ctr = 0 ;
86         while (iter2.hasNext()) {
87         Object JavaDoc obj = iter2.next() ;
88
89         try {
90             Array.set( result, ctr, obj ) ;
91         } catch (Throwable JavaDoc thr) {
92             throw wrapper.couldNotSetArray( thr,
93             getPropertyName(), new Integer JavaDoc(ctr),
94             componentType, new Integer JavaDoc(size),
95             ObjectUtility.compactObjectToString( obj )) ;
96         }
97         ctr++ ;
98         }
99
100         return result ;
101     } else
102         return null ;
103     }
104 }
105
Popular Tags