neilg       2003/01/24 09:20:12

  Modified:    java/src/org/apache/xerces/util ObjectFactory.java
                        SecuritySupport.java SecuritySupport12.java
  Log:
  make ObjectFactory cachec xerces.properties, refreshing it when necessary, in like 
manner to the FactoryFinder implementation we will be relying upon.
  
  Revision  Changes    Path
  1.10      +76 -17    xml-xerces/java/src/org/apache/xerces/util/ObjectFactory.java
  
  Index: ObjectFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/util/ObjectFactory.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ObjectFactory.java        6 Jan 2003 16:37:59 -0000       1.9
  +++ ObjectFactory.java        24 Jan 2003 17:20:11 -0000      1.10
  @@ -96,6 +96,20 @@
       /** Set to true for debugging */
       private static final boolean DEBUG = false;
   
  +    /** cache the contents of the xerces.properties file.
  +     *  Until an attempt has been made to read this file, this will
  +     * be null; if the file does not exist or we encounter some other error
  +     * during the read, this will be empty.
  +     */
  +    private static Properties fXercesProperties = null;
  +
  +    /***
  +     * Cache the time stamp of the xerces.properties file so
  +     * that we know if it's been modified and can invalidate
  +     * the cache when necessary.
  +     */
  +    private static long fLastModified = -1;
  +
       //
       // Public static methods
       //
  @@ -167,25 +181,70 @@
           }
   
           // Try to read from propertiesFilename, or $java.home/lib/xerces.properties
  -        try {
  -            if(propertiesFilename ==null) {
  +        String factoryClassName = null;
  +        // no properties file name specified; use $JAVA_HOME/lib/xerces.properties:
  +        if (propertiesFilename == null) {
               String javah = ss.getSystemProperty("java.home");
  -                propertiesFilename = javah + File.separator +
  -                    "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME;
  +            propertiesFilename = javah + File.separator +
  +                "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME;
  +            File propertiesFile = new File(propertiesFilename);
  +            boolean propertiesFileExists = ss.getFileExists(propertiesFile);
  +            synchronized (ObjectFactory.class) {
  +                boolean loadProperties = false;
  +                // file existed last time
  +                if(fLastModified >= 0) {
  +                    if(propertiesFileExists &&
  +                            (fLastModified < (fLastModified = 
ss.getLastModified(propertiesFile)))) {
  +                        loadProperties = true;
  +                    } else {
  +                        // file has stopped existing...
  +                        if(!propertiesFileExists) {
  +                            fLastModified = -1;
  +                            fXercesProperties = null;
  +                        } // else, file wasn't modified!
  +                    }
  +                } else {
  +                    // file has started to exist:
  +                    if(propertiesFileExists) {
  +                        loadProperties = true;
  +                        fLastModified = ss.getLastModified(propertiesFile);
  +                    } // else, nothing's changed
  +                }
  +                if(loadProperties) {
  +                    try {
  +                        // must never have attempted to read xerces.properties 
before (or it's outdeated)
  +                        fXercesProperties = new Properties();
  +                        FileInputStream fis = ss.getFileInputStream(propertiesFile);
  +                        fXercesProperties.load(fis);
  +                        fis.close();
  +                     } catch (Exception x) {
  +                         fXercesProperties = null;
  +                         fLastModified = -1;
  +                         // assert(x instanceof FileNotFoundException
  +                         //        || x instanceof SecurityException)
  +                         // In both cases, ignore and continue w/ next location
  +                     }
  +                }
  +            }
  +            if(fXercesProperties != null) {
  +                factoryClassName = fXercesProperties.getProperty(factoryId);
               }
  -            FileInputStream fis = ss.getFileInputStream(new 
File(propertiesFilename));
  -            Properties props = new Properties();
  -            props.load(fis);
  -            String factoryClassName = props.getProperty(factoryId);
  -            if (factoryClassName != null) {
  -                debugPrintln("found in " + propertiesFilename + ", value=" + 
factoryClassName);
  -                return newInstance(factoryClassName, cl, true);
  +        } else {
  +            try {
  +                FileInputStream fis = ss.getFileInputStream(new 
File(propertiesFilename));
  +                Properties props = new Properties();
  +                props.load(fis);
  +                fis.close();
  +                factoryClassName = props.getProperty(factoryId);
  +            } catch (Exception x) {
  +                // assert(x instanceof FileNotFoundException
  +                //        || x instanceof SecurityException)
  +                // In both cases, ignore and continue w/ next location
               }
  -            fis.close();
  -        } catch (Exception x) {
  -            // assert(x instanceof FileNotFoundException
  -            //        || x instanceof SecurityException)
  -            // In both cases, ignore and continue w/ next location
  +        }
  +        if (factoryClassName != null) {
  +            debugPrintln("found in " + propertiesFilename + ", value=" + 
factoryClassName);
  +            return newInstance(factoryClassName, cl, true);
           }
   
           // Try Jar Service Provider Mechanism
  
  
  
  1.2       +8 -0      xml-xerces/java/src/org/apache/xerces/util/SecuritySupport.java
  
  Index: SecuritySupport.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/util/SecuritySupport.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SecuritySupport.java      26 Aug 2002 23:57:11 -0000      1.1
  +++ SecuritySupport.java      24 Jan 2003 17:20:11 -0000      1.2
  @@ -137,4 +137,12 @@
           }
           return ris;
       }
  +    
  +    public boolean getFileExists(File f) {
  +        return f.exists();
  +    }
  +    
  +    public long getLastModified(File f) {
  +        return f.lastModified();
  +    }    
   }
  
  
  
  1.2       +19 -0     
xml-xerces/java/src/org/apache/xerces/util/SecuritySupport12.java
  
  Index: SecuritySupport12.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/util/SecuritySupport12.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SecuritySupport12.java    26 Aug 2002 23:57:11 -0000      1.1
  +++ SecuritySupport12.java    24 Jan 2003 17:20:11 -0000      1.2
  @@ -122,4 +122,23 @@
                   }
               });
       }
  +    
  +    public boolean getFileExists(final File f) {
  +    return ((Boolean)
  +            AccessController.doPrivileged(new PrivilegedAction() {
  +                public Object run() {
  +                    return new Boolean(f.exists());
  +                }
  +            })).booleanValue();
  +    }
  +    
  +    public long getLastModified(final File f) {
  +    return ((Long)
  +            AccessController.doPrivileged(new PrivilegedAction() {
  +                public Object run() {
  +                    return new Long(f.lastModified());
  +                }
  +            })).longValue();
  +    }
  +        
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to