Search in sources :

Example 21 with Factory

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

the class ActionAutowiringInterceptor method intercept.

/**
 * <p>
 * Looks for the <code>ApplicationContext</code> under the attribute that the Spring listener sets in
 * the servlet context.  The configuration is done the first time here instead of in init() since the
 * <code>ActionContext</code> is not available during <code>Interceptor</code> initialization.
 * </p>
 *
 * <p>
 * Autowires the action to Spring beans and places the <code>ApplicationContext</code>
 * on the <code>ActionContext</code>
 * </p>
 *
 * <p>
 * TODO: Should this check to see if the <code>SpringObjectFactory</code> has already been configured instead of instantiating a new one?  Or is there a good reason for the interceptor to have it's own factory?
 * </p>
 *
 * @param invocation the action invocation
 * @throws Exception in case of any errors
 */
@Override
public String intercept(ActionInvocation invocation) throws Exception {
    if (!initialized) {
        ApplicationContext applicationContext = (ApplicationContext) ActionContext.getContext().getApplication().get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        if (applicationContext == null) {
            LOG.warn("ApplicationContext could not be found.  Action classes will not be autowired.");
        } else {
            setApplicationContext(applicationContext);
            factory = new SpringObjectFactory();
            factory.setContainer(ActionContext.getContext().getContainer());
            factory.setApplicationContext(getApplicationContext());
            if (autowireStrategy != null) {
                factory.setAutowireStrategy(autowireStrategy);
            }
        }
        initialized = true;
    }
    if (factory != null) {
        Object bean = invocation.getAction();
        factory.autoWireBean(bean);
    }
    return invocation.invoke();
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SpringObjectFactory(com.opensymphony.xwork2.spring.SpringObjectFactory)

Example 22 with Factory

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

the class ClassReloadingXMLWebApplicationContext method setupReloading.

public void setupReloading(String[] watchList, String acceptClasses, ServletContext servletContext, boolean reloadConfig) {
    this.reloadConfig = reloadConfig;
    classLoader = new ReloadingClassLoader(ClassReloadingXMLWebApplicationContext.class.getClassLoader());
    // make a list of accepted classes
    if (StringUtils.isNotBlank(acceptClasses)) {
        String[] splitted = acceptClasses.split(",");
        Set<Pattern> patterns = new HashSet<>(splitted.length);
        for (String pattern : splitted) patterns.add(Pattern.compile(pattern));
        classLoader.setAccepClasses(patterns);
    }
    filesystemAlterationMonitor = new FilesystemAlterationMonitor();
    // setup stores
    for (String watch : watchList) {
        File file = new File(watch);
        // make it absolute, if it is a relative path
        if (!file.isAbsolute())
            file = new File(servletContext.getRealPath(watch));
        if (watch.endsWith(".jar")) {
            classLoader.addResourceStore(new JarResourceStore(file));
            // register with the filesystemAlterationMonitor
            filesystemAlterationMonitor.addListener(file, this);
            LOG.debug("Watching [{}] for changes", file.getAbsolutePath());
        } else {
            // get all subdirs
            List<File> dirs = new ArrayList<>();
            getAllPaths(file, dirs);
            classLoader.addResourceStore(new FileResourceStore(file));
            for (File dir : dirs) {
                // register with the filesystemAlterationMonitor
                filesystemAlterationMonitor.addListener(dir, this);
                LOG.debug("Watching [{}] for changes", dir.getAbsolutePath());
            }
        }
    }
    // setup the bean factory
    beanFactory = new ClassReloadingBeanFactory();
    beanFactory.setInstantiationStrategy(new ClassReloadingInstantiationStrategy());
    beanFactory.setBeanClassLoader(classLoader);
    // start watch thread
    filesystemAlterationMonitor.start();
}
Also used : Pattern(java.util.regex.Pattern) FileResourceStore(com.opensymphony.xwork2.util.classloader.FileResourceStore) ArrayList(java.util.ArrayList) ReloadingClassLoader(com.opensymphony.xwork2.util.classloader.ReloadingClassLoader) FilesystemAlterationMonitor(org.apache.commons.jci.monitor.FilesystemAlterationMonitor) JarResourceStore(com.opensymphony.xwork2.util.classloader.JarResourceStore) File(java.io.File) HashSet(java.util.HashSet)

Example 23 with Factory

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

the class InterceptorBuilder method constructInterceptorReference.

/**
 * Builds a list of interceptors referenced by the refName in the supplied PackageConfig (InterceptorMapping object).
 *
 * @param interceptorLocator interceptor locator
 * @param refName reference name
 * @param refParams reference parameters
 * @param location location
 * @param objectFactory object factory
 * @return list of interceptors referenced by the refName in the supplied PackageConfig (InterceptorMapping object).
 * @throws ConfigurationException in case of any configuration errors
 */
public static List<InterceptorMapping> constructInterceptorReference(InterceptorLocator interceptorLocator, String refName, Map<String, String> refParams, Location location, ObjectFactory objectFactory) throws ConfigurationException {
    Object referencedConfig = interceptorLocator.getInterceptorConfig(refName);
    List<InterceptorMapping> result = new ArrayList<>();
    if (referencedConfig == null) {
        throw new ConfigurationException("Unable to find interceptor class referenced by ref-name " + refName, location);
    } else {
        if (referencedConfig instanceof InterceptorConfig) {
            InterceptorConfig config = (InterceptorConfig) referencedConfig;
            Interceptor inter;
            try {
                inter = objectFactory.buildInterceptor(config, refParams);
                result.add(new InterceptorMapping(refName, inter, refParams));
            } catch (ConfigurationException ex) {
                LOG.warn(new ParameterizedMessage("Unable to load config class {} at {} probably due to a missing jar, which might be fine if you never plan to use the {} interceptor", config.getClassName(), ex.getLocation(), config.getName()), ex);
            }
        } else if (referencedConfig instanceof InterceptorStackConfig) {
            InterceptorStackConfig stackConfig = (InterceptorStackConfig) referencedConfig;
            if ((refParams != null) && (refParams.size() > 0)) {
                result = constructParameterizedInterceptorReferences(interceptorLocator, stackConfig, refParams, objectFactory);
            } else {
                result.addAll(stackConfig.getInterceptors());
            }
        } else {
            LOG.error("Got unexpected type for interceptor {}. Got {}", refName, referencedConfig);
        }
    }
    return result;
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) ArrayList(java.util.ArrayList) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor)

