Search in sources :

Example 11 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class DummyFileManager method testCreateDefaultFileManager.

public void testCreateDefaultFileManager() throws Exception {
    // given
    fileManager = null;
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    factory.setFileManager(new DefaultFileManager());
    factory.setContainer(new DummyContainer());
    // when
    FileManager fm = factory.getFileManager();
    // then
    assertTrue(fm instanceof DefaultFileManager);
}
Also used : FileManager(com.opensymphony.xwork2.FileManager)

Example 12 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class Dispatcher method cleanup.

/**
 * Releases all instances bound to this dispatcher instance.
 */
public void cleanup() {
    // clean up ObjectFactory
    ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class);
    if (objectFactory == null) {
        LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed");
    }
    if (objectFactory instanceof ObjectFactoryDestroyable) {
        try {
            ((ObjectFactoryDestroyable) objectFactory).destroy();
        } catch (Exception e) {
            // catch any exception that may occurred during destroy() and log it
            LOG.error("Exception occurred while destroying ObjectFactory [{}]", objectFactory.toString(), e);
        }
    }
    // clean up Dispatcher itself for this thread
    instance.set(null);
    servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null);
    // clean up DispatcherListeners
    if (!dispatcherListeners.isEmpty()) {
        for (DispatcherListener l : dispatcherListeners) {
            l.dispatcherDestroyed(this);
        }
    }
    // clean up all interceptors by calling their destroy() method
    Set<Interceptor> interceptors = new HashSet<>();
    Collection<PackageConfig> packageConfigs = configurationManager.getConfiguration().getPackageConfigs().values();
    for (PackageConfig packageConfig : packageConfigs) {
        for (Object config : packageConfig.getAllInterceptorConfigs().values()) {
            if (config instanceof InterceptorStackConfig) {
                for (InterceptorMapping interceptorMapping : ((InterceptorStackConfig) config).getInterceptors()) {
                    interceptors.add(interceptorMapping.getInterceptor());
                }
            }
        }
    }
    for (Interceptor interceptor : interceptors) {
        interceptor.destroy();
    }
    // Clear container holder when application is unloaded / server shutdown
    ContainerHolder.clear();
    // cleanup action context
    ActionContext.clear();
    // clean up configuration
    configurationManager.destroyConfiguration();
    configurationManager = null;
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ObjectFactoryDestroyable(org.apache.struts2.util.ObjectFactoryDestroyable) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ServletException(javax.servlet.ServletException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Example 13 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class Jsr168Dispatcher method init.

/**
 * Initialize the portlet with the init parameters from <tt>portlet.xml</tt>
 *
 * @param cfg portlet configuration
 * @throws PortletException in case of errors
 */
public void init(PortletConfig cfg) throws PortletException {
    super.init(cfg);
    LOG.debug("Initializing portlet {}", getPortletName());
    Map<String, String> params = new HashMap<String, String>();
    for (Enumeration e = cfg.getInitParameterNames(); e.hasMoreElements(); ) {
        String name = (String) e.nextElement();
        String value = cfg.getInitParameter(name);
        params.put(name, value);
    }
    servletContext = new PortletServletContext(cfg.getPortletContext());
    dispatcherUtils = new Dispatcher(servletContext, params);
    dispatcherUtils.init();
    // For testability
    if (factory == null) {
        factory = dispatcherUtils.getContainer().getInstance(ActionProxyFactory.class);
    }
    portletNamespace = cfg.getInitParameter("portletNamespace");
    LOG.debug("PortletNamespace: {}", portletNamespace);
    parseModeConfig(actionMap, cfg, PortletMode.VIEW, "viewNamespace", "defaultViewAction");
    parseModeConfig(actionMap, cfg, PortletMode.EDIT, "editNamespace", "defaultEditAction");
    parseModeConfig(actionMap, cfg, PortletMode.HELP, "helpNamespace", "defaultHelpAction");
    parseModeConfig(actionMap, cfg, new PortletMode("config"), "configNamespace", "defaultConfigAction");
    parseModeConfig(actionMap, cfg, new PortletMode("about"), "aboutNamespace", "defaultAboutAction");
    parseModeConfig(actionMap, cfg, new PortletMode("print"), "printNamespace", "defaultPrintAction");
    parseModeConfig(actionMap, cfg, new PortletMode("preview"), "previewNamespace", "defaultPreviewAction");
    parseModeConfig(actionMap, cfg, new PortletMode("edit_defaults"), "editDefaultsNamespace", "defaultEditDefaultsAction");
    if (StringUtils.isEmpty(portletNamespace)) {
        portletNamespace = "";
    }
    container = dispatcherUtils.getContainer();
    actionMapper = container.getInstance(ActionMapper.class);
}
Also used : ActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) PortletServletContext(org.apache.struts2.portlet.servlet.PortletServletContext) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) PortletMode(javax.portlet.PortletMode)

Example 14 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class ServletActionRedirectResultTest method testBuildResultWithParameter.

public void testBuildResultWithParameter() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", ServletActionRedirectResult.class.getName()).addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").addParam("anchor", "fragment").build();
    ObjectFactory factory = container.getInstance(ObjectFactory.class);
    ServletActionRedirectResult result = (ServletActionRedirectResult) factory.buildResult(resultConfig, ActionContext.getContext().getContextMap());
    assertNotNull(result);
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory)

Example 15 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class PrefixBasedActionProxyFactoryTest method setUp.

@Override
public void setUp() throws Exception {
    ConfigurationProvider[] providers = new ConfigurationProvider[] { new StrutsXmlConfigurationProvider("xwork-sample.xml"), new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ActionProxyFactory.class, "prefix1", new Factory() {

                public Object create(Context context) throws Exception {
                    return new Prefix1Factory();
                }

                public Class type() {
                    return Prefix1Factory.class;
                }
            }, Scope.SINGLETON);
        }
    }, new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ActionProxyFactory.class, "prefix2", new Factory() {

                public Object create(Context context) throws Exception {
                    return new Prefix2Factory();
                }

                public Class type() {
                    return Prefix2Factory.class;
                }
            }, Scope.SINGLETON);
        }
    } };
    loadConfigurationProviders(providers);
    factory = new PrefixBasedActionProxyFactory();
    factory.setContainer(container);
}
Also used : Context(com.opensymphony.xwork2.inject.Context) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) Factory(com.opensymphony.xwork2.inject.Factory) ActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory) DefaultActionProxyFactory(com.opensymphony.xwork2.DefaultActionProxyFactory) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties)

Aggregations

ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)10 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)7 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)5 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)4 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 StrutsException (org.apache.struts2.StrutsException)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 DefaultActionProxyFactory (com.opensymphony.xwork2.DefaultActionProxyFactory)3 FileManager (com.opensymphony.xwork2.FileManager)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 Factory (com.opensymphony.xwork2.inject.Factory)3 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)3 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3