Return-Path: X-Original-To: apmail-bval-commits-archive@www.apache.org Delivered-To: apmail-bval-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0B75110510 for ; Mon, 1 Jul 2013 10:07:07 +0000 (UTC) Received: (qmail 19068 invoked by uid 500); 1 Jul 2013 10:07:07 -0000 Delivered-To: apmail-bval-commits-archive@bval.apache.org Received: (qmail 19045 invoked by uid 500); 1 Jul 2013 10:07:06 -0000 Mailing-List: contact commits-help@bval.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: bval-dev@bval.apache.org Delivered-To: mailing list commits@bval.apache.org Received: (qmail 19038 invoked by uid 99); 1 Jul 2013 10:07:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Jul 2013 10:07:06 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Jul 2013 10:06:48 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E2C92238889B; Mon, 1 Jul 2013 10:06:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1498347 [1/7] - in /bval/branches/bval-11: ./ bval-core/src/main/java/org/apache/bval/ bval-core/src/main/java/org/apache/bval/model/ bval-core/src/main/java/org/apache/bval/util/ bval-extras/ bval-guice/ bval-guice/src/main/java/org/apach... Date: Mon, 01 Jul 2013 10:06:21 -0000 To: commits@bval.apache.org From: rmannibucau@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130701100625.E2C92238889B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: rmannibucau Date: Mon Jul 1 10:06:18 2013 New Revision: 1498347 URL: http://svn.apache.org/r1498347 Log: commiting some hacks on bean validation 1.1 Added: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ConstructorAccess.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/Meta.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/AnyLiteral.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValBinding.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValExtension.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptor.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/DefaultLiteral.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorBean.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/constraints/SizeValidatorForCharSequence.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/AppendValidationToList.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/BootstrapConfigurationImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstructorDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/CrossParameterDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/InvocableElementDescriptor.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/MethodDescriptor.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/MethodDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/NodeBuilderCustomizableContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/NodeContextBuilderImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ParameterAccess.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ParameterDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ParametersAccess.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ProcedureDescriptor.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ReturnAccess.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ReturnValueDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/groups/GroupConversionDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/parameter/ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/parameter/DefaultParameterNameProvider.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/IOs.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/LeafNodeBuilderCustomizableContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/LeafNodeBuilderDefinedContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/MarkableInputStream.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/Proxies.java bval/branches/bval-11/bval-jsr303/src/main/resources/META-INF/beans.xml bval/branches/bval-11/bval-jsr303/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension bval/branches/bval-11/bval-jsr303/src/main/xsd/validation-configuration-1.1.xsd bval/branches/bval-11/bval-jsr303/src/main/xsd/validation-mapping-1.1.xsd bval/branches/bval-11/bval-tck/work-tests-suite.xml bval/branches/bval-11/bval-tck11/ bval/branches/bval-11/bval-tck11/pom.xml bval/branches/bval-11/bval-tck11/src/ bval/branches/bval-11/bval-tck11/src/main/ bval/branches/bval-11/bval-tck11/src/main/java/ bval/branches/bval-11/bval-tck11/src/main/java/org/ bval/branches/bval-11/bval-tck11/src/main/java/org/apache/ bval/branches/bval-11/bval-tck11/src/main/java/org/apache/bval/ bval/branches/bval-11/bval-tck11/src/main/java/org/apache/bval/arquillian/ bval/branches/bval-11/bval-tck11/src/main/java/org/apache/bval/arquillian/BValArquillianExtension.java bval/branches/bval-11/bval-tck11/src/main/java/org/apache/bval/arquillian/EJBEnricher.java bval/branches/bval-11/bval-tck11/src/main/java/org/apache/bval/arquillian/jndi/ bval/branches/bval-11/bval-tck11/src/main/java/org/apache/bval/arquillian/jndi/BValJndiFactory.java bval/branches/bval-11/bval-tck11/src/main/resources/ bval/branches/bval-11/bval-tck11/src/main/resources/META-INF/ bval/branches/bval-11/bval-tck11/src/main/resources/META-INF/services/ bval/branches/bval-11/bval-tck11/src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension bval/branches/bval-11/bval-tck11/src/main/resources/jndi.properties bval/branches/bval-11/bval-tck11/src/test/ bval/branches/bval-11/bval-tck11/src/test/java/ bval/branches/bval-11/bval-tck11/src/test/java/org/ bval/branches/bval-11/bval-tck11/src/test/java/org/apache/ bval/branches/bval-11/bval-tck11/src/test/java/org/apache/webbeans/ bval/branches/bval-11/bval-tck11/src/test/java/org/apache/webbeans/intercept/ bval/branches/bval-11/bval-tck11/src/test/java/org/apache/webbeans/intercept/InterceptorsManager.java bval/branches/bval-11/bval-tck11/src/test/resources/ bval/branches/bval-11/bval-tck11/src/test/resources/arquillian.xml bval/branches/bval-11/bval-tck11/work-tests-suite.xml Removed: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/constraints/SizeValidatorForString.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ Modified: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/BeanValidationContext.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ValidationResults.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaBean.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/ValidationListener.java bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/ValidationHelper.java bval/branches/bval-11/bval-extras/pom.xml bval/branches/bval-11/bval-guice/pom.xml bval/branches/bval-11/bval-guice/src/main/java/org/apache/bval/guice/ConfigurationStateProvider.java bval/branches/bval-11/bval-guice/src/main/java/org/apache/bval/guice/GuiceAwareConstraintValidatorFactory.java bval/branches/bval-11/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java bval/branches/bval-11/bval-jsr303/pom.xml bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheFactoryContext.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorConfiguration.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorFactory.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/BeanDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConfigurationImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintFinderImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidationListener.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidatorContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintViolationImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultConstraintValidatorFactory.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultMessageInterpolator.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/ElementDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContext.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303Features.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/PropertyDescriptorImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/groups/Group.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/groups/GroupsComputer.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ClassHelper.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ConstraintDefinitionValidator.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderCustomizableContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderDefinedContextImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeContextBuilderImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathImpl.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/SecureActions.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationIgnores.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxy.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxyBuilder.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/MetaConstraint.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationMappingParser.java bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/ValidationParser.java bval/branches/bval-11/bval-jsr303/src/main/resources/org/apache/bval/jsr303/DefaultConstraints.properties bval/branches/bval-11/bval-jsr303/src/main/xjb/binding-customization.xjb bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/BootstrapTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/CustomConstraintValidatorFactoryTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/CustomValidatorFactoryTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/DefaultMessageInterpolatorTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/Jsr303Test.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/extensions/MethodValidatorImplTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/PathImplTest.java bval/branches/bval-11/bval-jsr303/src/test/java/org/apache/bval/jsr303/xml/ValidationParserTest.java bval/branches/bval-11/bval-jsr303/src/test/resources/sample-constraints.xml bval/branches/bval-11/bval-tck/pom.xml bval/branches/bval-11/pom.xml Modified: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/BeanValidationContext.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/BeanValidationContext.java?rev=1498347&r1=1498346&r2=1498347&view=diff ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/BeanValidationContext.java (original) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/BeanValidationContext.java Mon Jul 1 10:06:18 2013 @@ -300,6 +300,10 @@ public class BeanValidationContext constructor; + + public ConstructorAccess(final Constructor constructor) { + this.constructor = constructor; + if (!constructor.isAccessible()) { + run(new PrivilegedAction() { + public Void run() { + constructor.setAccessible(true); + return null; + } + }); + } + } + + @Override + public Object get(final Object instance) { + throw new UnsupportedOperationException(); + } + + public ElementType getElementType() { + return ElementType.CONSTRUCTOR; + } + + public Type getJavaType() { + return constructor.getDeclaringClass(); + } + + /** + * {@inheritDoc} + */ + public String getPropertyName() { + return constructor.getDeclaringClass().getSimpleName(); + } + + /** + * {@inheritDoc} + */ + public String toString() { + return constructor.toString(); + } + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final ConstructorAccess that = (ConstructorAccess) o; + return constructor.equals(that.constructor); + } + + public int hashCode() { + return constructor.hashCode(); + } + + private static T run(PrivilegedAction action) { + if (System.getSecurityManager() != null) { + return AccessController.doPrivileged(action); + } else { + return action.run(); + } + } +} Modified: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ValidationResults.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ValidationResults.java?rev=1498347&r1=1498346&r2=1498347&view=diff ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ValidationResults.java (original) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ValidationResults.java Mon Jul 1 10:06:18 2013 @@ -21,7 +21,12 @@ import org.apache.bval.model.ValidationC import org.apache.bval.model.ValidationListener; import java.io.Serializable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; /** * Description: Implements a contains to hold and transport validation results
Added: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/Meta.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/Meta.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/Meta.java (added) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/Meta.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.model; + +public class Meta extends FeaturesCapable { + protected MetaBean parentMetaBean; + + public MetaBean getParentMetaBean() { + return parentMetaBean; + } +} Modified: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaBean.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaBean.java?rev=1498347&r1=1498346&r2=1498347&view=diff ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaBean.java (original) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaBean.java Mon Jul 1 10:06:18 2013 @@ -16,6 +16,11 @@ */ package org.apache.bval.model; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Comparator; import java.util.Map; import java.util.TreeMap; @@ -31,7 +36,56 @@ public class MetaBean extends FeaturesCa private String id; private String name; private Class beanClass; - private Map properties = new TreeMap(); + + // TODO: optimize sortings + + private Map properties = new TreeMap(new Comparator() { // order of fields to ensure correct failling order + public int compare(final String o1, final String o2) { + return fieldIndex(o1) - fieldIndex(o2); + } + + private int fieldIndex(final String o2) { + final Class clazz = getBeanClass(); + + int i = 0; + Class beanClass1 = clazz; + while (beanClass1 != null && beanClass1 != Object.class) { + for (final Field f : beanClass1.getDeclaredFields()) { + i++; + if (f.getName().equals(o2)) { + return i; + } + } + beanClass1 = beanClass1.getSuperclass(); + } + + if (clazz != null) { + final String getter = "get" + Character.toUpperCase(o2.charAt(0)) + o2.substring(1); + for (final Method m : clazz.getMethods()) { + i++; + if (m.getName().equals(getter) && m.getParameterTypes().length == 0) { + return i; + } + } + } + + return Integer.MIN_VALUE; // to avoid collision and false positive in get() due to equals + } + }); + private Map methods = new TreeMap(new Comparator() { + public int compare(final Method o1, final Method o2) { + final int i = o1.getName().compareTo(o2.getName()); + if (i != 0) { + return i; + } + return Arrays.hashCode(o1.getParameterTypes()) - Arrays.hashCode(o2.getParameterTypes()); + } + }); + private Map, MetaConstructor> constructors = new TreeMap, MetaConstructor>(new Comparator>() { + public int compare(final Constructor o1, final Constructor o2) { + return Arrays.hashCode(o1.getParameterTypes()) - Arrays.hashCode(o2.getParameterTypes()); + } + }); /** * Get the id. @@ -99,6 +153,22 @@ public class MetaBean extends FeaturesCa return properties.values().toArray(new MetaProperty[this.properties.size()]); } + public MetaMethod[] getMethods() { + return methods.values().toArray(new MetaMethod[this.methods.size()]); + } + + public void addMethod(final Method method, final MetaMethod meta) { + methods.put(method, meta); + } + + public MetaConstructor[] getConstructors() { + return constructors.values().toArray(new MetaConstructor[this.constructors.size()]); + } + + public void addConstructor(final Constructor constructor, final MetaConstructor meta) { + constructors.put(constructor, meta); + } + /** * Set the properties. * @@ -204,4 +274,11 @@ public class MetaBean extends FeaturesCa return bean == null || bean == beanClass || beanClass.isInstance(bean) ? this : null; } + public MetaMethod getMethod(final Method method) { + return methods.get(method); + } + + public MetaConstructor getConstructor(final Constructor constructor) { + return constructors.get(constructor); + } } Added: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java (added) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.bval.model; + +import java.lang.reflect.Constructor; + +public class MetaConstructor extends MetaInvocable { + private final Constructor constructor; + + public MetaConstructor(final MetaBean metabean, final Constructor constructor) { + this.parentMetaBean = metabean; + this.constructor = constructor; + } + + public Constructor getConstructor() { + return constructor; + } +} Added: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java (added) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.bval.model; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public abstract class MetaInvocable extends Meta { + private Map parameters = new HashMap(); + private Annotation[] annotations = new Annotation[0]; + + public Collection getParameters() { + return new ArrayList(parameters.values()); + } + + public void addParameter(final int idx, final MetaParameter param) { + parameters.put(idx, param); + } + + public Annotation[] getAnnotations() { + return annotations; + } + + public void addAnnotation(final Annotation annotation) { + final Annotation[] a = new Annotation[annotations.length + 1]; + System.arraycopy(annotations, 0, a, 0, annotations.length); + a[a.length - 1] = annotation; + this.annotations = a; + } + + public MetaParameter getParameter(final Integer index) { + return parameters.get(index); + } +} Added: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java (added) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.bval.model; + +import java.lang.reflect.Method; + +public class MetaMethod extends MetaInvocable { + private final Method method; + + public MetaMethod(final MetaBean parent, final Method method) { + this.parentMetaBean = parent; + this.method = method; + } + + public Method getMethod() { + return method; + } +} Added: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java (added) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.bval.model; + +import java.lang.annotation.Annotation; + +public class MetaParameter extends Meta { + private final MetaInvocable invocable; + private final Integer index; + private Annotation[] annotations = new Annotation[0]; + + public MetaParameter(final MetaInvocable metaMethod, final Integer index) { + this.invocable = metaMethod; + this.index = index; + } + + public MetaInvocable getMethod() { + return invocable; + } + + public Integer getIndex() { + return index; + } + + public Annotation[] getAnnotations() { + return annotations; + } + + public void addAnnotation(final Annotation annotation) { + final Annotation[] a = new Annotation[annotations.length + 1]; + System.arraycopy(annotations, 0, a, 0, annotations.length); + a[a.length - 1] = annotation; + this.annotations = a; + } +} Modified: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java?rev=1498347&r1=1498346&r2=1498347&view=diff ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java (original) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java Mon Jul 1 10:06:18 2013 @@ -16,10 +16,10 @@ */ package org.apache.bval.model; -import java.lang.reflect.Type; - import org.apache.commons.lang3.reflect.TypeUtils; +import java.lang.reflect.Type; + /** * Description: the meta description of a property of a bean. It supports a map * of features and multiple validations.
@@ -27,7 +27,7 @@ import org.apache.commons.lang3.reflect. * @see Validation * @see MetaBean */ -public class MetaProperty extends FeaturesCapable +public class MetaProperty extends Meta implements Cloneable, Features.Property { private static final long serialVersionUID = 1L; @@ -35,7 +35,6 @@ public class MetaProperty extends Featur private Type type; private MetaBean metaBean; - private MetaBean parentMetaBean; /** * Create a new MetaProperty instance. @@ -60,14 +59,6 @@ public class MetaProperty extends Featur } /** - * Get the metabean that owns this property (set by MetaBean.putProperty()) - * @return - */ - public MetaBean getParentMetaBean() { - return parentMetaBean; - } - - /** * Set the metabean that owns this property (usually called by MetaBean.putProperty()) * @param parentMetaBean */ @@ -171,5 +162,4 @@ public class MetaProperty extends Featur public String toString() { return "MetaProperty{" + "name='" + name + '\'' + ", type=" + type + '}'; } - } Modified: bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java?rev=1498347&r1=1498346&r2=1498347&view=diff ============================================================================== --- bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java (original) +++ bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java Mon Jul 1 10:06:18 2013 @@ -106,6 +106,8 @@ public interface ValidationContext + javax.validation + validation-api + 1.1.0.Final + provided + + provided + --> provided + --> provided + --> junit junit - 3.8.2 - test org.mockito mockito-core test + + javax.xml.bind + jaxb-api + 2.2.6 + + + com.sun.xml.bind + jaxb-impl + 2.2.6 + @@ -170,7 +193,7 @@ org.codehaus.mojo jaxb2-maven-plugin - 1.2 + 1.3.1 @@ -181,6 +204,7 @@ org.apache.bval.jsr303.xml true + validation-configuration-1.1.xsd,validation-mapping-1.1.xsd Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/AnyLiteral.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/AnyLiteral.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/AnyLiteral.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/AnyLiteral.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.inject.Any; +import javax.enterprise.util.AnnotationLiteral; + +public class AnyLiteral extends AnnotationLiteral implements Any { + public static final AnyLiteral INSTANCE = new AnyLiteral(); + + @Override + public String toString() { + return "@javax.enterprise.inject.Any()"; + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.inject.spi.AnnotatedConstructor; +import javax.enterprise.inject.spi.AnnotatedField; +import javax.enterprise.inject.spi.AnnotatedMethod; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.util.AnnotationLiteral; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.HashSet; +import java.util.Set; + +public class BValAnnotatedType implements AnnotatedType { + private final AnnotatedType delegate; + private final Set annotations; + + public BValAnnotatedType(final AnnotatedType annotatedType) { + delegate = annotatedType; + + annotations = new HashSet(annotatedType.getAnnotations().size()); + annotations.addAll(annotatedType.getAnnotations()); + annotations.add(BValBindingLitteral.INSTANCE); + } + + public Class getJavaClass() { + return delegate.getJavaClass(); + } + + public Set> getConstructors() { + return delegate.getConstructors(); + } + + public Set> getMethods() { + return delegate.getMethods(); + } + + public Set> getFields() { + return delegate.getFields(); + } + + public Type getBaseType() { + return delegate.getBaseType(); + } + + public Set getTypeClosure() { + return delegate.getTypeClosure(); + } + + public T getAnnotation(final Class annotationType) { + for (final Annotation ann : annotations) { + if (ann.annotationType().equals(annotationType)) { + return (T) ann; + } + } + return null; + } + + public Set getAnnotations() { + return annotations; + } + + public boolean isAnnotationPresent(Class annotationType) { + for (final Annotation ann : annotations) { + if (ann.annotationType().equals(annotationType)) { + return true; + } + } + return false; + } + + public static class BValBindingLitteral extends AnnotationLiteral implements BValBinding { + public static final Annotation INSTANCE = new BValBindingLitteral(); + + public Class annotationType() { + return BValBinding.class; + } + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValBinding.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValBinding.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValBinding.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValBinding.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.interceptor.InterceptorBinding; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +@InterceptorBinding +public @interface BValBinding { +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValExtension.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValExtension.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValExtension.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValExtension.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,269 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.spi.AfterBeanDiscovery; +import javax.enterprise.inject.spi.AfterDeploymentValidation; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.BeforeBeanDiscovery; +import javax.enterprise.inject.spi.BeforeShutdown; +import javax.enterprise.inject.spi.Extension; +import javax.enterprise.inject.spi.InjectionTarget; +import javax.enterprise.inject.spi.ProcessAnnotatedType; +import javax.enterprise.inject.spi.ProcessBean; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.validation.BootstrapConfiguration; +import javax.validation.Configuration; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import javax.validation.executable.ExecutableType; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +// mainly copied from deltaspike to not force users to use deltaspike +// which would be a pain in servers +// TODO: get rid of beans.xml adding interceptor automatically +public class BValExtension implements Extension { + private static final Logger LOGGER = Logger.getLogger(BValExtension.class.getName()); + + private static BValExtension bmpSingleton = null; + private volatile Map bmInfos = new ConcurrentHashMap(); + + private boolean validatorFound = false; + private boolean validatorFactoryFound = false; + + private final Configuration config; + + private Set globalExecutableTypes; + private boolean isExecutableValidationEnabled; + + public BValExtension() { // read the config, could be done in a quicker way but this let us get defaults without duplicating code + config = Validation.byDefaultProvider().configure(); + try { + final BootstrapConfiguration bootstrap = config.getBootstrapConfiguration(); + globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes(); + isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled(); + } catch (final Exception e) { // custom providers can throw an exception + LOGGER.log(Level.SEVERE, e.getMessage(), e); + + globalExecutableTypes = Collections.emptySet(); + isExecutableValidationEnabled = false; + } + } + + public static BValExtension getInstance() { + return bmpSingleton; + } + + public void addBvalBinding(final @Observes BeforeBeanDiscovery beforeBeanDiscovery, final BeanManager beanManager) { + beforeBeanDiscovery.addInterceptorBinding(BValBinding.class); + } + + public void processAnnotatedType(final @Observes ProcessAnnotatedType pat) { + final Class javaClass = pat.getAnnotatedType().getJavaClass(); + final int modifiers = javaClass.getModifiers(); + if (!javaClass.getName().startsWith("javax.") && !javaClass.getName().startsWith("org.apache.bval") + && !javaClass.isInterface() && !Modifier.isFinal(modifiers) && !Modifier.isAbstract(modifiers)) { + pat.setAnnotatedType(new BValAnnotatedType(pat.getAnnotatedType())); + } + } + + public void processBean(final @Observes ProcessBean processBeanEvent) { + if (validatorFound && validatorFactoryFound) { + return; + } + + final Bean bean = processBeanEvent.getBean(); + if (ValidatorBean.class.isInstance(bean) || ValidatorFactoryBean.class.isInstance(bean)) { + return; + } + + final Set types = bean.getTypes(); + if (!validatorFound) { + validatorFound = types.contains(Validator.class); + } + if (!validatorFactoryFound) { + validatorFactoryFound = types.contains(ValidatorFactory.class); + } + } + + public void addBValBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery, final BeanManager beanManager) { + captureBeanManager(beanManager); + cdiIntegration(afterBeanDiscovery, beanManager); + } + + private void captureBeanManager(final BeanManager beanManager) { + // bean manager holder + if (bmpSingleton == null) { + bmpSingleton = this; + } + final BeanManagerInfo bmi = getBeanManagerInfo(loader()); + bmi.loadTimeBm = beanManager; + } + + private void cdiIntegration(final AfterBeanDiscovery afterBeanDiscovery, final BeanManager beanManager) { + // add validator and validatorFactory if needed + ValidatorFactory factory = null; + if (!validatorFactoryFound) { + try { + factory = config.buildValidatorFactory(); + afterBeanDiscovery.addBean(new ValidatorFactoryBean(factory)); + } catch (final Exception e) { // can throw an exception with custom providers + LOGGER.log(Level.SEVERE, e.getMessage(), e); + } + } + if (!validatorFound) { + try { + if (factory == null) { + factory = config.buildValidatorFactory(); + } + afterBeanDiscovery.addBean(new ValidatorBean(factory.getValidator())); + validatorFound = true; + } catch (final Exception e) { // getValidator can throw an exception with custom providers + LOGGER.log(Level.SEVERE, e.getMessage(), e); + } + } + + // add our interceptor, after having added validator if needed since it is injected in the interceptor + if (validatorFound) { + afterBeanDiscovery.addBean(new BValInterceptorBean(beanManager)); + } // else we couldn't resolve the interceptor injection point + } + + private static ClassLoader loader() { + return Thread.currentThread().getContextClassLoader(); + } + + public BeanManager getBeanManager() { + final BeanManagerInfo bmi = getBeanManagerInfo(loader()); + + BeanManager result = bmi.finalBm; + if (result == null) { + synchronized (this) { + result = resolveBeanManagerViaJndi(); + if (result == null) { + result = bmi.loadTimeBm; + } + if (result == null) { + throw new IllegalStateException("Unable to find BeanManager. " + + "Please ensure that you configured the CDI implementation of your choice properly."); + } + bmi.finalBm = result; + } + } + + return result; + } + + public void cleanupFinalBeanManagers(final @Observes AfterDeploymentValidation adv) { + for (final BeanManagerInfo bmi : bmpSingleton.bmInfos.values()) { + bmi.finalBm = null; + } + } + + public void cleanupStoredBeanManagerOnShutdown(final @Observes BeforeShutdown beforeShutdown) { + bmpSingleton.bmInfos.remove(loader()); + } + + private static BeanManager resolveBeanManagerViaJndi() { + try { + return (BeanManager) new InitialContext().lookup("java:comp/BeanManager"); + } catch (final NamingException e) { + return null; + } + } + + private BeanManagerInfo getBeanManagerInfo(final ClassLoader cl) { + BeanManagerInfo bmi = bmpSingleton.bmInfos.get(cl); + if (bmi == null) { + synchronized (this) { + bmi = bmpSingleton.bmInfos.get(cl); + if (bmi == null) { + bmi = new BeanManagerInfo(); + bmpSingleton.bmInfos.put(cl, bmi); + } + } + } + return bmi; + } + + public static Releasable inject(final Class clazz) { + try { + final BeanManager beanManager = getInstance().getBeanManager(); + final AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz); + final InjectionTarget it = beanManager.createInjectionTarget(annotatedType); + final CreationalContext context = beanManager.createCreationalContext(null); + final T instance = it.produce(context); + it.inject(instance, context); + it.postConstruct(instance); + + return new Releasable(context, it, instance); + } catch (final Exception e) { + // no-op + } catch (final NoClassDefFoundError error) { + // no-op + } + return null; + } + + private static class BeanManagerInfo { + private BeanManager loadTimeBm = null; + private BeanManager finalBm = null; + } + + public static class Releasable { + private final CreationalContext context; + private final InjectionTarget injectionTarget; + private final T instance; + + private Releasable(final CreationalContext context, final InjectionTarget injectionTarget, final T instance) { + this.context = context; + this.injectionTarget = injectionTarget; + this.instance = instance; + } + + public void release() { + try { + injectionTarget.preDestroy(instance); + injectionTarget.dispose(instance); + context.release(); + } catch (final Exception e) { + // no-op + } catch (final NoClassDefFoundError e) { + // no-op + } + } + + public T getInstance() { + return instance; + } + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptor.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptor.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptor.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptor.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import org.apache.bval.jsr303.util.Proxies; + +import javax.inject.Inject; +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.Validator; +import javax.validation.executable.ExecutableValidator; +import javax.validation.metadata.MethodDescriptor; +import java.lang.reflect.Method; +import java.util.Set; + +@Interceptor +@BValBinding +public class BValInterceptor { + @Inject + private Validator validator; + + @AroundInvoke + public Object around(final InvocationContext context) throws Throwable { + final ExecutableValidator ev = validator.forExecutables(); + + final Method method = context.getMethod(); + final MethodDescriptor constraintsForMethod = validator.getConstraintsForClass(Proxies.classFor(method.getDeclaringClass())).getConstraintsForMethod(method.getName(), method.getParameterTypes()); + if (constraintsForMethod == null) { + return context.proceed(); + } + + { + final Set> violations = ev.validateParameters(context.getTarget(), method, context.getParameters()); + if (!violations.isEmpty()) { + throw new ConstraintViolationException(violations); + } + } + + final Object result = context.proceed(); + + { + final Set> violations = ev.validateReturnValue(context.getTarget(), method, result); + if (!violations.isEmpty()) { + throw new ConstraintViolationException(violations); + } + } + + return result; + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.context.Dependent; +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Annotated; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.enterprise.inject.spi.InjectionTarget; +import javax.enterprise.inject.spi.PassivationCapable; +import java.lang.annotation.Annotation; +import java.lang.reflect.Member; +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class BValInterceptorBean implements Bean, PassivationCapable { + private final Set types; + private final Set qualifiers; + private final Set injectionPoints; + private final InjectionTarget injectionTarget; + + public BValInterceptorBean(final BeanManager bm) { + types = new HashSet(); + types.add(BValInterceptor.class); + types.add(Object.class); + + qualifiers = new HashSet(); + qualifiers.add(DefaultLiteral.INSTANCE); + qualifiers.add(AnyLiteral.INSTANCE); + + injectionTarget = bm.createInjectionTarget(bm.createAnnotatedType(BValInterceptor.class)); + injectionPoints = Collections.singleton(InjectionPoint.class.cast(new BValInterceptorInjectionPoint(this, injectionTarget.getInjectionPoints().iterator().next()))); + } + + public Set getTypes() { + return types; + } + + public Set getQualifiers() { + return qualifiers; + } + + public Class getScope() { + return Dependent.class; + } + + public String getName() { + return null; + } + + public boolean isNullable() { + return false; + } + + public Set getInjectionPoints() { + return injectionPoints; + } + + public Class getBeanClass() { + return BValInterceptor.class; + } + + public Set> getStereotypes() { + return Collections.emptySet(); + } + + public boolean isAlternative() { + return false; + } + + public BValInterceptor create(final CreationalContext context) { + final BValInterceptor produced = injectionTarget.produce(context); + injectionTarget.inject(produced, context); + injectionTarget.postConstruct(produced); + return produced; + } + + public void destroy(final BValInterceptor instance, final CreationalContext context) { + injectionTarget.preDestroy(instance); + injectionTarget.dispose(instance); + context.release(); + } + + public String getId() { + return "BValInterceptor-" + hashCode(); + } + + private static class BValInterceptorInjectionPoint implements InjectionPoint { + private final InjectionPoint delegate; + private final Bean bean; + + public BValInterceptorInjectionPoint(final Bean bean, final InjectionPoint next) { + this.bean = bean; + delegate = next; + } + + public Type getType() { + return delegate.getType(); + } + + public Set getQualifiers() { + return delegate.getQualifiers(); + } + + public Bean getBean() { + return bean; + } + + public Member getMember() { + return delegate.getMember(); + } + + public Annotated getAnnotated() { + return delegate.getAnnotated(); + } + + public boolean isDelegate() { + return delegate.isDelegate(); + } + + public boolean isTransient() { + return delegate.isTransient(); + } + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/DefaultLiteral.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/DefaultLiteral.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/DefaultLiteral.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/DefaultLiteral.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.inject.Default; +import javax.enterprise.util.AnnotationLiteral; + +public class DefaultLiteral extends AnnotationLiteral implements Default { + public static final DefaultLiteral INSTANCE = new DefaultLiteral(); + + @Override + public String toString() { + return "@javax.enterprise.inject.Default()"; + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorBean.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorBean.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorBean.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorBean.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.enterprise.inject.spi.PassivationCapable; +import javax.validation.Validator; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class ValidatorBean implements Bean , PassivationCapable{ + private final Set types; + private final Set qualifiers; + private final Validator instance; + + public ValidatorBean(Validator validator) { + this.instance = validator; + + types = new HashSet(); + types.add(Validator.class); + types.add(Object.class); + + qualifiers = new HashSet(); + qualifiers.add(DefaultLiteral.INSTANCE); + qualifiers.add(AnyLiteral.INSTANCE); + } + + public Set getTypes() { + return types; + } + + public Set getQualifiers() { + return qualifiers; + } + + public Class getScope() { + return ApplicationScoped.class; + } + + public String getName() { + return null; + } + + public boolean isNullable() { + return false; + } + + public Set getInjectionPoints() { + return Collections.emptySet(); + } + + public Class getBeanClass() { + return Validator.class; + } + + public Set> getStereotypes() { + return Collections.emptySet(); + } + + public boolean isAlternative() { + return false; + } + + public Validator create(final CreationalContext context) { + return instance; + } + + public void destroy(final Validator instance, final CreationalContext context) { + // no-op + } + + public String getId() { + return "BValValidator-" + hashCode(); + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.cdi; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.enterprise.inject.spi.PassivationCapable; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class ValidatorFactoryBean implements Bean , PassivationCapable{ + private final Set types; + private final Set qualifiers; + private final ValidatorFactory instance; + + public ValidatorFactoryBean(final ValidatorFactory validatorFactory) { + this.instance = validatorFactory; + + types = new HashSet(); + types.add(ValidatorFactory.class); + types.add(Object.class); + + qualifiers = new HashSet(); + qualifiers.add(DefaultLiteral.INSTANCE); + qualifiers.add(AnyLiteral.INSTANCE); + } + + public Set getTypes() { + return types; + } + + public Set getQualifiers() { + return qualifiers; + } + + public Class getScope() { + return ApplicationScoped.class; + } + + public String getName() { + return null; + } + + public boolean isNullable() { + return false; + } + + public Set getInjectionPoints() { + return Collections.emptySet(); + } + + public Class getBeanClass() { + return ValidatorFactory.class; + } + + public Set> getStereotypes() { + return Collections.emptySet(); + } + + public boolean isAlternative() { + return false; + } + + public ValidatorFactory create(final CreationalContext context) { + return instance; + } + + public void destroy(final ValidatorFactory instance, final CreationalContext context) { + instance.close(); + } + + public String getId() { + return "BValValidatorFactory-" + hashCode(); + } +} Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/constraints/SizeValidatorForCharSequence.java URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/constraints/SizeValidatorForCharSequence.java?rev=1498347&view=auto ============================================================================== --- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/constraints/SizeValidatorForCharSequence.java (added) +++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/constraints/SizeValidatorForCharSequence.java Mon Jul 1 10:06:18 2013 @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bval.constraints; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.constraints.Size; + +/** Check that a string's length is between min and max. */ +public class SizeValidatorForCharSequence extends SizeValidator + implements ConstraintValidator { + /** + * Checks the length of the specified string. + * + * @param s The string to validate. + * @param context context in which the constraint is evaluated. + * @return Returns true if the string is null or the length of s between the specified + * min and max values (inclusive), false otherwise. + */ + public boolean isValid(CharSequence s, ConstraintValidatorContext context) { + if (s == null) { + return true; + } + int length = s.length(); + return length >= min && length <= max; + } + +}