1 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 ; 13 import java.security.AccessController ; 14 15 import java.applet.Applet ; 16 17 import java.util.Properties ; 18 import java.util.Vector ; 19 import java.util.Set ; 20 import java.util.HashSet ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.StringTokenizer ; 24 25 import java.net.URL ; 26 27 import java.security.AccessController ; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 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 propertyNames ; 41 private Set propertyPrefixes ; 42 private Set URLPropertyNames ; 43 protected String localHostName ; 44 protected String configurationHostName ; 45 private boolean setParserCalled ; 46 private Properties originalProps ; 47 private Properties resultProps ; 48 49 public DataCollectorBase( Properties props, String localHostName, 50 String configurationHostName ) 51 { 52 URLPropertyNames = new HashSet () ; 55 URLPropertyNames.add( ORBConstants.INITIAL_SERVICES_PROPERTY ) ; 56 57 propertyNames = new HashSet () ; 58 59 propertyNames.add( ORBConstants.ORB_INIT_REF_PROPERTY ) ; 63 64 propertyPrefixes = new HashSet () ; 65 66 this.originalProps = props ; 67 this.localHostName = localHostName ; 68 this.configurationHostName = configurationHostName ; 69 setParserCalled = false ; 70 resultProps = new Properties () ; 71 } 72 73 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 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 getProperties() 100 { 101 checkSetParserCalled() ; 102 return resultProps ; 103 } 104 105 110 public abstract boolean isApplet() ; 111 112 116 protected abstract void collect() ; 117 118 122 protected void checkPropertyDefaults() 123 { 124 String 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 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 [] params ) 148 { 149 if (params == null) 150 return; 151 152 155 String name ; 156 String 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 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 app ) 179 { 180 if (app == null) 183 return; 184 185 PropertyCallback callback = new PropertyCallback() { 186 public String get(String name) { 187 return app.getParameter(name); 188 } 189 } ; 190 191 findPropertiesByName( propertyNames.iterator(), callback ) ; 192 193 PropertyCallback URLCallback = new PropertyCallback() { 200 public String get( String name ) { 201 String value = resultProps.getProperty(name); 202 if (value == null) 203 return null ; 204 205 try { 206 URL url = new URL ( app.getDocumentBase(), value ) ; 207 return url.toExternalForm() ; 208 } catch (java.net.MalformedURLException exc) { 209 return value ; 212 } 213 } 214 } ; 215 216 findPropertiesByName( URLPropertyNames.iterator(), 217 URLCallback ) ; 218 } 219 220 private void doProperties( final Properties props ) 221 { 222 PropertyCallback callback = new PropertyCallback() { 223 public String get(String 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 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 protected void findPropertiesFromSystem() 259 { 260 Set normalNames = getCORBAPrefixes( propertyNames ) ; 261 Set prefixNames = getCORBAPrefixes( propertyPrefixes ) ; 262 263 PropertyCallback callback = new PropertyCallback() { 264 public String get(String name) { 265 return getSystemProperty(name); 266 } 267 } ; 268 269 findPropertiesByName( normalNames.iterator(), callback ) ; 270 271 findPropertiesByPrefix( prefixNames, 272 getSystemPropertyNames(), callback ) ; 273 } 274 275 279 private void setProperty( String name, String value ) 283 { 284 if( name.equals( ORBConstants.ORB_INIT_REF_PROPERTY ) ) { 285 StringTokenizer st = new StringTokenizer ( value, "=" ) ; 287 if (st.countTokens() != 2) 288 throw new IllegalArgumentException () ; 289 290 String refName = st.nextToken() ; 291 String 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 ( "setParser not called." ) ; 303 } 304 305 private void findPropertiesByPrefix( Set prefixes, 309 Iterator propertyNames, PropertyCallback getProperty ) 310 { 311 while (propertyNames.hasNext()) { 312 String name = (String )(propertyNames.next()) ; 313 Iterator iter = prefixes.iterator() ; 314 while (iter.hasNext()) { 315 String prefix = (String )(iter.next()) ; 316 if (name.startsWith( prefix )) { 317 String value = getProperty.get( name ) ; 318 319 setProperty( name, value ) ; 322 } 323 } 324 } 325 } 326 327 private void findPropertiesByName( Iterator names, 331 PropertyCallback getProperty ) 332 { 333 while (names.hasNext()) { 334 String name = (String )(names.next()) ; 335 String value = getProperty.get( name ) ; 336 if (value != null) 337 setProperty( name, value ) ; 338 } 339 } 340 341 private static String getSystemProperty(final String name) 342 { 343 return (String )AccessController.doPrivileged( 344 new GetPropertyAction(name)); 345 } 346 347 private String findMatchingPropertyName( Set names, 350 String suffix ) 351 { 352 Iterator iter = names.iterator() ; 353 while (iter.hasNext()) { 354 String name = (String )(iter.next()) ; 355 if (name.endsWith( suffix )) 356 return name ; 357 } 358 359 return null ; 360 } 361 362 private static Iterator makeIterator( final Enumeration enumeration ) 363 { 364 return new Iterator () { 365 public boolean hasNext() { return enumeration.hasMoreElements() ; } 366 public Object next() { return enumeration.nextElement() ; } 367 public void remove() { throw new UnsupportedOperationException () ; } 368 } ; 369 } 370 371 private static Iterator getSystemPropertyNames() 372 { 373 Enumeration enumeration = (Enumeration ) 376 AccessController.doPrivileged( 377 new PrivilegedAction () { 378 public java.lang.Object run() { 379 return System.getProperties().propertyNames(); 380 } 381 } 382 ); 383 384 return makeIterator( enumeration ) ; 385 } 386 387 private void getPropertiesFromFile( Properties props, String fileName ) 388 { 389 try { 390 File file = new File ( fileName ) ; 391 if (!file.exists()) 392 return ; 393 394 FileInputStream in = new FileInputStream ( file ) ; 395 396 try { 397 props.load( in ) ; 398 } finally { 399 in.close() ; 400 } 401 } catch (Exception exc) { 402 } 406 } 407 408 private Properties getFileProperties() 409 { 410 Properties defaults = new Properties () ; 411 412 String javaHome = getSystemProperty( "java.home" ) ; 413 String fileName = javaHome + File.separator + "lib" + File.separator + 414 "orb.properties" ; 415 416 getPropertiesFromFile( defaults, fileName ) ; 417 418 Properties results = new Properties ( defaults ) ; 419 420 String 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 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 private Set getCORBAPrefixes( final Set prefixes ) 438 { 439 Set result = new HashSet () ; 440 Iterator iter = prefixes.iterator() ; 441 while (iter.hasNext()) { 442 String element = (String )(iter.next()) ; 443 if (hasCORBAPrefix( element )) 444 result.add( element ) ; 445 } 446 447 return result ; 448 } 449 } 450 451 abstract class PropertyCallback 453 { 454 abstract public String get(String name); 455 } 456 | Popular Tags |