jaxp/src/javax/xml/validation/SchemaFactoryFinder.java
changeset 12458 d601e4bba306
parent 12457 c348e06f0e82
child 14942 0dbd45cfc14b
equal deleted inserted replaced
12457:c348e06f0e82 12458:d601e4bba306
    41 
    41 
    42 /**
    42 /**
    43  * Implementation of {@link SchemaFactory#newInstance(String)}.
    43  * Implementation of {@link SchemaFactory#newInstance(String)}.
    44  *
    44  *
    45  * @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
    45  * @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
       
    46  * @version $Revision: 1.8 $, $Date: 2010-11-01 04:36:13 $
    46  * @since 1.5
    47  * @since 1.5
    47  */
    48  */
    48 class SchemaFactoryFinder  {
    49 class SchemaFactoryFinder  {
    49 
    50 
    50     /** debug support code. */
    51     /** debug support code. */
    59         private static Properties cacheProps = new Properties();
    60         private static Properties cacheProps = new Properties();
    60 
    61 
    61         /**
    62         /**
    62          * <p>First time requires initialization overhead.</p>
    63          * <p>First time requires initialization overhead.</p>
    63          */
    64          */
    64         private static boolean firstTime = true;
    65         private static volatile boolean firstTime = true;
    65 
    66 
    66     static {
    67     static {
    67         // Use try/catch block to support applets
    68         // Use try/catch block to support applets
    68         try {
    69         try {
    69             debug = ss.getSystemProperty("jaxp.debug") != null;
    70             debug = ss.getSystemProperty("jaxp.debug") != null;
   164         try {
   165         try {
   165             debugPrintln("Looking up system property '"+propertyName+"'" );
   166             debugPrintln("Looking up system property '"+propertyName+"'" );
   166             String r = ss.getSystemProperty(propertyName);
   167             String r = ss.getSystemProperty(propertyName);
   167             if(r!=null) {
   168             if(r!=null) {
   168                 debugPrintln("The value is '"+r+"'");
   169                 debugPrintln("The value is '"+r+"'");
   169                 sf = createInstance(r);
   170                 sf = createInstance(r, true);
   170                 if(sf!=null)    return sf;
   171                 if(sf!=null)    return sf;
   171             } else
   172             } else
   172                 debugPrintln("The property is undefined.");
   173                 debugPrintln("The property is undefined.");
   173         } catch( Throwable t ) {
   174         } catch( Throwable t ) {
   174             if( debug ) {
   175             if( debug ) {
   199             }
   200             }
   200             factoryClassName = cacheProps.getProperty(propertyName);
   201             factoryClassName = cacheProps.getProperty(propertyName);
   201             debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties");
   202             debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties");
   202 
   203 
   203             if (factoryClassName != null) {
   204             if (factoryClassName != null) {
   204                 sf = createInstance(factoryClassName);
   205                 sf = createInstance(factoryClassName, true);
   205                 if(sf != null){
   206                 if(sf != null){
   206                     return sf;
   207                     return sf;
   207                 }
   208                 }
   208             }
   209             }
   209         } catch (Exception ex) {
   210         } catch (Exception ex) {
   252         }
   253         }
   253 
   254 
   254         // platform default
   255         // platform default
   255         if(schemaLanguage.equals("http://www.w3.org/2001/XMLSchema")) {
   256         if(schemaLanguage.equals("http://www.w3.org/2001/XMLSchema")) {
   256             debugPrintln("attempting to use the platform default XML Schema validator");
   257             debugPrintln("attempting to use the platform default XML Schema validator");
   257             return createInstance("com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory");
   258             return createInstance("com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory", true);
   258         }
   259         }
   259 
   260 
   260         debugPrintln("all things were tried, but none was found. bailing out.");
   261         debugPrintln("all things were tried, but none was found. bailing out.");
   261         return null;
   262         return null;
   262     }
   263     }
   292      *
   293      *
   293      * @return null
   294      * @return null
   294      *      if it fails. Error messages will be printed by this method.
   295      *      if it fails. Error messages will be printed by this method.
   295      */
   296      */
   296     SchemaFactory createInstance( String className ) {
   297     SchemaFactory createInstance( String className ) {
       
   298         return createInstance( className, false );
       
   299     }
       
   300 
       
   301     SchemaFactory createInstance( String className, boolean useServicesMechanism ) {
   297         SchemaFactory schemaFactory = null;
   302         SchemaFactory schemaFactory = null;
   298 
   303 
   299         debugPrintln("createInstance(" + className + ")");
   304         debugPrintln("createInstance(" + className + ")");
   300 
   305 
   301         // get Class from className
   306         // get Class from className
   306         }
   311         }
   307         debugPrintln("loaded " + className + " from " + which(clazz));
   312         debugPrintln("loaded " + className + " from " + which(clazz));
   308 
   313 
   309         // instantiate Class as a SchemaFactory
   314         // instantiate Class as a SchemaFactory
   310         try {
   315         try {
   311                 schemaFactory = (SchemaFactory) clazz.newInstance();
   316             if (!useServicesMechanism) {
       
   317                 schemaFactory = (SchemaFactory) newInstanceNoServiceLoader(clazz);
       
   318             }
       
   319                 if (schemaFactory == null) {
       
   320                     schemaFactory = (SchemaFactory) clazz.newInstance();
       
   321                 }
   312         } catch (ClassCastException classCastException) {
   322         } catch (ClassCastException classCastException) {
   313                 debugPrintln("could not instantiate " + clazz.getName());
   323                 debugPrintln("could not instantiate " + clazz.getName());
   314                 if (debug) {
   324                 if (debug) {
   315                         classCastException.printStackTrace();
   325                         classCastException.printStackTrace();
   316                 }
   326                 }
   328                 }
   338                 }
   329                 return null;
   339                 return null;
   330         }
   340         }
   331 
   341 
   332         return schemaFactory;
   342         return schemaFactory;
       
   343     }
       
   344     /**
       
   345      * Try to construct using newTransformerFactoryNoServiceLoader
       
   346      *   method if available.
       
   347      */
       
   348     private static Object newInstanceNoServiceLoader(
       
   349          Class<?> providerClass
       
   350     ) {
       
   351         // Retain maximum compatibility if no security manager.
       
   352         if (System.getSecurityManager() == null) {
       
   353             return null;
       
   354         }
       
   355         try {
       
   356             Method creationMethod =
       
   357                 providerClass.getDeclaredMethod(
       
   358                     "newXMLSchemaFactoryNoServiceLoader"
       
   359                 );
       
   360                 return creationMethod.invoke(null, null);
       
   361             } catch (NoSuchMethodException exc) {
       
   362                 return null;
       
   363             } catch (Exception exc) {
       
   364                 return null;
       
   365         }
   333     }
   366     }
   334 
   367 
   335     /** Iterator that lazily computes one value and returns it. */
   368     /** Iterator that lazily computes one value and returns it. */
   336     private static abstract class SingleIterator implements Iterator {
   369     private static abstract class SingleIterator implements Iterator {
   337         private boolean seen = false;
   370         private boolean seen = false;