Thu, 31 Aug 2017 18:10:36 +0800
merge
1 /*
2 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25 package com.sun.corba.se.spi.orb ;
27 import java.util.Map ;
28 import java.util.Set ;
29 import java.util.Iterator ;
30 import java.util.Properties ;
32 import java.security.PrivilegedExceptionAction ;
33 import java.security.PrivilegedActionException ;
34 import java.security.AccessController ;
36 import java.lang.reflect.Field ;
38 import org.omg.CORBA.INTERNAL ;
40 import com.sun.corba.se.spi.logging.CORBALogDomains ;
42 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
44 import com.sun.corba.se.impl.orbutil.ObjectUtility ;
46 // XXX This could probably be further extended by using more reflection and
47 // a dynamic proxy that satisfies the interfaces that are inherited by the
48 // more derived class. Do we want to go that far?
49 public abstract class ParserImplBase {
50 private ORBUtilSystemException wrapper ;
52 protected abstract PropertyParser makeParser() ;
54 /** Override this method if there is some needed initialization
55 * that takes place after argument parsing. It is always called
56 * at the end of setFields.
57 */
58 protected void complete()
59 {
60 }
62 public ParserImplBase()
63 {
64 // Do nothing in this case: no parsing takes place
65 wrapper = ORBUtilSystemException.get(
66 CORBALogDomains.ORB_LIFECYCLE ) ;
67 }
69 public void init( DataCollector coll )
70 {
71 PropertyParser parser = makeParser() ;
72 coll.setParser( parser ) ;
73 Properties props = coll.getProperties() ;
74 Map map = parser.parse( props ) ;
75 setFields( map ) ;
76 }
78 private Field getAnyField( String name )
79 {
80 Field result = null ;
82 try {
83 Class cls = this.getClass() ;
84 result = cls.getDeclaredField( name ) ;
85 while (result == null) {
86 cls = cls.getSuperclass() ;
87 if (cls == null)
88 break ;
90 result = cls.getDeclaredField( name ) ;
91 }
92 } catch (Exception exc) {
93 throw wrapper.fieldNotFound( exc, name ) ;
94 }
96 if (result == null)
97 throw wrapper.fieldNotFound( name ) ;
99 return result ;
100 }
102 protected void setFields( Map map )
103 {
104 Set entries = map.entrySet() ;
105 Iterator iter = entries.iterator() ;
106 while (iter.hasNext()) {
107 java.util.Map.Entry entry = (java.util.Map.Entry)(iter.next()) ;
108 final String name = (String)(entry.getKey()) ;
109 final Object value = entry.getValue() ;
111 try {
112 AccessController.doPrivileged(
113 new PrivilegedExceptionAction() {
114 public Object run() throws IllegalAccessException,
115 IllegalArgumentException
116 {
117 Field field = getAnyField( name ) ;
118 field.setAccessible( true ) ;
119 field.set( ParserImplBase.this, value ) ;
120 return null ;
121 }
122 }
123 ) ;
124 } catch (PrivilegedActionException exc) {
125 // Since exc wraps the actual exception, use exc.getCause()
126 // instead of exc.
127 throw wrapper.errorSettingField( exc.getCause(), name,
128 value.toString() ) ;
129 }
130 }
132 // Make sure that any extra initialization takes place after all the
133 // fields are set from the map.
134 complete() ;
135 }
136 }