001: /*****************************************************************************
002: * Copyright (C) NanoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: *****************************************************************************/package com.opensymphony.webwork.pico;
009:
010: import java.util.Map;
011:
012: import javax.servlet.http.HttpServletRequest;
013:
014: import org.nanocontainer.nanowar.ActionsContainerFactory;
015: import org.picocontainer.MutablePicoContainer;
016: import org.picocontainer.defaults.DefaultPicoContainer;
017: import org.picocontainer.defaults.ObjectReference;
018:
019: import com.opensymphony.xwork.ObjectFactory;
020: import com.opensymphony.xwork.Result;
021: import com.opensymphony.xwork.config.ConfigurationException;
022: import com.opensymphony.xwork.config.entities.ActionConfig;
023: import com.opensymphony.xwork.config.entities.InterceptorConfig;
024: import com.opensymphony.xwork.config.entities.ResultConfig;
025: import com.opensymphony.xwork.interceptor.Interceptor;
026: import com.opensymphony.xwork.validator.Validator;
027:
028: /**
029: * <p>
030: * XWork ObjectFactory which uses a PicoContainer to create component instances.
031: * </p>
032: *
033: * @author Cyrille Le Clerc
034: * @author Jonas Engman
035: * @author Mauro Talevi
036: * @author Grégory Joseph
037: */
038: public class PicoObjectFactory extends ObjectFactory {
039:
040: private final ActionsContainerFactory actionsContainerFactory = new ActionsContainerFactory();
041: private final ObjectReference objectReference;
042:
043: /**
044: * Creates a PicoObjectFactory with given object reference,
045: * used to pass the http request to the factory
046: *
047: * @param objectReference the ObjectReference
048: */
049: public PicoObjectFactory(ObjectReference objectReference) {
050: this .objectReference = objectReference;
051: }
052:
053: public boolean isNoArgConstructorRequired() {
054: return false;
055: }
056:
057: /**
058: * Webwork-2.2 / XWork-1.1 method. ExtraContext can be ignored.
059: */
060: public Object buildBean(Class clazz, Map extraContext)
061: throws Exception {
062: return buildBean(clazz);
063: }
064:
065: /**
066: * Webwork-2.2 / XWork-1.1 method. ExtraContext can be ignored.
067: */
068: public Object buildBean(String className, Map extraContext)
069: throws Exception {
070: return buildBean(className);
071: }
072:
073: /**
074: * Webwork-2.2 / XWork-1.1 method. Used to validate a class be loaded.
075: * Using actionsContainerFactory for consistency with build methods.
076: */
077: public Class getClassInstance(String className)
078: throws ClassNotFoundException {
079: return actionsContainerFactory.getActionClass(className);
080: }
081:
082: public Object buildAction(String actionName, String namespace,
083: ActionConfig config, Map extraContext) throws Exception {
084: return super .buildAction(actionName, namespace, config,
085: extraContext);
086: }
087:
088: public Interceptor buildInterceptor(
089: InterceptorConfig interceptorConfig,
090: Map interceptorRefParams) throws ConfigurationException {
091: return super .buildInterceptor(interceptorConfig,
092: interceptorRefParams);
093: }
094:
095: public Result buildResult(ResultConfig resultConfig,
096: Map extraContext) throws Exception {
097: return super .buildResult(resultConfig, extraContext);
098: }
099:
100: public Validator buildValidator(String className, Map params,
101: Map extraContext) throws Exception {
102: return super .buildValidator(className, params, extraContext);
103: }
104:
105: /**
106: * Instantiates an action using the PicoContainer found in the request scope.
107: */
108: public Object buildBean(Class actionClass) throws Exception {
109: MutablePicoContainer actionsContainer = actionsContainerFactory
110: .getActionsContainer((HttpServletRequest) objectReference
111: .get());
112: Object action = actionsContainer
113: .getComponentInstance(actionClass);
114:
115: if (action == null) {
116: // The action wasn't registered. Attempt to instantiate it.
117: MutablePicoContainer container = new DefaultPicoContainer(
118: actionsContainer);
119: container.registerComponentImplementation(actionClass);
120: action = container.getComponentInstance(actionClass);
121: }
122: return action;
123: }
124:
125: public Object buildBean(String className) throws Exception {
126: Class actionClass = actionsContainerFactory
127: .getActionClass(className);
128: return buildBean(actionClass);
129: }
130: }
|