Author: hardy.ferentschik
Date: 2009-03-11 09:09:25 -0400 (Wed, 11 Mar 2009)
New Revision: 16143
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java
Modified:
validator/trunk/hibernate-validator/pom.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
Log:
HV-104 Added dependecy to SNAPSHOT release of JPA2 spec. Also added
JPATraversableResolver.
Removed obsolete AmbiguousConstraintUsageException.
Modified: validator/trunk/hibernate-validator/pom.xml
===================================================================
--- validator/trunk/hibernate-validator/pom.xml 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/pom.xml 2009-03-11 13:09:25 UTC (rev 16143)
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>hibernate-validator-parent</artifactId>
<groupId>org.hibernate</groupId>
@@ -28,13 +28,24 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
+ <!--
+ This is a temporary dependency. Do not use this dependecy in production.
+ -->
<dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>jpa-api</artifactId>
+ <version>2.0.Beta1-SNAPSHOT</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
@@ -125,7 +136,7 @@
</goals>
</execution>
</executions>
- </plugin>
+ </plugin>
</plugins>
</build>
<reporting>
Modified:
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml
===================================================================
---
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml 2009-03-11
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml 2009-03-11
13:09:25 UTC (rev 16143)
@@ -164,7 +164,6 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
- <version>RELEASE</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
Deleted:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java 2009-03-11
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -1,45 +0,0 @@
-// $Id: AmbiguousConstraintUsageException.java 15902 2009-02-05 11:53:49Z
hardy.ferentschik $
-/*
-* 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 org.hibernate.validation;
-
-import javax.validation.ValidationException;
-import javax.validation.UnexpectedTypeException;
-
-/**
- * Exception raised in the case that the constraint validator resolution returns more
than one possible validators for
- * a given type.
- *
- * @author Hardy Ferentschik
- */
-public class AmbiguousConstraintUsageException extends UnexpectedTypeException {
- public AmbiguousConstraintUsageException(String message) {
- super( message );
- }
-
- public AmbiguousConstraintUsageException() {
- super();
- }
-
- public AmbiguousConstraintUsageException(String message, Throwable cause) {
- super( message, cause );
- }
-
- public AmbiguousConstraintUsageException(Throwable cause) {
- super( cause );
- }
-}
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
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -34,6 +34,7 @@
import javax.validation.spi.ValidationProvider;
import org.hibernate.validation.util.Version;
+import org.hibernate.validation.engine.resolver.DefaultTraversableResolver;
/**
* @author Emmanuel Bernard
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-11
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -31,7 +31,6 @@
import org.slf4j.Logger;
-import org.hibernate.validation.AmbiguousConstraintUsageException;
import org.hibernate.validation.util.LoggerFactory;
import org.hibernate.validation.util.ValidatorTypeHelper;
@@ -175,7 +174,7 @@
builder.append( ", " );
}
builder.delete( builder.length() - 2, builder.length() );
- throw new AmbiguousConstraintUsageException( builder.toString() );
+ throw new UnexpectedTypeException( builder.toString() );
}
}
Deleted:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java 2009-03-11
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -1,13 +0,0 @@
-package org.hibernate.validation.engine;
-
-import java.lang.annotation.ElementType;
-import javax.validation.TraversableResolver;
-
-/**
- * @author Emmanuel Bernard
- */
-public class DefaultTraversableResolver implements TraversableResolver {
- public boolean isTraversable(Object traversableObject, String traversableProperty,
Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
- return true;
- }
-}
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
(from rev 16121,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -0,0 +1,91 @@
+package org.hibernate.validation.engine.resolver;
+
+import java.lang.annotation.ElementType;
+import javax.validation.TraversableResolver;
+
+import org.slf4j.Logger;
+
+import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.util.ReflectionHelper;
+
+/**
+ * A JPA 2 aware <code>TraversableResolver</code>.
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class DefaultTraversableResolver implements TraversableResolver {
+
+ private static final Logger log = LoggerFactory.make();
+
+ /**
+ * Class to load to check whether JPA 2 is on the classpath.
+ */
+ private static final String PERSISTENCE_UTIL_CLASS_NAME =
"javax.persistence.PersistenceUtil";
+
+ /**
+ * Class to instantiate in case JPA 2 is on the classpath.
+ */
+ private static final String JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME =
"org.hibernate.validation.engine.resolver.JPATraversableResolver";
+
+ /**
+ * A JPA 2 aware traversable resolver.
+ */
+ private TraversableResolver jpaTraversableResolver;
+
+
+ public DefaultTraversableResolver() {
+ detectJPA();
+ }
+
+ public boolean isTraversable(Object traversableObject, String traversableProperty,
Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
+ return jpaTraversableResolver == null || jpaTraversableResolver.isTraversable(
+ traversableObject, traversableProperty, rootBeanType, pathToTraversableObject,
elementType
+ );
+ }
+
+ /**
+ * Tries to load detect and load JPA.
+ */
+ private void detectJPA() {
+ try {
+ ReflectionHelper.classForName( PERSISTENCE_UTIL_CLASS_NAME, this.getClass() );
+ log.info( "Found {} on classpath.", PERSISTENCE_UTIL_CLASS_NAME );
+ }
+ catch ( ClassNotFoundException e ) {
+ log.info(
+ "Cannot find {} on classpath. All properties will per default be
traversable.",
+ PERSISTENCE_UTIL_CLASS_NAME
+ );
+ return;
+ }
+
+ try {
+ Class jpaAwareResolverClass = ReflectionHelper.classForName(
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME, this.getClass()
+ );
+ jpaTraversableResolver = ( TraversableResolver ) jpaAwareResolverClass.newInstance();
+ log.info(
+ "Instantiated an instance of {}.",
JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ catch ( ClassNotFoundException e ) {
+ log.info(
+ "Unable to load JPA aware resolver {}. All properties will per default be
traversable.",
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ catch ( IllegalAccessException e ) {
+ log.info(
+ "Unable to instantiate JPA aware resolver {}. All properties will per default
be traversable.",
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ catch ( InstantiationException e ) {
+ log.info(
+ "Unable to instantiate JPA aware resolver {}. All properties will per default
be traversable.",
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ }
+}
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -0,0 +1,32 @@
+// $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 org.hibernate.validation.engine.resolver;
+
+import java.lang.annotation.ElementType;
+import javax.persistence.Persistence;
+import javax.validation.TraversableResolver;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class JPATraversableResolver implements TraversableResolver {
+
+ public boolean isTraversable(Object traversableObject, String traversableProperty,
Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
+ return Persistence.getPersistenceUtil().isLoaded( traversableObject,
traversableProperty );
+ }
+}
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-03-11
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -421,4 +421,19 @@
}
return false;
}
+
+ public static Class<?> classForName(String name, Class<?> caller) throws
ClassNotFoundException {
+ try {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( contextClassLoader != null ) {
+ return contextClassLoader.loadClass( name );
+ }
+ }
+ catch ( Throwable e ) {
+ // ignore
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+
+
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-11
11:32:22 UTC (rev 16142)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-11
13:09:25 UTC (rev 16143)
@@ -18,13 +18,12 @@
package org.hibernate.validation.engine.validatorresolution;
import java.util.Set;
-import org.hibernate.validation.AmbiguousConstraintUsageException;
import javax.validation.ConstraintViolation;
-import javax.validation.Validator;
import javax.validation.UnexpectedTypeException;
+import javax.validation.Validator;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -58,7 +57,7 @@
validator.validate( foo );
fail();
}
- catch ( AmbiguousConstraintUsageException e ) {
+ catch ( UnexpectedTypeException e ) {
assertTrue( e.getMessage().startsWith( "There are multiple validators" ) );
}
}