1 /*
   2  * Copyright (c) 1999, 2001, 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 
  26 /*
  27  * (C) Copyright IBM Corp. 1993 - 1997 - All Rights Reserved
  28  *
  29  * The original version of this source code and documentation is
  30  * copyrighted and owned by IBM, Inc. These materials are provided under
  31  * terms of a License Agreement between IBM and Sun. This technology is
  32  * protected by multiple US and International patents. This notice and
  33  * attribution to IBM may not be removed.
  34  *
  35  */
  36 
  37 
  38 package javax.rmi.CORBA;
  39 
  40 import java.io.IOException;
  41 import java.io.File;
  42 import java.io.FileInputStream;
  43 
  44 import java.security.AccessController;
  45 import java.security.PrivilegedAction;
  46 import java.util.Properties;
  47 
  48 class GetORBPropertiesFileAction implements PrivilegedAction {
  49     private boolean debug = false ;
  50 
  51     public GetORBPropertiesFileAction () {
  52     }
  53 
  54     private String getSystemProperty(final String name) {
  55         // This will not throw a SecurityException because this
  56         // class was loaded from rt.jar using the bootstrap classloader.
  57         String propValue = (String) AccessController.doPrivileged(
  58             new PrivilegedAction() {
  59                 public java.lang.Object run() {
  60                     return System.getProperty(name);
  61                 }
  62             }
  63         );
  64 
  65         return propValue;
  66     }
  67 
  68     private void getPropertiesFromFile( Properties props, String fileName )
  69     {
  70         try {
  71             File file = new File( fileName ) ;
  72             if (!file.exists())
  73                 return ;
  74 
  75             FileInputStream in = new FileInputStream( file ) ;
  76 
  77             try {
  78                 props.load( in ) ;
  79             } finally {
  80                 in.close() ;
  81             }
  82         } catch (Exception exc) {
  83             if (debug)
  84                 System.out.println( "ORB properties file " + fileName +
  85                     " not found: " + exc) ;
  86         }
  87     }
  88 
  89     public Object run()
  90     {
  91         Properties defaults = new Properties() ;
  92 
  93         String javaHome = getSystemProperty( "java.home" ) ;
  94         String fileName = javaHome + File.separator + "lib" + File.separator +
  95             "orb.properties" ;
  96 
  97         getPropertiesFromFile( defaults, fileName ) ;
  98 
  99         Properties results = new Properties( defaults ) ;
 100 
 101         String userHome = getSystemProperty( "user.home" ) ;
 102         fileName = userHome + File.separator + "orb.properties" ;
 103 
 104         getPropertiesFromFile( results, fileName ) ;
 105         return results ;
 106     }
 107 }