geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xiam...@apache.org
Subject svn commit: r1336013 - in /geronimo/bundles/trunk/bval-jsr303: pom.xml src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java
Date Wed, 09 May 2012 09:15:50 GMT
Author: xiaming
Date: Wed May  9 09:15:49 2012
New Revision: 1336013

URL: http://svn.apache.org/viewvc?rev=1336013&view=rev
Log:
GERONIMO-6345 Add hacked ValidationParser logic back

Added:
    geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java
      - copied, changed from r1335570, geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java
Modified:
    geronimo/bundles/trunk/bval-jsr303/pom.xml

Modified: geronimo/bundles/trunk/bval-jsr303/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/bundles/trunk/bval-jsr303/pom.xml?rev=1336013&r1=1336012&r2=1336013&view=diff
==============================================================================
--- geronimo/bundles/trunk/bval-jsr303/pom.xml (original)
+++ geronimo/bundles/trunk/bval-jsr303/pom.xml Wed May  9 09:15:49 2012
@@ -90,10 +90,10 @@
                             org.apache.bval.xml.*;resolution:=optional,*</Import-Package>
                         <!-- geronimo spec osgi services support -->
                         <SPI-Provider>true</SPI-Provider>
-                        <!--<Include-Resource>
+                        <Include-Resource>
                            {maven-resources},
                            org/apache/bval/jsr303/xml/ValidationParser.class=target/classes/org/apache/bval/jsr303/xml/ValidationParser.class
-                        </Include-Resource>-->
+                        </Include-Resource>
                     </instructions>
                 </configuration>
             </plugin>

Copied: geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java
(from r1335570, geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java)
URL: http://svn.apache.org/viewvc/geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java?p2=geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java&p1=geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java&r1=1335570&r2=1336013&rev=1336013&view=diff
==============================================================================
--- geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java
(original)
+++ geronimo/bundles/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java
Wed May  9 09:15:49 2012
@@ -19,11 +19,11 @@
 package org.apache.bval.jsr303.xml;
 
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.StringReader;
-import java.net.URL;
-import java.util.Enumeration;
+import org.apache.bval.jsr303.ConfigurationImpl;
+import org.apache.bval.jsr303.util.IOUtils;
+import org.apache.bval.jsr303.util.SecureActions;
+import org.apache.bval.util.PrivilegedActions;
+import org.xml.sax.SAXException;
 
 import javax.validation.ConstraintValidatorFactory;
 import javax.validation.MessageInterpolator;
