|
PicoContainer example source code file (PropertyApplyingBehavior.java)
The PicoContainer PropertyApplyingBehavior.java source code
/*****************************************************************************
* 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.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.CachingBehavior;
/**
* 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 <em>not the same as {@link SetterInjector},
* which is a true Setter Injection adapter.
* <p/>
* 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 <code>setAsText method.
* <p/>
* <em>
* Note that this class doesn't cache instances. If you want caching,
* use a {@link CachingBehavior} around this one.
* </em>
*
* @author Aslak Hellesøy
* @version $Revision: 3603 $
*/
public class PropertyApplyingBehavior extends AbstractBehavior {
private Map properties;
private transient Map<String, Method> setters = null;
/**
* Construct a PropertyApplyingBehavior.
*
* @param delegate the wrapped {@link ComponentAdapter}
* @throws PicoCompositionException {@inheritDoc}
*/
public PropertyApplyingBehavior(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 Object getComponentInstance(PicoContainer container) throws PicoCompositionException {
final Object componentInstance = super.getComponentInstance(container);
if (setters == null) {
setters = getSetters(getComponentImplementation());
}
if (properties != null) {
ComponentMonitor componentMonitor = currentMonitor();
Set<String> 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, PropertyApplyingBehavior.this, setter, componentInstance);
long startTime = System.currentTimeMillis();
setter.invoke(componentInstance, valueToInvoke);
componentMonitor.invoked(container,
PropertyApplyingBehavior.this,
setter, componentInstance, System.currentTimeMillis() - startTime);
} catch (final Exception e) {
componentMonitor.invocationFailed(setter, componentInstance, e);
throw new PicoCompositionException("Failed to set property " + propertyName + " to " + propertyValue + ": " + e.getMessage(), e);
}
}
}
return componentInstance;
}
private Map<String, Method> getSetters(Class clazz) {
Map<String, Method> result = new HashMap
Other PicoContainer examples (source code examples)Here is a short list of links related to this PicoContainer PropertyApplyingBehavior.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.