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.configuration.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.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.configuration.ConfigurationRuntimeException;
import org.apache.commons.lang.ClassUtils;
/**
*
* A helper class for creating bean instances that are defined in configuration
* files.
*
*
* This class provides static 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.
*
*
* @since 1.3
* @author Commons
* Configuration team
* @version $Id: BeanHelper.java 1534393 2013-10-21 22:02:27Z henning $
*/
public final class BeanHelper
{
/** Stores a map with the registered bean factories. */
private static final Map BEAN_FACTORIES = Collections
.synchronizedMap(new HashMap());
/**
* Stores the default bean factory, which will be used if no other factory
* is provided.
*/
private static BeanFactory defaultBeanFactory = DefaultBeanFactory.INSTANCE;
/**
* Private constructor, so no instances can be created.
*/
private BeanHelper()
{
}
/**
* 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 static void registerBeanFactory(String name, 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!");
}
BEAN_FACTORIES.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 static BeanFactory deregisterBeanFactory(String name)
{
return BEAN_FACTORIES.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 static Set registeredFactoryNames()
{
return BEAN_FACTORIES.keySet();
}
/**
* Returns the default bean factory.
*
* @return the default bean factory
*/
public static BeanFactory getDefaultBeanFactory()
{
return defaultBeanFactory;
}
/**
* Sets the default bean factory. This factory will be used for all create
* operations, for which no special factory is provided in the bean
* declaration.
*
* @param factory the default bean factory (must not be null)
*/
public static void setDefaultBeanFactory(BeanFactory factory)
{
if (factory == null)
{
throw new IllegalArgumentException(
"Default bean factory must not be null!");
}
defaultBeanFactory = factory;
}
/**
* 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 static void initBean(Object bean, BeanDeclaration data)
throws ConfigurationRuntimeException
{
initBeanProperties(bean, data);
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")
Collection