Minor code improvement's in the struts spring plugin

- Use Java 7 features like diamond operator
- Improve some logging message
- Remove out commented code
- Use BooleanUtils.toBoolean(string) instead of "true".isEquals to be more robust
This commit is contained in:
Johannes Geppert
2015-06-08 19:51:09 +02:00
parent e47a1127f8
commit decf48eb71
3 changed files with 21 additions and 42 deletions
@@ -43,22 +43,12 @@ public class ClassReloadingBeanFactory extends DefaultListableBeanFactory {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
//commented to cached constructor is not used
/* // Shortcut when re-creating the same bean...
if (mbd.resolvedConstructorOrFactoryMethod != null) {
if (mbd.constructorArgumentsResolved) {
return autowireConstructor(beanName, mbd, null, args);
} else {
return instantiateBean(beanName, mbd);
}
}*/
// Need to determine the constructor...
Constructor[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
Constructor[] constructors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (constructors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
return autowireConstructor(beanName, mbd, constructors, args);
}
// No special handling: simply use no-arg constructor.
@@ -67,10 +57,6 @@ public class ClassReloadingBeanFactory extends DefaultListableBeanFactory {
protected Class resolveBeanClass(RootBeanDefinition mbd, String beanName, Class[] typesToMatch) {
try {
//commented to cached class is not used
/* if (mbd.hasBeanClass()) {
return mbd.getBeanClass();
}*/
if (typesToMatch != null) {
ClassLoader tempClassLoader = getTempClassLoader();
if (tempClassLoader != null) {
@@ -74,7 +74,7 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
private static final Logger LOG = LogManager.getLogger(ClassReloadingXMLWebApplicationContext.class);
protected ReloadingClassLoader classLoader;
protected FilesystemAlterationMonitor fam;
protected FilesystemAlterationMonitor filesystemAlterationMonitor;
protected ClassReloadingBeanFactory beanFactory;
//reload the runtime configuration when a change is detected
@@ -88,14 +88,14 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
//make a list of accepted classes
if (StringUtils.isNotBlank(acceptClasses)) {
String[] splitted = acceptClasses.split(",");
Set<Pattern> patterns = new HashSet<Pattern>(splitted.length);
Set<Pattern> patterns = new HashSet<>(splitted.length);
for (String pattern : splitted)
patterns.add(Pattern.compile(pattern));
classLoader.setAccepClasses(patterns);
}
fam = new FilesystemAlterationMonitor();
filesystemAlterationMonitor = new FilesystemAlterationMonitor();
//setup stores
for (String watch : watchList) {
@@ -107,19 +107,19 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
if (watch.endsWith(".jar")) {
classLoader.addResourceStore(new JarResourceStore(file));
//register with the fam
fam.addListener(file, this);
//register with the filesystemAlterationMonitor
filesystemAlterationMonitor.addListener(file, this);
LOG.debug("Watching [{}] for changes", file.getAbsolutePath());
} else {
//get all subdirs
List<File> dirs = new ArrayList<File>();
List<File> dirs = new ArrayList<>();
getAllPaths(file, dirs);
classLoader.addResourceStore(new FileResourceStore(file));
for (File dir : dirs) {
//register with the fam
fam.addListener(dir, this);
//register with the filesystemAlterationMonitor
filesystemAlterationMonitor.addListener(dir, this);
LOG.debug("Watching [{}] for changes", dir.getAbsolutePath());
}
}
@@ -130,11 +130,11 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
beanFactory.setBeanClassLoader(classLoader);
//start watch thread
fam.start();
filesystemAlterationMonitor.start();
}
/**
* If root is a dir, find al the subdir paths
* If root is a dir, find all the subdir paths
*/
private void getAllPaths(File root, List<File> dirs) {
dirs.add(root);
@@ -154,9 +154,9 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
public void close() {
super.close();
if (fam != null) {
fam.removeListener(this);
fam.stop();
if (filesystemAlterationMonitor != null) {
filesystemAlterationMonitor.removeListener(this);
filesystemAlterationMonitor.stop();
}
}
@@ -176,8 +176,9 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
super.prepareBeanFactory(beanFactory);
//overwrite the class loader in the bean factory
if (classLoader != null)
if (classLoader != null) {
beanFactory.setBeanClassLoader(classLoader);
}
}
public void onDirectoryChange(File file) {
@@ -201,7 +202,6 @@ public class ClassReloadingXMLWebApplicationContext extends XmlWebApplicationCon
private void reload(File file) {
if (classLoader != null) {
final boolean debugEnabled = LOG.isDebugEnabled();
LOG.debug("Change detected in file [{}], reloading class loader", file.getAbsolutePath());
classLoader.reload();
if (reloadConfig && Dispatcher.getInstance() != null) {
@@ -24,6 +24,7 @@ package org.apache.struts2.spring;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.spring.SpringObjectFactory;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -46,14 +47,6 @@ import javax.servlet.ServletContext;
public class StrutsSpringObjectFactory extends SpringObjectFactory {
private static final Logger LOG = LogManager.getLogger(StrutsSpringObjectFactory.class);
//@Inject
//public StrutsSpringObjectFactory(
// @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
// @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
// @Inject ServletContext servletContext) {
// this(autoWire, "false", useClassCacheStr, servletContext);
//}
/**
* Constructs the spring object factory
* @param autoWire The type of autowiring to use
@@ -73,7 +66,7 @@ public class StrutsSpringObjectFactory extends SpringObjectFactory {
@Inject Container container) {
super();
boolean useClassCache = "true".equals(useClassCacheStr);
boolean useClassCache = BooleanUtils.toBoolean(useClassCacheStr);
LOG.info("Initializing Struts-Spring integration...");
Object rootWebApplicationContext = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
@@ -136,7 +129,7 @@ public class StrutsSpringObjectFactory extends SpringObjectFactory {
this.setUseClassCache(useClassCache);
this.setAlwaysRespectAutowireStrategy("true".equalsIgnoreCase(alwaysAutoWire));
this.setAlwaysRespectAutowireStrategy(BooleanUtils.toBoolean(alwaysAutoWire));
this.setEnableAopSupport(enableAopSupport);