org.nanocontainer.nanowar.webwork2.PicoObjectFactory Maven / Gradle / Ivy
The newest version!
/*****************************************************************************
* Copyright (C) NanoContainer 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. *
* *
*****************************************************************************/
package org.nanocontainer.nanowar.webwork2;
import com.opensymphony.xwork.ObjectFactory;
import org.nanocontainer.nanowar.ActionsContainerFactory;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.defaults.ObjectReference;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
*
* XWork ObjectFactory which uses a PicoContainer to create component instances.
*
*
* @author Cyrille Le Clerc
* @author Jonas Engman
* @author Mauro Talevi
* @author Grégory Joseph
*/
public class PicoObjectFactory extends ObjectFactory {
private final ActionsContainerFactory actionsContainerFactory = new ActionsContainerFactory();
private final ObjectReference objectReference;
/**
* Creates a PicoObjectFactory with given object reference,
* used to pass the http request to the factory
*
* @param objectReference the ObjectReference
*/
public PicoObjectFactory(ObjectReference objectReference) {
this.objectReference = objectReference;
}
public boolean isNoArgConstructorRequired() {
return false;
}
/**
* Webwork-2.2 / XWork-1.1 method. ExtraContext can be ignored.
*/
public Object buildBean(Class clazz, Map extraContext) throws Exception {
return buildBean(clazz);
}
/**
* Webwork-2.2 / XWork-1.1 method. ExtraContext can be ignored.
*/
public Object buildBean(String className, Map extraContext) throws Exception {
return buildBean(className);
}
/**
* Webwork-2.2 / XWork-1.1 method. Used to validate a class be loaded.
* Using actionsContainerFactory for consistency with build methods.
*/
public Class getClassInstance(String className) throws ClassNotFoundException {
return actionsContainerFactory.getActionClass(className);
}
/**
* Instantiates an action using the PicoContainer found in the request scope.
*
* @see com.opensymphony.xwork.ObjectFactory#buildBean(java.lang.Class)
*/
public Object buildBean(Class actionClass) throws Exception {
MutablePicoContainer actionsContainer = actionsContainerFactory.getActionsContainer((HttpServletRequest) objectReference.get());
Object action = actionsContainer.getComponentInstance(actionClass);
if (action == null) {
// The action wasn't registered. Attempt to instantiate it.
actionsContainer.registerComponentImplementation(actionClass);
action = actionsContainer.getComponentInstance(actionClass);
}
return action;
}
/**
* As {@link ObjectFactory#buildBean(java.lang.String)}does not delegate to
* {@link ObjectFactory#buildBean(java.lang.Class)} but directly calls
* clazz.newInstance()
, overwrite this method to call
* buildBean()
*
* @see com.opensymphony.xwork.ObjectFactory#buildBean(java.lang.String)
*/
public Object buildBean(String className) throws Exception {
Class actionClass = actionsContainerFactory.getActionClass(className);
return buildBean(actionClass);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy