KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ParserImplBase.java 1.19 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.Map JavaDoc ;
10 import java.util.Set JavaDoc ;
11 import java.util.Iterator JavaDoc ;
12 import java.util.Properties JavaDoc ;
13
14 import java.security.PrivilegedExceptionAction JavaDoc ;
15 import java.security.PrivilegedActionException JavaDoc ;
16 import java.security.AccessController JavaDoc ;
17
18 import java.lang.reflect.Field JavaDoc ;
19
20 import org.omg.CORBA.INTERNAL JavaDoc ;
21
22 import com.sun.corba.se.spi.logging.CORBALogDomains ;
23
24 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
25
26 import com.sun.corba.se.impl.orbutil.ObjectUtility ;
27
28 // XXX This could probably be further extended by using more reflection and
29
// a dynamic proxy that satisfies the interfaces that are inherited by the
30
// more derived class. Do we want to go that far?
31
public abstract class ParserImplBase {
32     private ORBUtilSystemException wrapper ;
33
34     protected abstract PropertyParser makeParser() ;
35
36     /** Override this method if there is some needed initialization
37     * that takes place after argument parsing. It is always called
38     * at the end of setFields.
39     */

40     protected void complete()
41     {
42     }
43
44     public ParserImplBase()
45     {
46     // Do nothing in this case: no parsing takes place
47
wrapper = ORBUtilSystemException.get(
48         CORBALogDomains.ORB_LIFECYCLE ) ;
49     }
50
51     public void init( DataCollector coll )
52     {
53     PropertyParser parser = makeParser() ;
54     coll.setParser( parser ) ;
55     Properties JavaDoc props = coll.getProperties() ;
56     Map JavaDoc map = parser.parse( props ) ;
57     setFields( map ) ;
58     }
59
60     private Field JavaDoc getAnyField( String JavaDoc name )
61     {
62     Field JavaDoc result = null ;
63
64     try {
65         Class JavaDoc cls = this.getClass() ;
66         result = cls.getDeclaredField( name ) ;
67         while (result == null) {
68         cls = cls.getSuperclass() ;
69         if (cls == null)
70             break ;
71
72         result = cls.getDeclaredField( name ) ;
73         }
74     } catch (Exception JavaDoc exc) {
75         throw wrapper.fieldNotFound( exc, name ) ;
76     }
77
78     if (result == null)
79         throw wrapper.fieldNotFound( name ) ;
80
81     return result ;
82     }
83
84     protected void setFields( Map JavaDoc map )
85     {
86     Set JavaDoc entries = map.entrySet() ;
87     Iterator JavaDoc iter = entries.iterator() ;
88     while (iter.hasNext()) {
89         java.util.Map.Entry entry = (java.util.Map.Entry)(iter.next()) ;
90         final String JavaDoc name = (String JavaDoc)(entry.getKey()) ;
91         final Object JavaDoc value = entry.getValue() ;
92
93         try {
94         AccessController.doPrivileged(
95             new PrivilegedExceptionAction JavaDoc() {
96             public Object JavaDoc run() throws IllegalAccessException JavaDoc,
97                 IllegalArgumentException JavaDoc
98             {
99                 Field JavaDoc field = getAnyField( name ) ;
100                 field.setAccessible( true ) ;
101                 field.set( ParserImplBase.this, value ) ;
102                 return null ;
103             }
104             }
105         ) ;
106         } catch (PrivilegedActionException JavaDoc exc) {
107         // Since exc wraps the actual exception, use exc.getCause()
108
// instead of exc.
109
throw wrapper.errorSettingField( exc.getCause(), name,
110             ObjectUtility.compactObjectToString(value) ) ;
111         }
112     }
113
114     // Make sure that any extra initialization takes place after all the
115
// fields are set from the map.
116
complete() ;
117     }
118 }
119
Popular Tags