KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DataCollectorBase.java 1.19 04/03/01
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 com.sun.corba.se.impl.orbutil.GetPropertyAction ;
11
12 import java.security.PrivilegedAction JavaDoc ;
13 import java.security.AccessController JavaDoc ;
14
15 import java.applet.Applet JavaDoc ;
16
17 import java.util.Properties JavaDoc ;
18 import java.util.Vector JavaDoc ;
19 import java.util.Set JavaDoc ;
20 import java.util.HashSet JavaDoc ;
21 import java.util.Enumeration JavaDoc ;
22 import java.util.Iterator JavaDoc ;
23 import java.util.StringTokenizer JavaDoc ;
24
25 import java.net.URL JavaDoc ;
26
27 import java.security.AccessController JavaDoc ;
28
29 import java.io.File JavaDoc ;
30 import java.io.FileInputStream JavaDoc ;
31
32 import com.sun.corba.se.spi.orb.DataCollector ;
33 import com.sun.corba.se.spi.orb.PropertyParser ;
34
35 import com.sun.corba.se.impl.orbutil.ORBConstants ;
36 import com.sun.corba.se.impl.orbutil.ORBUtility;
37
38 public abstract class DataCollectorBase implements DataCollector {
39     private PropertyParser parser ;
40     private Set JavaDoc propertyNames ;
41     private Set JavaDoc propertyPrefixes ;
42     private Set JavaDoc URLPropertyNames ;
43     protected String JavaDoc localHostName ;
44     protected String JavaDoc configurationHostName ;
45     private boolean setParserCalled ;
46     private Properties JavaDoc originalProps ;
47     private Properties JavaDoc resultProps ;
48
49     public DataCollectorBase( Properties JavaDoc props, String JavaDoc localHostName,
50     String JavaDoc configurationHostName )
51     {
52     // XXX This is fully initialized here. So do we ever want to
53
// generalize this (or perhaps this is the wrong place for this?)
54
URLPropertyNames = new HashSet JavaDoc() ;
55     URLPropertyNames.add( ORBConstants.INITIAL_SERVICES_PROPERTY ) ;
56
57     propertyNames = new HashSet JavaDoc() ;
58
59     // Make sure that we are ready to handle -ORBInitRef. This is special
60
// due to the need to handle multiple -ORBInitRef args as prefix
61
// parsing.
62
propertyNames.add( ORBConstants.ORB_INIT_REF_PROPERTY ) ;
63
64     propertyPrefixes = new HashSet JavaDoc() ;
65
66     this.originalProps = props ;
67     this.localHostName = localHostName ;
68     this.configurationHostName = configurationHostName ;
69     setParserCalled = false ;
70     resultProps = new Properties JavaDoc() ;
71     }
72
73 //////////////////////////////////////////////////////////
74
// Public interface defined in DataCollector
75
//////////////////////////////////////////////////////////
76

77     public boolean initialHostIsLocal()
78     {
79     checkSetParserCalled() ;
80     return localHostName.equals( resultProps.getProperty(
81         ORBConstants.INITIAL_HOST_PROPERTY ) ) ;
82     }
83
84     public void setParser( PropertyParser parser )
85     {
86     Iterator JavaDoc iter = parser.iterator() ;
87     while (iter.hasNext()) {
88         ParserAction pa = (ParserAction)(iter.next()) ;
89         if (pa.isPrefix())
90         propertyPrefixes.add( pa.getPropertyName() ) ;
91         else
92         propertyNames.add( pa.getPropertyName() ) ;
93     }
94
95     collect() ;
96     setParserCalled = true ;
97     }
98
99     public Properties JavaDoc getProperties()
100     {
101     checkSetParserCalled() ;
102     return resultProps ;
103     }
104
105 //////////////////////////////////////////////////////////
106
// public interface from DataCollector that must be defined
107
// in subclasses
108
//////////////////////////////////////////////////////////
109

110     public abstract boolean isApplet() ;
111
112 //////////////////////////////////////////////////////////
113
// Implementation methods needed in subclasses
114
//////////////////////////////////////////////////////////
115

116     protected abstract void collect() ;
117
118 //////////////////////////////////////////////////////////
119
// methods for use by subclasses
120
//////////////////////////////////////////////////////////
121

122     protected void checkPropertyDefaults()
123     {
124         String JavaDoc host =
125             resultProps.getProperty( ORBConstants.INITIAL_HOST_PROPERTY ) ;
126
127         if ((host == null) || (host.equals("")))
128             setProperty( ORBConstants.INITIAL_HOST_PROPERTY,
129         configurationHostName );
130
131         String JavaDoc serverHost =
132             resultProps.getProperty( ORBConstants.SERVER_HOST_PROPERTY ) ;
133
134         if (serverHost == null ||
135         serverHost.equals("") ||
136         serverHost.equals("0.0.0.0") ||
137         serverHost.equals("::") ||
138         serverHost.toLowerCase().equals("::ffff:0.0.0.0"))
139     {
140             setProperty(ORBConstants.SERVER_HOST_PROPERTY,
141             localHostName);
142         setProperty(ORBConstants.LISTEN_ON_ALL_INTERFACES,
143             ORBConstants.LISTEN_ON_ALL_INTERFACES);
144     }
145     }
146
147     protected void findPropertiesFromArgs( String JavaDoc[] params )
148     {
149         if (params == null)
150             return;
151
152         // All command-line args are of the form "-ORBkey value".
153
// The key is mapped to <prefix>.ORBkey.
154

155         String JavaDoc name ;
156         String JavaDoc value ;
157
158         for ( int i=0; i<params.length; i++ ) {
159             value = null ;
160             name = null ;
161
162             if ( params[i] != null && params[i].startsWith("-ORB") ) {
163                 String JavaDoc argName = params[i].substring( 1 ) ;
164                 name = findMatchingPropertyName( propertyNames, argName ) ;
165
166                 if (name != null)
167                     if ( i+1 < params.length && params[i+1] != null ) {
168                         value = params[++i];
169                     }
170             }
171
172             if (value != null) {
173         setProperty( name, value ) ;
174             }
175         }
176     }
177
178     protected void findPropertiesFromApplet( final Applet JavaDoc app )
179     {
180         // Cannot use propertyPrefixes here, since there is no
181
// way to fetch properties by prefix from an Applet.
182
if (app == null)
183             return;
184
185         PropertyCallback callback = new PropertyCallback() {
186         public String JavaDoc get(String JavaDoc name) {
187         return app.getParameter(name);
188         }
189     } ;
190
191     findPropertiesByName( propertyNames.iterator(), callback ) ;
192     
193         // Special Case:
194
//
195
// Convert any applet parameter relative URLs to an
196
// absolute URL based on the Document Root. This is so HTML
197
// URLs can be kept relative which is sometimes useful for
198
// managing the Document Root layout.
199
PropertyCallback URLCallback = new PropertyCallback() {
200         public String JavaDoc get( String JavaDoc name ) {
201         String JavaDoc value = resultProps.getProperty(name);
202         if (value == null)
203             return null ;
204
205         try {
206             URL JavaDoc url = new URL JavaDoc( app.getDocumentBase(), value ) ;
207             return url.toExternalForm() ;
208         } catch (java.net.MalformedURLException JavaDoc exc) {
209             // Just preserve the original (malformed) value:
210
// the error will be handled later.
211
return value ;
212         }
213         }
214     } ;
215
216     findPropertiesByName( URLPropertyNames.iterator(),
217         URLCallback ) ;
218     }
219
220     private void doProperties( final Properties JavaDoc props )
221     {
222         PropertyCallback callback = new PropertyCallback() {
223         public String JavaDoc get(String JavaDoc name) {
224         return props.getProperty(name);
225         }
226     } ;
227     
228     findPropertiesByName( propertyNames.iterator(), callback ) ;
229
230         findPropertiesByPrefix( propertyPrefixes,
231         makeIterator( props.propertyNames()), callback );
232     }
233
234     protected void findPropertiesFromFile()
235     {
236         final Properties JavaDoc fileProps = getFileProperties() ;
237         if (fileProps==null)
238             return ;
239
240     doProperties( fileProps ) ;
241     }
242
243     protected void findPropertiesFromProperties()
244     {
245         if (originalProps == null)
246             return;
247
248     doProperties( originalProps ) ;
249     }
250
251     //
252
// Map System properties to ORB properties.
253
// Security bug fix 4278205:
254
// Only allow reading of system properties with ORB prefixes.
255
// Previously a malicious subclass was able to read ANY system property.
256
// Note that other prefixes are fine in other contexts; it is only
257
// system properties that should impose a restriction.
258
protected void findPropertiesFromSystem()
259     {
260     Set JavaDoc normalNames = getCORBAPrefixes( propertyNames ) ;
261     Set JavaDoc prefixNames = getCORBAPrefixes( propertyPrefixes ) ;
262
263     PropertyCallback callback = new PropertyCallback() {
264         public String JavaDoc get(String JavaDoc name) {
265         return getSystemProperty(name);
266         }
267     } ;
268
269     findPropertiesByName( normalNames.iterator(), callback ) ;
270
271         findPropertiesByPrefix( prefixNames,
272         getSystemPropertyNames(), callback ) ;
273     }
274
275 //////////////////////////////////////////////////////////
276
// internal implementation
277
//////////////////////////////////////////////////////////
278

279     // Store name, value in resultProps, with special
280
// treatment of ORBInitRef. All updates to resultProps
281
// must happen through this method.
282
private void setProperty( String JavaDoc name, String JavaDoc value )
283     {
284     if( name.equals( ORBConstants.ORB_INIT_REF_PROPERTY ) ) {
285         // Value is <name>=<URL>
286
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( value, "=" ) ;
287         if (st.countTokens() != 2)
288         throw new IllegalArgumentException JavaDoc() ;
289
290         String JavaDoc refName = st.nextToken() ;
291         String JavaDoc refValue = st.nextToken() ;
292
293         resultProps.setProperty( name + "." + refName, refValue ) ;
294     } else {
295         resultProps.setProperty( name, value ) ;
296     }
297     }
298
299     private void checkSetParserCalled()
300     {
301     if (!setParserCalled)
302         throw new IllegalStateException JavaDoc( "setParser not called." ) ;
303     }
304
305     // For each prefix in prefixes, For each name in propertyNames,
306
// if (prefix is a prefix of name) get value from getProperties and
307
// setProperty (name, value).
308
private void findPropertiesByPrefix( Set JavaDoc prefixes,
309     Iterator JavaDoc propertyNames, PropertyCallback getProperty )
310     {
311     while (propertyNames.hasNext()) {
312         String JavaDoc name = (String JavaDoc)(propertyNames.next()) ;
313         Iterator JavaDoc iter = prefixes.iterator() ;
314         while (iter.hasNext()) {
315         String JavaDoc prefix = (String JavaDoc)(iter.next()) ;
316         if (name.startsWith( prefix )) {
317             String JavaDoc value = getProperty.get( name ) ;
318
319                     // Note: do a put even if value is null since just
320
// the presence of the property may be significant.
321
setProperty( name, value ) ;
322         }
323         }
324     }
325     }
326
327     // For each prefix in names, get the corresponding property
328
// value from the callback, and store the name/value pair in
329
// the result.
330
private void findPropertiesByName( Iterator JavaDoc names,
331     PropertyCallback getProperty )
332     {
333     while (names.hasNext()) {
334         String JavaDoc name = (String JavaDoc)(names.next()) ;
335         String JavaDoc value = getProperty.get( name ) ;
336         if (value != null)
337         setProperty( name, value ) ;
338     }
339     }
340
341     private static String JavaDoc getSystemProperty(final String JavaDoc name)
342     {
343         return (String JavaDoc)AccessController.doPrivileged(
344         new GetPropertyAction(name));
345     }
346
347     // Map command-line arguments to ORB properties.
348
//
349
private String JavaDoc findMatchingPropertyName( Set JavaDoc names,
350     String JavaDoc suffix )
351     {
352     Iterator JavaDoc iter = names.iterator() ;
353     while (iter.hasNext()) {
354         String JavaDoc name = (String JavaDoc)(iter.next()) ;
355         if (name.endsWith( suffix ))
356         return name ;
357     }
358
359     return null ;
360     }
361
362     private static Iterator JavaDoc makeIterator( final Enumeration JavaDoc enumeration )
363     {
364     return new Iterator JavaDoc() {
365         public boolean hasNext() { return enumeration.hasMoreElements() ; }
366         public Object JavaDoc next() { return enumeration.nextElement() ; }
367         public void remove() { throw new UnsupportedOperationException JavaDoc() ; }
368     } ;
369     }
370
371     private static Iterator JavaDoc getSystemPropertyNames()
372     {
373         // This will not throw a SecurityException because this
374
// class was loaded from rt.jar using the bootstrap classloader.
375
Enumeration JavaDoc enumeration = (Enumeration JavaDoc)
376             AccessController.doPrivileged(
377                 new PrivilegedAction JavaDoc() {
378                       public java.lang.Object JavaDoc run() {
379                           return System.getProperties().propertyNames();
380                       }
381                 }
382         );
383
384     return makeIterator( enumeration ) ;
385     }
386
387     private void getPropertiesFromFile( Properties JavaDoc props, String JavaDoc fileName )
388     {
389         try {
390         File JavaDoc file = new File JavaDoc( fileName ) ;
391         if (!file.exists())
392         return ;
393
394             FileInputStream JavaDoc in = new FileInputStream JavaDoc( file ) ;
395         
396         try {
397         props.load( in ) ;
398         } finally {
399         in.close() ;
400         }
401         } catch (Exception JavaDoc exc) {
402             // if (ORBInitDebug)
403
// dprint( "ORB properties file " + fileName + " not found: " +
404
// exc) ;
405
}
406     }
407
408     private Properties JavaDoc getFileProperties()
409     {
410         Properties JavaDoc defaults = new Properties JavaDoc() ;
411
412     String JavaDoc javaHome = getSystemProperty( "java.home" ) ;
413     String JavaDoc fileName = javaHome + File.separator + "lib" + File.separator +
414         "orb.properties" ;
415
416     getPropertiesFromFile( defaults, fileName ) ;
417
418     Properties JavaDoc results = new Properties JavaDoc( defaults ) ;
419
420         String JavaDoc userHome = getSystemProperty( "user.home" ) ;
421         fileName = userHome + File.separator + "orb.properties" ;
422
423     getPropertiesFromFile( results, fileName ) ;
424     return results ;
425     }
426
427     private boolean hasCORBAPrefix( String JavaDoc prefix )
428     {
429     return prefix.startsWith( ORBConstants.ORG_OMG_PREFIX ) ||
430         prefix.startsWith( ORBConstants.SUN_PREFIX ) ||
431         prefix.startsWith( ORBConstants.SUN_LC_PREFIX ) ||
432         prefix.startsWith( ORBConstants.SUN_LC_VERSION_PREFIX ) ;
433     }
434
435     // Return only those element of prefixes for which hasCORBAPrefix
436
// is true.
437
private Set JavaDoc getCORBAPrefixes( final Set JavaDoc prefixes )
438     {
439     Set JavaDoc result = new HashSet JavaDoc() ;
440     Iterator JavaDoc iter = prefixes.iterator() ;
441     while (iter.hasNext()) {
442         String JavaDoc element = (String JavaDoc)(iter.next()) ;
443         if (hasCORBAPrefix( element ))
444         result.add( element ) ;
445     }
446
447     return result ;
448     }
449 }
450
451 // Used to collect properties from various sources.
452
abstract class PropertyCallback
453 {
454     abstract public String JavaDoc get(String JavaDoc name);
455 }
456
Popular Tags