Author: epbernard
Date: 2009-03-11 09:53:07 -0400 (Wed, 11 Mar 2009)
New Revision: 16144
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java
Removed:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
Log:
BVAL-135 remove DefaultValidationProviderResolver from the API
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java 2009-03-11
13:09:25 UTC (rev 16143)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java 2009-03-11
13:53:07 UTC (rev 16144)
@@ -17,7 +17,16 @@
*/
package javax.validation;
-import javax.validation.bootstrap.DefaultValidationProviderResolver;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.net.URL;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.IOException;
import javax.validation.bootstrap.GenericBootstrap;
import javax.validation.bootstrap.ProviderSpecificBootstrap;
import javax.validation.spi.BootstrapState;
@@ -32,7 +41,7 @@
* <pre>
* ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
* </pre>
- * In this case {@link javax.validation.bootstrap.DefaultValidationProviderResolver}
+ * In this case, the default validation provider resolver
* will be used to locate available providers.
*
* The chosen provider is defined as followed:
@@ -86,7 +95,6 @@
*
* @author Emmanuel Bernard
* @author Hardy Feretnschik
- * @see DefaultValidationProviderResolver
*/
public class Validation {
@@ -95,8 +103,8 @@
* default Bean Validation provider and following the
* XML configuration.
* <p/>
- * The provider list is resolved using the
- * {@link javax.validation.bootstrap.DefaultValidationProviderResolver}.
+ * The provider list is resolved using the default validation provider resolver
+ * logic.
* <p/> The code is semantically equivalent to
*
<code>Validation.byDefaultProvider().configure().buildValidatorFactory()</code>
*
@@ -231,4 +239,108 @@
return resolver.getValidationProviders().get( 0 ).createGenericConfiguration( this );
}
}
+
+ /**
+ * Find <code>ValidationProvider</code> according to the default
<code>ValidationProviderResolver</code> defined in the
+ * Bean Validation specification. This implementation uses the current classloader or
the classloader which has loaded
+ * the current class if the current class loader is unavailable. The classloader is used
to retrieve the Service Provider files.
+ * <p>
+ * This class implements the Service Provider pattern described <a
href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service...;.
+ * Since we cannot rely on Java 6 we have to reimplement the
<code>Service</code> functionality.
+ * </p>
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+ private static class DefaultValidationProviderResolver implements
ValidationProviderResolver {
+
+ //cache per classloader for an appropriate discovery
+ //keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
+ //TODO use a WeakConcurrentHashMap
+ private static final Map<ClassLoader, List<ValidationProvider>>
providersPerClassloader =
+ new WeakHashMap<ClassLoader, List<ValidationProvider>>();
+
+ private static final String SERVICES_FILE = "META-INF/services/" +
ValidationProvider.class.getName();
+
+ public List<ValidationProvider> getValidationProviders() {
+ ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+ if ( classloader == null ) {
+ classloader = DefaultValidationProviderResolver.class.getClassLoader();
+ }
+
+ List<ValidationProvider> providers;
+ synchronized ( providersPerClassloader ) {
+ providers = providersPerClassloader.get( classloader );
+ }
+
+ if ( providers == null ) {
+ providers = new ArrayList<ValidationProvider>();
+ String name = null;
+ try {
+ Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE
);
+ while ( providerDefinitions.hasMoreElements() ) {
+ URL url = providerDefinitions.nextElement();
+ InputStream stream = url.openStream();
+ try {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100
);
+ name = reader.readLine();
+ while ( name != null ) {
+ name = name.trim();
+ if ( !name.startsWith( "#" ) ) {
+ final Class<?> providerClass = loadClass(
+ name,
+ DefaultValidationProviderResolver.class
+ );
+
+ providers.add(
+ ( ValidationProvider ) providerClass.newInstance()
+ );
+ }
+ name = reader.readLine();
+ }
+ }
+ finally {
+ stream.close();
+ }
+ }
+ }
+ catch ( IOException e ) {
+ throw new ValidationException( "Unable to read " + SERVICES_FILE, e );
+ }
+ catch ( ClassNotFoundException e ) {
+ //TODO is it better to not fail the whole loading because of a black sheep?
+ throw new ValidationException( "Unable to load Bean Validation provider "
+ name, e );
+ }
+ catch ( IllegalAccessException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation
provider" + name, e );
+ }
+ catch ( InstantiationException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation
provider" + name, e );
+ }
+
+ synchronized ( providersPerClassloader ) {
+ providersPerClassloader.put( classloader, providers );
+ }
+ }
+
+ return providers;
+ }
+
+ private static Class<?> loadClass(String name, Class caller) throws
ClassNotFoundException {
+ try {
+ //try context classloader, if fails try caller classloader
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if ( loader != null ) {
+ return loader.loadClass( name );
+ }
+ }
+ catch ( ClassNotFoundException e ) {
+ //trying caller classloader
+ if ( caller == null ) {
+ throw e;
+ }
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+ }
}
Deleted:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java 2009-03-11
13:09:25 UTC (rev 16143)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java 2009-03-11
13:53:07 UTC (rev 16144)
@@ -1,136 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
http://www.apache.org/licenses/LICENSE-2.0
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-package javax.validation.bootstrap;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
-import java.util.WeakHashMap;
-import javax.validation.ValidationException;
-import javax.validation.ValidationProviderResolver;
-import javax.validation.spi.ValidationProvider;
-
-/**
- * Find <code>ValidationProvider</code> according to the default
<code>ValidationProviderResolver</code> defined in the
- * Bean Validation specification. This implementation uses the current classloader or the
classloader which has loaded
- * the current class if the current class loader is unavailable. The classloader is used
to retrieve the Service Provider files.
- * <p>
- * This class implements the Service Provider pattern described <a
href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service...;.
- * Since we cannot rely on Java 6 we have to reimplement the
<code>Service</code> functionality.
- * </p>
- *
- * @author Emmanuel Bernard
- * @author Hardy Ferentschik
- */
-public class DefaultValidationProviderResolver implements ValidationProviderResolver {
-
- //cache per classloader for an appropriate discovery
- //keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
- //TODO use a WeakConcurrentHashMap
- private static final Map<ClassLoader, List<ValidationProvider>>
providersPerClassloader =
- new WeakHashMap<ClassLoader, List<ValidationProvider>>();
-
- private static final String SERVICES_FILE = "META-INF/services/" +
ValidationProvider.class.getName();
-
- public List<ValidationProvider> getValidationProviders() {
- ClassLoader classloader = Thread.currentThread().getContextClassLoader();
- if ( classloader == null ) {
- classloader = DefaultValidationProviderResolver.class.getClassLoader();
- }
-
- List<ValidationProvider> providers;
- synchronized ( providersPerClassloader ) {
- providers = providersPerClassloader.get( classloader );
- }
-
- if ( providers == null ) {
- providers = new ArrayList<ValidationProvider>();
- String name = null;
- try {
- Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE
);
- while ( providerDefinitions.hasMoreElements() ) {
- URL url = providerDefinitions.nextElement();
- InputStream stream = url.openStream();
- try {
- BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100
);
- name = reader.readLine();
- while ( name != null ) {
- name = name.trim();
- if ( !name.startsWith( "#" ) ) {
- final Class<?> providerClass = loadClass(
- name,
- DefaultValidationProviderResolver.class
- );
-
- providers.add(
- ( ValidationProvider ) providerClass.newInstance()
- );
- }
- name = reader.readLine();
- }
- }
- finally {
- stream.close();
- }
- }
- }
- catch ( IOException e ) {
- throw new ValidationException( "Unable to read " + SERVICES_FILE, e );
- }
- catch ( ClassNotFoundException e ) {
- //TODO is it better to not fail the whole loading because of a black sheep?
- throw new ValidationException( "Unable to load Bean Validation provider " +
name, e );
- }
- catch ( IllegalAccessException e ) {
- throw new ValidationException( "Unable to instanciate Bean Validation
provider" + name, e );
- }
- catch ( InstantiationException e ) {
- throw new ValidationException( "Unable to instanciate Bean Validation
provider" + name, e );
- }
-
- synchronized ( providersPerClassloader ) {
- providersPerClassloader.put( classloader, providers );
- }
- }
-
- return providers;
- }
-
- public static Class<?> loadClass(String name, Class caller) throws
ClassNotFoundException {
- try {
- //try context classloader, if fails try caller classloader
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if ( loader != null ) {
- return loader.loadClass( name );
- }
- }
- catch ( ClassNotFoundException e ) {
- //trying caller classloader
- if ( caller == null ) {
- throw e;
- }
- }
- return Class.forName( name, true, caller.getClassLoader() );
- }
-}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11
13:09:25 UTC (rev 16143)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11
13:53:07 UTC (rev 16144)
@@ -28,7 +28,6 @@
import javax.validation.ValidationException;
import javax.validation.ValidationProviderResolver;
import javax.validation.ValidatorFactory;
-import javax.validation.bootstrap.DefaultValidationProviderResolver;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;
import javax.validation.spi.ValidationProvider;
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java 2009-03-11
13:53:07 UTC (rev 16144)
@@ -0,0 +1,121 @@
+package org.hibernate.validation.engine;
+
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.net.URL;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import javax.validation.ValidationProviderResolver;
+import javax.validation.ValidationException;
+import javax.validation.spi.ValidationProvider;
+
+/**
+ * Find <code>ValidationProvider</code> according to the default
<code>ValidationProviderResolver</code> defined in the
+ * Bean Validation specification. This implementation uses the current classloader or the
classloader which has loaded
+ * the current class if the current class loader is unavailable. The classloader is used
to retrieve the Service Provider files.
+ * <p>
+ * This class implements the Service Provider pattern described <a
href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service...;.
+ * Since we cannot rely on Java 6 we have to reimplement the
<code>Service</code> functionality.
+ * </p>
+ *
+ * Duplicated from the private innner class Validation#DefaultValidationProviderResolver
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class DefaultValidationProviderResolver implements ValidationProviderResolver {
+
+ //cache per classloader for an appropriate discovery
+ //keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
+ //TODO use a WeakConcurrentHashMap
+ private static final Map<ClassLoader, List<ValidationProvider>>
providersPerClassloader =
+ new WeakHashMap<ClassLoader, List<ValidationProvider>>();
+
+ private static final String SERVICES_FILE = "META-INF/services/" +
ValidationProvider.class.getName();
+
+ public List<ValidationProvider> getValidationProviders() {
+ ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+ if ( classloader == null ) {
+ classloader = DefaultValidationProviderResolver.class.getClassLoader();
+ }
+
+ List<ValidationProvider> providers;
+ synchronized ( providersPerClassloader ) {
+ providers = providersPerClassloader.get( classloader );
+ }
+
+ if ( providers == null ) {
+ providers = new ArrayList<ValidationProvider>();
+ String name = null;
+ try {
+ Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE
);
+ while ( providerDefinitions.hasMoreElements() ) {
+ URL url = providerDefinitions.nextElement();
+ InputStream stream = url.openStream();
+ try {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100
);
+ name = reader.readLine();
+ while ( name != null ) {
+ name = name.trim();
+ if ( !name.startsWith( "#" ) ) {
+ final Class<?> providerClass = loadClass(
+ name,
+ DefaultValidationProviderResolver.class
+ );
+
+ providers.add(
+ ( ValidationProvider ) providerClass.newInstance()
+ );
+ }
+ name = reader.readLine();
+ }
+ }
+ finally {
+ stream.close();
+ }
+ }
+ }
+ catch ( IOException e ) {
+ throw new ValidationException( "Unable to read " + SERVICES_FILE, e );
+ }
+ catch ( ClassNotFoundException e ) {
+ //TODO is it better to not fail the whole loading because of a black sheep?
+ throw new ValidationException( "Unable to load Bean Validation provider " +
name, e );
+ }
+ catch ( IllegalAccessException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation
provider" + name, e );
+ }
+ catch ( InstantiationException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation
provider" + name, e );
+ }
+
+ synchronized ( providersPerClassloader ) {
+ providersPerClassloader.put( classloader, providers );
+ }
+ }
+
+ return providers;
+ }
+
+ private static Class<?> loadClass(String name, Class caller) throws
ClassNotFoundException {
+ try {
+ //try context classloader, if fails try caller classloader
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if ( loader != null ) {
+ return loader.loadClass( name );
+ }
+ }
+ catch ( ClassNotFoundException e ) {
+ //trying caller classloader
+ if ( caller == null ) {
+ throw e;
+ }
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+}