@@ -39,14 +39,15 @@ import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
-
-import org.apache.bval.jsr303.ConfigurationImpl;
-import org.apache.bval.jsr303.util.IOUtils;
-import org.apache.bval.jsr303.util.SecureActions;
-import org.apache.bval.util.PrivilegedActions;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.SAXException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Enumeration;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Description: uses jaxb to parse validation.xml<br/>
@@ -54,19 +55,19 @@ import org.xml.sax.SAXException;
 @SuppressWarnings("restriction")
 public class ValidationParser {
     private static final String DEFAULT_VALIDATION_XML_FILE = "META-INF/validation.xml";
-
-    private static final String VALIDATION_CONFIGURATION_XSD = "META-INF/validation-configuration-1.0.xsd";
+    private static final String VALIDATION_CONFIGURATION_XSD =
+            "META-INF/validation-configuration-1.0.xsd";
     private static final String VALIDATION_MAPPING_XSD = "META-INF/validation-mapping-1.0.xsd";
-
-    private static final Logger log = LoggerFactory.getLogger(ValidationParser.class);
-    private final String validationXmlFile;
+    private static final Logger log = Logger.getLogger(ValidationParser.class.getName());
+    protected final String validationXmlFile;
 
     /**
      * Create a new ValidationParser instance.
+     *
      * @param file
      */
     public ValidationParser(String file) {
-        if(file == null) {
+        if (file == null) {
             validationXmlFile = DEFAULT_VALIDATION_XML_FILE;
         } else {
             validationXmlFile = file;
@@ -75,6 +76,7 @@ public class ValidationParser {
 
     /**
      * Process the validation configuration into <code>targetConfig</code>.
+     *
      * @param targetConfig
      */
     public void processValidationConfig(ConfigurationImpl targetConfig) {
@@ -89,11 +91,11 @@ public class ValidationParser {
         try {
             inputStream = getInputStream(validationXmlFile);
             if (inputStream == null) {
-                log.debug("No {} found. Using annotation based configuration only.", validationXmlFile);
+            	log.log(Level.FINEST, String.format("No %s found. Using annotation based configuration
only.", validationXmlFile));
                 return null;
             }
 
-            log.debug("{} found.", validationXmlFile);
+            log.log(Level.FINEST, String.format("%s found.", validationXmlFile));
 
             Schema schema = getSchema(new ValidationConfigurationSchemaSource());
             JAXBContext jc = JAXBContext.newInstance(ValidationConfigType.class);
@@ -101,7 +103,7 @@ public class ValidationParser {
             unmarshaller.setSchema(schema);
             StreamSource stream = new StreamSource(inputStream);
             JAXBElement<ValidationConfigType> root =
-                  unmarshaller.unmarshal(stream, ValidationConfigType.class);
+                    unmarshaller.unmarshal(stream, ValidationConfigType.class);
             return root.getValue();
         } catch (JAXBException e) {
             throw new ValidationException("Unable to parse " + validationXmlFile, e);
@@ -112,24 +114,27 @@ public class ValidationParser {
         }
     }
 
-    private InputStream getInputStream(String path) throws IOException {
+    protected InputStream getInputStream(String path) throws IOException {
         ClassLoader loader = PrivilegedActions.getClassLoader(getClass());
-        InputStream inputStream = loader.getResourceAsStream( path );
+        InputStream inputStream = loader.getResourceAsStream(path);
 
-        if ( inputStream != null ) {
+        if (inputStream != null) {
             // spec says: If more than one META-INF/validation.xml file
             // is found in the classpath, a ValidationException is raised.
-            if ( path.equals("META-INF/validation.xml") ) {
-                Enumeration<URL> urls = loader.getResources(path);
-                if ( urls.hasMoreElements() && (urls.nextElement() != null) &&
urls.hasMoreElements() ) {
-                    throw new ValidationException("More than one " + path + " is found in
the classpath");
+            Enumeration<URL> urls = loader.getResources(path);
+            if (urls.hasMoreElements()) {
+                String url = urls.nextElement().toString();
+                while (urls.hasMoreElements()) {
+                    if (!url.equals(urls.nextElement().toString())) { // complain when first
duplicate found
+                        throw new ValidationException("More than one " + path + " is found
in the classpath");
+                    }
                 }
             }
         }
 
         return inputStream;
     }
-
+    
     /**
      * Get a Schema object from the specified resource name.
      * @return {@link Schema}
@@ -145,6 +150,10 @@ public class ValidationParser {
         }
     }
 
+    private Schema getSchema() {
+        return getSchema(VALIDATION_CONFIGURATION_XSD);
+    }
+
     /**
      * Get a Schema object from the specified resource name.
      * @param xsd
@@ -164,7 +173,7 @@ public class ValidationParser {
         try {
             return sf.newSchema(schemaUrl);
         } catch (SAXException e) {
-            log.warn("Unable to parse schema: " + xsd, e);
+            log.log(Level.WARNING, String.format("Unable to parse schema: %s", xsd), e);
             return null;
         }
     }
@@ -180,9 +189,8 @@ public class ValidationParser {
 
     private void applyProperties(ValidationConfigType xmlConfig, ConfigurationImpl target)
{
         for (PropertyType property : xmlConfig.getProperty()) {
-            if (log.isDebugEnabled()) {
-                log.debug("Found property '" + property.getName() + "' with value '" +
-                      property.getValue() + "' in " + validationXmlFile);
+            if (log.isLoggable(Level.FINEST)) {
+                log.log(Level.FINEST, String.format("Found property '%s' with value '%s'
in %s", property.getName(), property.getValue(), validationXmlFile));
             }
             target.addProperty(property.getName(), property.getValue());
         }
@@ -193,10 +201,9 @@ public class ValidationParser {
         String providerClassName = xmlConfig.getDefaultProvider();
         if (providerClassName != null) {
             Class<? extends ValidationProvider<?>> clazz =
-                  (Class<? extends ValidationProvider<?>>) SecureActions
-                        .loadClass(providerClassName, this.getClass());
+                    (Class<? extends ValidationProvider<?>>) loadClass(providerClassName);
             target.setProviderClass(clazz);
-            log.info("Using {} as validation provider.", providerClassName);
+            log.log(Level.INFO, String.format("Using %s as validation provider.", providerClassName));
         }
     }
 
@@ -204,12 +211,12 @@ public class ValidationParser {
     private void applyMessageInterpolator(ValidationConfigType xmlConfig,
                                           ConfigurationImpl target) {
         String messageInterpolatorClass = xmlConfig.getMessageInterpolator();
-        if ( target.getMessageInterpolator() == null ) {
+        if (target.getMessageInterpolator() == null) {
             if (messageInterpolatorClass != null) {
-                Class<MessageInterpolator> clazz = (Class<MessageInterpolator>)
SecureActions
-                      .loadClass(messageInterpolatorClass, this.getClass());
-                target.messageInterpolator(SecureActions.newInstance(clazz));
-                log.info("Using {} as message interpolator.", messageInterpolatorClass);
+                Class<MessageInterpolator> clazz = (Class<MessageInterpolator>)
+                        loadClass(messageInterpolatorClass);
+                target.messageInterpolator(newInstance(clazz));
+                log.log(Level.INFO, String.format("Using %s as message interpolator.", messageInterpolatorClass));
             }
         }
     }
@@ -218,27 +225,38 @@ public class ValidationParser {
     private void applyTraversableResolver(ValidationConfigType xmlConfig,
                                           ConfigurationImpl target) {
         String traversableResolverClass = xmlConfig.getTraversableResolver();
-        if ( target.getTraversableResolver() == null ) {
+        if (target.getTraversableResolver() == null) {
             if (traversableResolverClass != null) {
-                Class<TraversableResolver> clazz = (Class<TraversableResolver>)
SecureActions
-                      .loadClass(traversableResolverClass, this.getClass());
-                target.traversableResolver(SecureActions.newInstance(clazz));
-                log.info("Using {} as traversable resolver.", traversableResolverClass);
+                Class<TraversableResolver> clazz = (Class<TraversableResolver>)
+                        loadClass(traversableResolverClass);
+                target.traversableResolver(newInstance(clazz));
+                log.log(Level.INFO, String.format("Using %s as traversable resolver.", traversableResolverClass));
             }
         }
     }
 
+    private <T> T newInstance(final Class<T> cls) {
+        return AccessController.doPrivileged(new PrivilegedAction<T>() {
+            public T run() {
+                try {
+                    return cls.newInstance();
+                } catch (final Exception ex) {
+                    throw new ValidationException("Cannot instantiate : " + cls, ex);
+                }
+            }
+        });
+    }
+
     @SuppressWarnings("unchecked")
     private void applyConstraintFactory(ValidationConfigType xmlConfig,
                                         ConfigurationImpl target) {
         String constraintFactoryClass = xmlConfig.getConstraintValidatorFactory();
-        if ( target.getConstraintValidatorFactory() == null ) {
+        if (target.getConstraintValidatorFactory() == null) {
             if (constraintFactoryClass != null) {
-                Class<ConstraintValidatorFactory> clazz =
-                      (Class<ConstraintValidatorFactory>) SecureActions
-                            .loadClass(constraintFactoryClass, this.getClass());
-                target.constraintValidatorFactory(SecureActions.newInstance(clazz));
-                log.info("Using {} as constraint factory.", constraintFactoryClass);
+                Class<ConstraintValidatorFactory> clazz = (Class<ConstraintValidatorFactory>)
+                        loadClass(constraintFactoryClass);
+                target.constraintValidatorFactory(newInstance(clazz));
+                log.log(Level.INFO, String.format("Using %s as constraint factory.", constraintFactoryClass));
             }
         }
     }
@@ -247,27 +265,48 @@ public class ValidationParser {
                                      ConfigurationImpl target) {
         for (JAXBElement<String> mappingFileNameElement : xmlConfig.getConstraintMapping())
{
             String mappingFileName = mappingFileNameElement.getValue();
-            if ( mappingFileName.startsWith("/") ) {
+            if (mappingFileName.startsWith("/")) {
                 // Classloader needs a path without a starting /
                 mappingFileName = mappingFileName.substring(1);
             }
-            log.debug("Trying to open input stream for {}", mappingFileName);
+            log.log(Level.FINEST, String.format("Trying to open input stream for %s", mappingFileName));
             InputStream in = null;
             try {
                 in = getInputStream(mappingFileName);
                 if (in == null) {
                     throw new ValidationException(
-                          "Unable to open input stream for mapping file " +
-                                mappingFileName);
+                            "Unable to open input stream for mapping file " +
+                                    mappingFileName);
                 }
             } catch (IOException e) {
                 throw new ValidationException("Unable to open input stream for mapping file
" +
-                      mappingFileName, e);
+                        mappingFileName, e);
             }
             target.addMapping(in);
         }
     }
 
+
+    private static <T> T doPrivileged(final PrivilegedAction<T> action) {
+        if (System.getSecurityManager() != null) {
+            return AccessController.doPrivileged(action);
+        } else {
+            return action.run();
+        }
+    }
+
+    private Class<?> loadClass(final String className) {
+        ClassLoader loader = doPrivileged(SecureActions.getContextClassLoader());
+        if (loader == null)
+            loader = getClass().getClassLoader();
+
+        try {
+            return Class.forName(className, true, loader);
+        } catch (ClassNotFoundException ex) {
+            throw new ValidationException("Unable to load class: " + className, ex);
+        }
+    }
+    
     private static final class ValidationConfigurationSchemaSource extends StreamSource {
 
         private ValidationConfigurationSchemaSource() {
@@ -304,7 +343,7 @@ public class ValidationParser {
                     "</xs:schema>";
         }
     }
-
+    
     private static final class ValidationMappingSchemaSource extends StreamSource {
 
         private ValidationMappingSchemaSource() {
@@ -467,4 +506,5 @@ public class ValidationParser {
                     "</xs:schema>";
         }
     }
+
 }



Mime
View raw message