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.commons.configuration2.beanutils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.FluentPropertyBeanIntrospector;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.beanutils.WrapDynaBean;
import org.apache.commons.beanutils.WrapDynaClass;
import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
import org.apache.commons.lang3.ClassUtils;
/**
*
* A helper class for creating bean instances that are defined in configuration
* files.
*
*
* This class provides utility methods related to bean creation
* operations. These methods simplify such operations because a client need not
* deal with all involved interfaces. Usually, if a bean declaration has already
* been obtained, a single method call is necessary to create a new bean
* instance.
*
*
* This class also supports the registration of custom bean factories.
* Implementations of the {@link BeanFactory} interface can be
* registered under a symbolic name using the {@code registerBeanFactory()}
* method. In the configuration file the name of the bean factory can be
* specified in the bean declaration. Then this factory will be used to create
* the bean.
*
*
* In order to create beans using {@code BeanHelper}, create and instance of
* this class and initialize it accordingly - a default {@link BeanFactory}
* can be passed to the constructor, and additional bean factories can be
* registered (see above). Then this instance can be used to create beans from
* {@link BeanDeclaration} objects. {@code BeanHelper} is thread-safe. So an
* instance can be passed around in an application and shared between multiple
* components.
*
*
* @since 1.3
* @version $Id: BeanHelper.java 1842194 2018-09-27 22:24:23Z ggregory $
*/
public final class BeanHelper
{
/**
* A default instance of {@code BeanHelper} which can be shared between
* arbitrary components. If no special configuration is needed, this
* instance can be used throughout an application. Otherwise, new instances
* can be created with their own configuration.
*/
public static final BeanHelper INSTANCE = new BeanHelper();
/**
* A special instance of {@code BeanUtilsBean} which is used for all
* property set and copy operations. This instance was initialized with
* {@code BeanIntrospector} objects which support fluent interfaces. This is
* required for handling builder parameter objects correctly.
*/
private static final BeanUtilsBean BEAN_UTILS_BEAN = initBeanUtilsBean();
/** Stores a map with the registered bean factories. */
private final Map beanFactories = Collections
.synchronizedMap(new HashMap());
/**
* Stores the default bean factory, which is used if no other factory
* is provided in a bean declaration.
*/
private final BeanFactory defaultBeanFactory;
/**
* Creates a new instance of {@code BeanHelper} with the default instance of
* {@link DefaultBeanFactory} as default {@link BeanFactory}.
*/
public BeanHelper()
{
this(null);
}
/**
* Creates a new instance of {@code BeanHelper} and sets the specified
* default {@code BeanFactory}.
*
* @param defFactory the default {@code BeanFactory} (can be null,
* then a default instance is used)
*/
public BeanHelper(final BeanFactory defFactory)
{
defaultBeanFactory =
(defFactory != null) ? defFactory : DefaultBeanFactory.INSTANCE;
}
/**
* Register a bean factory under a symbolic name. This factory object can
* then be specified in bean declarations with the effect that this factory
* will be used to obtain an instance for the corresponding bean
* declaration.
*
* @param name the name of the factory
* @param factory the factory to be registered
*/
public void registerBeanFactory(final String name, final BeanFactory factory)
{
if (name == null)
{
throw new IllegalArgumentException(
"Name for bean factory must not be null!");
}
if (factory == null)
{
throw new IllegalArgumentException("Bean factory must not be null!");
}
beanFactories.put(name, factory);
}
/**
* Deregisters the bean factory with the given name. After that this factory
* cannot be used any longer.
*
* @param name the name of the factory to be deregistered
* @return the factory that was registered under this name; null if
* there was no such factory
*/
public BeanFactory deregisterBeanFactory(final String name)
{
return beanFactories.remove(name);
}
/**
* Returns a set with the names of all currently registered bean factories.
*
* @return a set with the names of the registered bean factories
*/
public Set registeredFactoryNames()
{
return beanFactories.keySet();
}
/**
* Returns the default bean factory.
*
* @return the default bean factory
*/
public BeanFactory getDefaultBeanFactory()
{
return defaultBeanFactory;
}
/**
* Initializes the passed in bean. This method will obtain all the bean's
* properties that are defined in the passed in bean declaration. These
* properties will be set on the bean. If necessary, further beans will be
* created recursively.
*
* @param bean the bean to be initialized
* @param data the bean declaration
* @throws ConfigurationRuntimeException if a property cannot be set
*/
public void initBean(final Object bean, final BeanDeclaration data)
{
initBeanProperties(bean, data);
final Map nestedBeans = data.getNestedBeanDeclarations();
if (nestedBeans != null)
{
if (bean instanceof Collection)
{
// This is safe because the collection stores the values of the
// nested beans.
@SuppressWarnings("unchecked")
final
Collection