Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.openejb.assembler.classic;
import org.apache.openejb.bval.ValidatorUtil;
import org.apache.openejb.jee.bval.DefaultValidatedExecutableTypesType;
import org.apache.openejb.jee.bval.ExecutableValidationType;
import org.apache.openejb.jee.bval.PropertyType;
import org.apache.openejb.jee.bval.ValidationConfigType;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.util.AppFinder;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.container.BeanManagerImpl;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.naming.NamingException;
import javax.validation.BootstrapConfiguration;
import javax.validation.Configuration;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.ParameterNameProvider;
import javax.validation.TraversableResolver;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorContext;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableType;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
public final class ValidatorBuilder {
public static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, ValidatorBuilder.class);
public static final String VALIDATION_PROVIDER_KEY = "openejb.bean-validation.provider";
private ValidatorBuilder() {
// no-op
}
public static ValidatorFactory buildFactory(final ClassLoader classLoader, final ValidationInfo info) {
// now we will not be polluted by log build
return buildFactory(info, classLoader);
}
public static ValidationInfo getInfo(final ValidationConfigType config) {
final ValidationInfo info = new ValidationInfo();
if (config != null) {
info.version = config.getVersion();
info.providerClassName = config.getDefaultProvider();
info.constraintFactoryClass = config.getConstraintValidatorFactory();
info.traversableResolverClass = config.getTraversableResolver();
info.messageInterpolatorClass = config.getMessageInterpolator();
info.parameterNameProviderClass = config.getParameterNameProvider();
final ExecutableValidationType executableValidation = config.getExecutableValidation();
if (executableValidation != null) {
info.executableValidationEnabled = executableValidation.getEnabled();
final DefaultValidatedExecutableTypesType executableTypes = executableValidation.getDefaultValidatedExecutableTypes();
if (executableTypes != null) {
for (final ExecutableType type : executableTypes.getExecutableType()) {
info.validatedTypes.add(type.name());
}
}
}
for (final PropertyType p : config.getProperty()) {
info.propertyTypes.put(p.getName(), p.getValue());
}
for (final String element : config.getConstraintMapping()) {
info.constraintMappings.add(element);
}
}
return info;
}
public static ValidatorFactory buildFactory(final ValidationInfo config, final ClassLoader classLoader) {
ValidatorFactory factory = null;
final Thread thread = Thread.currentThread();
final ClassLoader oldContextLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(classLoader);
if (config == null) {
factory = Validation.buildDefaultValidatorFactory();
} else {
final Configuration> configuration = getConfig(config);
try {
factory = configuration.buildValidatorFactory();
} catch (final ValidationException ve) {
thread.setContextClassLoader(ValidatorBuilder.class.getClassLoader());
factory = Validation.buildDefaultValidatorFactory();
thread.setContextClassLoader(classLoader);
logger.warning("Unable create validator factory with config " + config
+ " (" + ve.getMessage() + ")."
+ " Default factory will be used.");
}
}
} finally {
thread.setContextClassLoader(oldContextLoader);
}
return factory;
}
@SuppressWarnings("unchecked")
private static Configuration> getConfig(final ValidationInfo info) {
Configuration> target = null;
final Thread thread = Thread.currentThread();
final ClassLoader classLoader = thread.getContextClassLoader();
String providerClassName = info.providerClassName;
if (providerClassName == null) {
providerClassName = SystemInstance.get().getOptions().get(VALIDATION_PROVIDER_KEY, (String) null);
}
if (providerClassName != null) {
try {
@SuppressWarnings({"unchecked", "rawtypes"}) final
Class clazz = classLoader.loadClass(providerClassName);
target = Validation.byProvider(clazz).configure();
logger.info("Using " + providerClassName + " as validation provider.");
} catch (final ClassNotFoundException e) {
logger.warning("Unable to load provider class " + providerClassName, e);
} catch (final ValidationException ve) {
logger.warning("Unable create validator factory with provider " + providerClassName
+ " (" + ve.getMessage() + ")."
+ " Default one will be used.");
}
}
if (target == null) {
// force to use container provider to ignore any conflicting configuration
thread.setContextClassLoader(ValidatorBuilder.class.getClassLoader());
target = Validation.byDefaultProvider().configure();
thread.setContextClassLoader(classLoader);
}
final Set types = new HashSet<>();
for (final String type : info.validatedTypes) {
types.add(ExecutableType.valueOf(type));
}
final Map props = new HashMap<>();
for (final Map.Entry