Revision: 619
http://stripes.svn.sourceforge.net/stripes/?rev=619&view=rev
Author: bengunter
Date: 2007-11-11 20:42:24 -0800 (Sun, 11 Nov 2007)
Log Message:
-----------
Fixed STS-428: separate core interceptors from add-ons. Setting
Interceptor.Classes no longer affects the core interceptors. The new
CoreInterceptor.Classes property may be used to change the list of core
interceptors.
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/config/DefaultConfiguration.java
trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java
Modified:
trunk/stripes/src/net/sourceforge/stripes/config/DefaultConfiguration.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/config/DefaultConfiguration.java
2007-11-09 03:07:00 UTC (rev 618)
+++ trunk/stripes/src/net/sourceforge/stripes/config/DefaultConfiguration.java
2007-11-12 04:42:24 UTC (rev 619)
@@ -14,6 +14,14 @@
*/
package net.sourceforge.stripes.config;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
import net.sourceforge.stripes.controller.ActionBeanContextFactory;
import net.sourceforge.stripes.controller.ActionBeanPropertyBinder;
import net.sourceforge.stripes.controller.ActionResolver;
@@ -24,8 +32,8 @@
import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.controller.NameBasedActionResolver;
+import
net.sourceforge.stripes.controller.multipart.DefaultMultipartWrapperFactory;
import net.sourceforge.stripes.controller.multipart.MultipartWrapperFactory;
-import
net.sourceforge.stripes.controller.multipart.DefaultMultipartWrapperFactory;
import net.sourceforge.stripes.exception.DefaultExceptionHandler;
import net.sourceforge.stripes.exception.ExceptionHandler;
import net.sourceforge.stripes.exception.StripesRuntimeException;
@@ -39,16 +47,10 @@
import net.sourceforge.stripes.tag.DefaultTagErrorRendererFactory;
import net.sourceforge.stripes.tag.PopulationStrategy;
import net.sourceforge.stripes.tag.TagErrorRendererFactory;
+import net.sourceforge.stripes.util.Log;
import net.sourceforge.stripes.validation.DefaultTypeConverterFactory;
import net.sourceforge.stripes.validation.TypeConverterFactory;
-import javax.servlet.ServletContext;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
/**
* <p>Centralized location for defaults for all Configuration properties.
This implementation does
* not lookup configuration information anywhere! It returns hard-coded
defaults that will result
@@ -70,6 +72,9 @@
* @author Tim Fennell
*/
public class DefaultConfiguration implements Configuration {
+ /** Log implementation for use within this class. */
+ private static final Log log = Log.getInstance(DefaultConfiguration.class);
+
private BootstrapPropertyResolver resolver;
private ActionResolver actionResolver;
private ActionBeanPropertyBinder actionBeanPropertyBinder;
@@ -161,18 +166,15 @@
this.multipartWrapperFactory.init(this);
}
- this.interceptors = initInterceptors();
- if (this.interceptors == null) {
- this.interceptors = new HashMap<LifecycleStage,
Collection<Interceptor>>();
- Class<? extends Interceptor> bam =
BeforeAfterMethodInterceptor.class;
- BeforeAfterMethodInterceptor interceptor = new
BeforeAfterMethodInterceptor();
-
- for (LifecycleStage stage :
bam.getAnnotation(Intercepts.class).value()) {
- Collection<Interceptor> instances = new
ArrayList<Interceptor>();
- instances.add(interceptor);
- this.interceptors.put(stage, instances);
- }
+ this.interceptors = new HashMap<LifecycleStage,
Collection<Interceptor>>();
+ Map<LifecycleStage, Collection<Interceptor>> map =
initCoreInterceptors();
+ if (map != null) {
+ this.interceptors.putAll(map);
}
+ map = initInterceptors();
+ if (map != null) {
+ this.interceptors.putAll(map);
+ }
}
catch (Exception e) {
throw new StripesRuntimeException
@@ -331,7 +333,57 @@
}
return interceptors;
}
+
+ /**
+ * Adds the interceptor to the map, associating it with the [EMAIL
PROTECTED] LifecycleStage}s indicated
+ * by the [EMAIL PROTECTED] Intercepts} annotation. If the interceptor
implements
+ * [EMAIL PROTECTED] ConfigurableComponent}, then its init() method will
be called.
+ */
+ protected void addInterceptor(Map<LifecycleStage, Collection<Interceptor>>
map,
+ Interceptor interceptor) {
+ Class<? extends Interceptor> type = interceptor.getClass();
+ Intercepts intercepts = type.getAnnotation(Intercepts.class);
+ if (intercepts == null) {
+ log.error("An interceptor of type ", type.getName(), " was
configured ",
+ "but was not marked with an @Intercepts annotation. As a ",
+ "result it is not possible to determine at which ",
+ "lifecycle stages the interceptor should be applied. This
",
+ "interceptor will be ignored.");
+ return;
+ }
+ else {
+ log.debug("Configuring interceptor '", type.getSimpleName(),
+ "', for lifecycle stages: ", intercepts.value());
+ }
+ // call init() if the interceptor implements ConfigurableComponent
+ if (interceptor instanceof ConfigurableComponent) {
+ try {
+ ((ConfigurableComponent) interceptor).init(this);
+ }
+ catch (Exception e) {
+ log.error("Error initializing interceptor of type " +
type.getName(), e);
+ }
+ }
+
+ for (LifecycleStage stage : intercepts.value()) {
+ Collection<Interceptor> stack = map.get(stage);
+ if (stack == null) {
+ stack = new LinkedList<Interceptor>();
+ map.put(stage, stack);
+ }
+
+ stack.add(interceptor);
+ }
+ }
+
+ /** Instantiates the core interceptors, allowing subclasses to override
the default behavior */
+ protected Map<LifecycleStage, Collection<Interceptor>>
initCoreInterceptors() {
+ Map<LifecycleStage, Collection<Interceptor>> interceptors = new
HashMap<LifecycleStage, Collection<Interceptor>>();
+ addInterceptor(interceptors, new BeforeAfterMethodInterceptor());
+ return interceptors;
+ }
+
/** Allows subclasses to initialize a non-default Map of Interceptor
instances. */
protected Map<LifecycleStage,Collection<Interceptor>> initInterceptors() {
return null; }
}
Modified:
trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java
2007-11-09 03:07:00 UTC (rev 618)
+++ trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java
2007-11-12 04:42:24 UTC (rev 619)
@@ -14,15 +14,18 @@
*/
package net.sourceforge.stripes.config;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
import net.sourceforge.stripes.controller.ActionBeanContextFactory;
import net.sourceforge.stripes.controller.ActionBeanPropertyBinder;
import net.sourceforge.stripes.controller.ActionResolver;
import net.sourceforge.stripes.controller.Interceptor;
-import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.controller.multipart.MultipartWrapperFactory;
+import net.sourceforge.stripes.exception.ExceptionHandler;
import net.sourceforge.stripes.exception.StripesRuntimeException;
-import net.sourceforge.stripes.exception.ExceptionHandler;
import net.sourceforge.stripes.format.FormatterFactory;
import net.sourceforge.stripes.localization.LocalePicker;
import net.sourceforge.stripes.localization.LocalizationBundleFactory;
@@ -33,11 +36,6 @@
import net.sourceforge.stripes.util.StringUtil;
import net.sourceforge.stripes.validation.TypeConverterFactory;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
-
/**
* <p>Configuration class that uses the BootstrapPropertyResolver to look for
configuration values,
* and when it cannot find a value, falls back on the DefaultConfiguration to
supply default
@@ -89,6 +87,9 @@
/** The Configuration Key for looking up the name of the
MultipartWrapperFactory class */
public static final String MULTIPART_WRAPPER_FACTORY =
"MultipartWrapperFactory.Class";
+
+ /** The Configuration Key for looking up the comma separated list of core
interceptor classes. */
+ public static final String CORE_INTERCEPTOR_LIST =
"CoreInterceptor.Classes";
/** The Configuration Key for looking up the comma separated list of
interceptor classes. */
public static final String INTERCEPTOR_LIST = "Interceptor.Classes";
@@ -151,16 +152,43 @@
/**
* Looks for a list of class names separated by commas under the
configuration key
+ * [EMAIL PROTECTED] #CORE_INTERCEPTOR_LIST}. White space surrounding the
class names is trimmed,
+ * the classes instantiated and then stored under the lifecycle stage(s)
they should
+ * intercept.
+ *
+ * @return a Map of [EMAIL PROTECTED] LifecycleStage} to Collection of
[EMAIL PROTECTED] Interceptor}
+ */
+ @Override
+ protected Map<LifecycleStage, Collection<Interceptor>>
initCoreInterceptors() {
+ String classList =
getBootstrapPropertyResolver().getProperty(CORE_INTERCEPTOR_LIST);
+ if (classList == null)
+ return super.initCoreInterceptors();
+ else
+ return initInterceptors(classList);
+ }
+
+ /**
+ * Looks for a list of class names separated by commas under the
configuration key
* [EMAIL PROTECTED] #INTERCEPTOR_LIST}. White space surrounding the
class names is trimmed,
* the classes instantiated and then stored under the lifecycle stage(s)
they should
* intercept.
*
* @return a Map of [EMAIL PROTECTED] LifecycleStage} to Collection of
[EMAIL PROTECTED] Interceptor}
*/
- @SuppressWarnings("unchecked")
@Override
protected Map<LifecycleStage, Collection<Interceptor>> initInterceptors() {
String classList =
getBootstrapPropertyResolver().getProperty(INTERCEPTOR_LIST);
+ return initInterceptors(classList);
+ }
+
+ /**
+ * Splits a comma-separated list of class names and maps each [EMAIL
PROTECTED] LifecycleStage} to the
+ * interceptors in the list that intercept it.
+ *
+ * @return a Map of [EMAIL PROTECTED] LifecycleStage} to Collection of
[EMAIL PROTECTED] Interceptor}
+ */
+ @SuppressWarnings("unchecked")
+ protected Map<LifecycleStage, Collection<Interceptor>>
initInterceptors(String classList) {
if (classList == null) {
return null;
}
@@ -172,35 +200,8 @@
for (String className : classNames) {
try {
Class<? extends Interceptor> type =
ReflectUtil.findClass(className.trim());
- Intercepts intercepts =
type.getAnnotation(Intercepts.class);
- if (intercepts == null) {
- log.error("An interceptor of type ", type.getName(), "
was configured ",
- "but was not marked with an @Intercepts
annotation. As a ",
- "result it is not possible to determine at
which ",
- "lifecycle stages the interceprot should be
applied. This ",
- "interceptor will be ignored.");
- }
- else {
- log.debug("Configuring interceptor '",
type.getSimpleName(),
- "', for lifecycle stages: ",
intercepts.value());
- }
-
- // Instantiate it and optionally call init() if the
interceptor
- // implements ConfigurableComponent
Interceptor interceptor = type.newInstance();
- if (interceptor instanceof ConfigurableComponent) {
- ((ConfigurableComponent) interceptor).init(this);
- }
-
- for (LifecycleStage stage : intercepts.value()) {
- Collection<Interceptor> stack = map.get(stage);
- if (stack == null) {
- stack = new LinkedList<Interceptor>();
- map.put(stage, stack);
- }
-
- stack.add(interceptor);
- }
+ addInterceptor(map, interceptor);
}
catch (Exception e) {
throw new StripesRuntimeException(
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development