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();
}
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();
}
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;
}
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());
}
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");
}
Aggregations