org.picocontainer.behaviors.PropertyApplicator Maven / Gradle / Ivy
/*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoClassNotFoundException;
import org.picocontainer.injectors.SetterInjector;
import org.picocontainer.behaviors.AbstractBehavior;
import org.picocontainer.behaviors.Cached;
/**
* Decorating component adapter that can be used to set additional properties
* on a component in a bean style. These properties must be managed manually
* by the user of the API, and will not be managed by PicoContainer. This class
* is therefore not the same as {@link SetterInjector},
* which is a true Setter Injection adapter.
*
* This adapter is mostly handy for setting various primitive properties via setters;
* it is also able to set javabean properties by discovering an appropriate
* {@link PropertyEditor} and using its setAsText
method.
*
*
* Note that this class doesn't cache instances. If you want caching,
* use a {@link Cached} around this one.
*
*
* @author Aslak Hellesøy
* @author Mauro Talevi
*/
@SuppressWarnings("serial")
public class PropertyApplicator extends AbstractBehavior {
private Map properties;
private transient Map setters = null;
/**
* Construct a PropertyApplicator.
*
* @param delegate the wrapped {@link ComponentAdapter}
* @throws PicoCompositionException {@inheritDoc}
*/
public PropertyApplicator(ComponentAdapter delegate) throws PicoCompositionException {
super(delegate);
}
/**
* Get a component instance and set given property values.
*
* @return the component instance with any properties of the properties map set.
* @throws PicoCompositionException {@inheritDoc}
* @throws PicoCompositionException {@inheritDoc}
* @throws org.picocontainer.PicoCompositionException
* {@inheritDoc}
* @see #setProperties(Map)
*/
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
final T componentInstance = super.getComponentInstance(container, into);
if (setters == null) {
setters = getSetters(getComponentImplementation());
}
if (properties != null) {
ComponentMonitor componentMonitor = currentMonitor();
Set propertyNames = properties.keySet();
for (String propertyName : propertyNames) {
final Object propertyValue = properties.get(propertyName);
Method setter = setters.get(propertyName);
Object valueToInvoke = this.getSetterParameter(propertyName, propertyValue, componentInstance, container);
try {
componentMonitor.invoking(container, PropertyApplicator.this, setter, componentInstance, new Object[] {valueToInvoke});
long startTime = System.currentTimeMillis();
setter.invoke(componentInstance, valueToInvoke);
componentMonitor.invoked(container,
PropertyApplicator.this,
setter, componentInstance, System.currentTimeMillis() - startTime, new Object[] {valueToInvoke}, null);
} catch (final Exception e) {
componentMonitor.invocationFailed(setter, componentInstance, e);
throw new PicoCompositionException("Failed to set property " + propertyName + " to " + propertyValue + ": " + e.getMessage(), e);
}
}
}
return componentInstance;
}
public String getDescriptor() {
return "PropertyApplied";
}
private Map getSetters(Class> clazz) {
Map result = new HashMap();
Method[] methods = getMethods(clazz);
for (Method method : methods) {
if (isSetter(method)) {
result.put(getPropertyName(method), method);
}
}
return result;
}
private Method[] getMethods(final Class> clazz) {
return (Method[]) AccessController.doPrivileged(new PrivilegedAction
© 2015 - 2024 Weber Informatics LLC | Privacy Policy