Example 24 with Factory

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

the class FreeMarkerResultTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    mgr = new FreemarkerManager();
    mgr.setEncoding("UTF-8");
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    container.inject(factory);
    mgr.setFileManagerFactory(factory);
    FreemarkerThemeTemplateLoader themeLoader = new FreemarkerThemeTemplateLoader();
    container.inject(themeLoader);
    mgr.setThemeTemplateLoader(themeLoader);
    stringWriter = new StringWriter();
    writer = new PrintWriter(stringWriter);
    response = new StrutsMockHttpServletResponse();
    response.setWriter(writer);
    request = new MockHttpServletRequest();
    servletContext = new StrutsMockServletContext();
    stack = ActionContext.getContext().getValueStack();
    context = ActionContext.of(stack.getContext()).withServletResponse(response).withServletRequest(request).withServletContext(servletContext).bind();
    servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
    invocation = new MockActionInvocation();
    invocation.setStack(stack);
    invocation.setInvocationContext(context);
    invocation.setProxy(new MockActionProxy());
    servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource("someFreeMarkerFile.ftl").toURI()).toURL().getFile());
}
Also used : DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) StringWriter(java.io.StringWriter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) StrutsMockHttpServletResponse(org.apache.struts2.views.jsp.StrutsMockHttpServletResponse) File(java.io.File) StrutsMockServletContext(org.apache.struts2.views.jsp.StrutsMockServletContext) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy) PrintWriter(java.io.PrintWriter)

Example 25 with Factory

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

the class DummyFreemarkerManager method testIfStrutsEncodingIsSetProperty.

public void testIfStrutsEncodingIsSetProperty() throws Exception {
    FreemarkerManager mgr = new FreemarkerManager();
    mgr.setEncoding("UTF-8");
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    container.inject(factory);
    mgr.setFileManagerFactory(factory);
    mgr.setThemeTemplateLoader(new FreemarkerThemeTemplateLoader());
    StrutsMockServletContext servletContext = new StrutsMockServletContext();
    servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
    freemarker.template.Configuration conf = mgr.getConfiguration(servletContext);
    assertEquals(conf.getDefaultEncoding(), "UTF-8");
}
Also used : DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) Configuration(freemarker.template.Configuration) StrutsMockServletContext(org.apache.struts2.views.jsp.StrutsMockServletContext)

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