/* * @(#)DataCollectorBase.java 1.19 04/03/01 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.sun.corba.se.impl.orb ; import com.sun.corba.se.impl.orbutil.GetPropertyAction ; import java.security.PrivilegedAction ; import java.security.AccessController ; import java.applet.Applet ; import java.util.Properties ; import java.util.Vector ; import java.util.Set ; import java.util.HashSet ; import java.util.Enumeration ; import java.util.Iterator ; import java.util.StringTokenizer ; import java.net.URL ; import java.security.AccessController ; import java.io.File ; import java.io.FileInputStream ; import com.sun.corba.se.spi.orb.DataCollector ; import com.sun.corba.se.spi.orb.PropertyParser ; import com.sun.corba.se.impl.orbutil.ORBConstants ; import com.sun.corba.se.impl.orbutil.ORBUtility; public abstract class DataCollectorBase implements DataCollector { private PropertyParser parser ; private Set propertyNames ; private Set propertyPrefixes ; private Set URLPropertyNames ; protected String localHostName ; protected String configurationHostName ; private boolean setParserCalled ; private Properties originalProps ; private Properties resultProps ; public DataCollectorBase( Properties props, String localHostName, String configurationHostName ) { // XXX This is fully initialized here. So do we ever want to // generalize this (or perhaps this is the wrong place for this?) URLPropertyNames = new HashSet() ; URLPropertyNames.add( ORBConstants.INITIAL_SERVICES_PROPERTY ) ; propertyNames = new HashSet() ; // Make sure that we are ready to handle -ORBInitRef. This is special // due to the need to handle multiple -ORBInitRef args as prefix // parsing. propertyNames.add( ORBConstants.ORB_INIT_REF_PROPERTY ) ; propertyPrefixes = new HashSet() ; this.originalProps = props ; this.localHostName = localHostName ; this.configurationHostName = configurationHostName ; setParserCalled = false ; resultProps = new Properties() ; } ////////////////////////////////////////////////////////// // Public interface defined in DataCollector ////////////////////////////////////////////////////////// public boolean initialHostIsLocal() { checkSetParserCalled() ; return localHostName.equals( resultProps.getProperty( ORBConstants.INITIAL_HOST_PROPERTY ) ) ; } public void setParser( PropertyParser parser ) { Iterator iter = parser.iterator() ; while (iter.hasNext()) { ParserAction pa = (ParserAction)(iter.next()) ; if (pa.isPrefix()) propertyPrefixes.add( pa.getPropertyName() ) ; else propertyNames.add( pa.getPropertyName() ) ; } collect() ; setParserCalled = true ; } public Properties getProperties() { checkSetParserCalled() ; return resultProps ; } ////////////////////////////////////////////////////////// // public interface from DataCollector that must be defined // in subclasses ////////////////////////////////////////////////////////// public abstract boolean isApplet() ; ////////////////////////////////////////////////////////// // Implementation methods needed in subclasses ////////////////////////////////////////////////////////// protected abstract void collect() ; ////////////////////////////////////////////////////////// // methods for use by subclasses ////////////////////////////////////////////////////////// protected void checkPropertyDefaults() { String host = resultProps.getProperty( ORBConstants.INITIAL_HOST_PROPERTY ) ; if ((host == null) || (host.equals(""))) setProperty( ORBConstants.INITIAL_HOST_PROPERTY, configurationHostName ); String serverHost = resultProps.getProperty( ORBConstants.SERVER_HOST_PROPERTY ) ; if (serverHost == null || serverHost.equals("") || serverHost.equals("0.0.0.0") || serverHost.equals("::") || serverHost.toLowerCase().equals("::ffff:0.0.0.0")) { setProperty(ORBConstants.SERVER_HOST_PROPERTY, localHostName); setProperty(ORBConstants.LISTEN_ON_ALL_INTERFACES, ORBConstants.LISTEN_ON_ALL_INTERFACES); } } protected void findPropertiesFromArgs( String[] params ) { if (params == null) return; // All command-line args are of the form "-ORBkey value". // The key is mapped to .ORBkey. String name ; String value ; for ( int i=0; i= StringTokenizer st = new StringTokenizer( value, "=" ) ; if (st.countTokens() != 2) throw new IllegalArgumentException() ; String refName = st.nextToken() ; String refValue = st.nextToken() ; resultProps.setProperty( name + "." + refName, refValue ) ; } else { resultProps.setProperty( name, value ) ; } } private void checkSetParserCalled() { if (!setParserCalled) throw new IllegalStateException( "setParser not called." ) ; } // For each prefix in prefixes, For each name in propertyNames, // if (prefix is a prefix of name) get value from getProperties and // setProperty (name, value). private void findPropertiesByPrefix( Set prefixes, Iterator propertyNames, PropertyCallback getProperty ) { while (propertyNames.hasNext()) { String name = (String)(propertyNames.next()) ; Iterator iter = prefixes.iterator() ; while (iter.hasNext()) { String prefix = (String)(iter.next()) ; if (name.startsWith( prefix )) { String value = getProperty.get( name ) ; // Note: do a put even if value is null since just // the presence of the property may be significant. setProperty( name, value ) ; } } } } // For each prefix in names, get the corresponding property // value from the callback, and store the name/value pair in // the result. private void findPropertiesByName( Iterator names, PropertyCallback getProperty ) { while (names.hasNext()) { String name = (String)(names.next()) ; String value = getProperty.get( name ) ; if (value != null) setProperty( name, value ) ; } } private static String getSystemProperty(final String name) { return (String)AccessController.doPrivileged( new GetPropertyAction(name)); } // Map command-line arguments to ORB properties. // private String findMatchingPropertyName( Set names, String suffix ) { Iterator iter = names.iterator() ; while (iter.hasNext()) { String name = (String)(iter.next()) ; if (name.endsWith( suffix )) return name ; } return null ; } private static Iterator makeIterator( final Enumeration enumeration ) { return new Iterator() { public boolean hasNext() { return enumeration.hasMoreElements() ; } public Object next() { return enumeration.nextElement() ; } public void remove() { throw new UnsupportedOperationException() ; } } ; } private static Iterator getSystemPropertyNames() { // This will not throw a SecurityException because this // class was loaded from rt.jar using the bootstrap classloader. Enumeration enumeration = (Enumeration) AccessController.doPrivileged( new PrivilegedAction() { public java.lang.Object run() { return System.getProperties().propertyNames(); } } ); return makeIterator( enumeration ) ; } private void getPropertiesFromFile( Properties props, String fileName ) { try { File file = new File( fileName ) ; if (!file.exists()) return ; FileInputStream in = new FileInputStream( file ) ; try { props.load( in ) ; } finally { in.close() ; } } catch (Exception exc) { // if (ORBInitDebug) // dprint( "ORB properties file " + fileName + " not found: " + // exc) ; } } private Properties getFileProperties() { Properties defaults = new Properties() ; String javaHome = getSystemProperty( "java.home" ) ; String fileName = javaHome + File.separator + "lib" + File.separator + "orb.properties" ; getPropertiesFromFile( defaults, fileName ) ; Properties results = new Properties( defaults ) ; String userHome = getSystemProperty( "user.home" ) ; fileName = userHome + File.separator + "orb.properties" ; getPropertiesFromFile( results, fileName ) ; return results ; } private boolean hasCORBAPrefix( String prefix ) { return prefix.startsWith( ORBConstants.ORG_OMG_PREFIX ) || prefix.startsWith( ORBConstants.SUN_PREFIX ) || prefix.startsWith( ORBConstants.SUN_LC_PREFIX ) || prefix.startsWith( ORBConstants.SUN_LC_VERSION_PREFIX ) ; } // Return only those element of prefixes for which hasCORBAPrefix // is true. private Set getCORBAPrefixes( final Set prefixes ) { Set result = new HashSet() ; Iterator iter = prefixes.iterator() ; while (iter.hasNext()) { String element = (String)(iter.next()) ; if (hasCORBAPrefix( element )) result.add( element ) ; } return result ; } } // Used to collect properties from various sources. abstract class PropertyCallback { abstract public String get(String name); }