Revision: 808
          http://stripes.svn.sourceforge.net/stripes/?rev=808&view=rev
Author:   mongus
Date:     2008-01-24 12:37:12 -0800 (Thu, 24 Jan 2008)

Log Message:
-----------
STS-492: auto-discovery code moved into BootstrapPropertyResolver

Modified Paths:
--------------
    
trunk/stripes/src/net/sourceforge/stripes/config/BootstrapPropertyResolver.java
    trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java

Modified: 
trunk/stripes/src/net/sourceforge/stripes/config/BootstrapPropertyResolver.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/config/BootstrapPropertyResolver.java 
    2008-01-24 20:12:32 UTC (rev 807)
+++ 
trunk/stripes/src/net/sourceforge/stripes/config/BootstrapPropertyResolver.java 
    2008-01-24 20:37:12 UTC (rev 808)
@@ -14,8 +14,18 @@
  */
 package net.sourceforge.stripes.config;
 
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
+
 import javax.servlet.FilterConfig;
 
+import net.sourceforge.stripes.exception.StripesRuntimeException;
+import net.sourceforge.stripes.util.Log;
+import net.sourceforge.stripes.util.ReflectUtil;
+import net.sourceforge.stripes.util.ResolverUtil;
+import net.sourceforge.stripes.util.StringUtil;
+
 /**
  * <p>Resolves configuration properties that are used to bootstrap the system. 
 Essentially this boils
  * down to a handful of properties that are needed to figure out which 
configuration class should
@@ -33,8 +43,13 @@
  * @author Tim Fennell
  */
 public class BootstrapPropertyResolver {
+    private static final Log log = 
Log.getInstance(BootstrapPropertyResolver.class);
+    
     private FilterConfig filterConfig;
 
+    /** The Configuration Key for looking up the comma separated list of 
extension packages. */
+    public static final String EXTENSION_LIST = "Extension.Packages";
+
     /** Constructs a new BootstrapPropertyResolver with the given 
ServletConfig. */
     public BootstrapPropertyResolver(FilterConfig filterConfig) {
         setFilterConfig(filterConfig);
@@ -70,4 +85,124 @@
 
         return value;
     }
+    
+    /**
+     * Attempts to find a class the user has specified in web.xml or by 
auto-discovery in packages listed in web.xml under Extension.Packages. Classes 
specified in web.xml take precedence.   
+     * 
+     * @param paramName the parameter to look for in web.xml
+     * @param targetType the type that we're looking for
+     * @return the Class that was found
+     */
+    @SuppressWarnings("unchecked")
+    public <T> Class<? extends T> getClassProperty(String paramName, Class<T> 
targetType)
+    {
+        Class<? extends T> clazz = null;
+        
+        String className = getProperty(paramName);
+        
+        if (className != null)
+        {
+            // web.xml takes precedence
+            try {
+                clazz = (Class<? extends T>) ReflectUtil.findClass(className);
+                log.info("Class implementing/extending ", 
targetType.getSimpleName(), " found in web.xml: ", className);
+            }
+            catch (ClassNotFoundException e) {
+                log.error("Couldn't find class specified in web.xml under 
param ", paramName, ": ", className);
+            }
+        }
+        else
+        {
+            // we didn't find it in web.xml so now we check any extension 
packages
+            ResolverUtil<T> resolver = new ResolverUtil<T>();
+            String[] packages = 
StringUtil.standardSplit(getProperty(EXTENSION_LIST));
+            resolver.findImplementations(targetType, packages);
+            Set<Class<? extends T>> classes = resolver.getClasses();
+            if (classes.size() == 1) {
+                clazz = classes.iterator().next();
+                className = clazz.getName();
+                log.info("Class implementing/extending  ", 
targetType.getSimpleName(), " found via auto-discovery: ", className);
+            }
+            else if (classes.size() > 1) {
+                throw new 
StripesRuntimeException(StringUtil.combineParts("Found too many classes 
implementing/extending ",
+                                        targetType.getSimpleName(),
+                                        ": ",
+                                        classes));
+            }
+        }
+
+        return clazz;
+    }
+    
+    /**
+     * Attempts to find all classes the user has specified in web.xml.
+     *
+     * @param paramName the parameter to look for in web.xml
+     * @return a List of classes found
+     */
+    @SuppressWarnings("unchecked")
+    public List<Class> getClassPropertyList(String paramName)
+    {
+        List<Class> classes = new Vector<Class>();
+        
+        String classList = getProperty(paramName);
+        
+        if (classList != null) {
+            String[] classNames = StringUtil.standardSplit(classList);
+            for (String className : classNames) {
+                className = className.trim();
+                try {
+                    classes.add(ReflectUtil.findClass(className));
+                }
+                catch (ClassNotFoundException e) {
+                    throw new StripesRuntimeException("Could not find 
configured Interceptor ["
+                            + className + "]. The " + "property '" + paramName
+                            + "' contained [" + classList
+                            + "]. This value must contain fully qualified 
class names separated "
+                            + "by commas.");
+                }
+            }
+        }
+
+        return classes;
+    }
+    
+    /**
+     * Attempts to find classes by auto-discovery in packages listed in 
web.xml under Extension.Packages.   
+     * 
+     * @param targetType the type that we're looking for
+     * @return a List of classes found
+     */
+    public <T> List<Class<? extends T>> getClassPropertyList(Class<T> 
targetType)
+    {
+        List<Class<? extends T>> classes = new Vector<Class<? extends T>>();
+
+        ResolverUtil<T> resolver = new ResolverUtil<T>();
+        String[] packages = 
StringUtil.standardSplit(getProperty(EXTENSION_LIST));
+        resolver.findImplementations(targetType, packages);
+        classes.addAll(resolver.getClasses());
+
+        return classes;
+    }
+
+    /**
+     * Attempts to find all matching classes the user has specified in web.xml 
or by auto-discovery in packages listed in web.xml under Extension.Packages.   
+     * 
+     * @param paramName the parameter to look for in web.xml
+     * @param targetType the type that we're looking for
+     * @return the Class that was found
+     */
+    @SuppressWarnings("unchecked")
+    public <T> List<Class<? extends T>> getClassPropertyList(String paramName, 
Class<T> targetType)
+    {
+        List<Class<? extends T>> classes = new Vector<Class<? extends T>>();
+        
+        for (Class<?> clazz : getClassPropertyList(paramName))
+            // can't use addAll :(
+            classes.add((Class<? extends T>) clazz);
+        
+        classes.addAll(getClassPropertyList(targetType));
+
+        return classes;
+    }
 }

Modified: 
trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java  
2008-01-24 20:12:32 UTC (rev 807)
+++ trunk/stripes/src/net/sourceforge/stripes/config/RuntimeConfiguration.java  
2008-01-24 20:37:12 UTC (rev 808)
@@ -17,9 +17,8 @@
 import java.lang.reflect.Type;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import net.sourceforge.stripes.controller.ActionBeanContextFactory;
 import net.sourceforge.stripes.controller.ActionBeanPropertyBinder;
@@ -37,8 +36,6 @@
 import net.sourceforge.stripes.tag.TagErrorRendererFactory;
 import net.sourceforge.stripes.util.Log;
 import net.sourceforge.stripes.util.ReflectUtil;
-import net.sourceforge.stripes.util.ResolverUtil;
-import net.sourceforge.stripes.util.StringUtil;
 import net.sourceforge.stripes.validation.TypeConverter;
 import net.sourceforge.stripes.validation.TypeConverterFactory;
 import net.sourceforge.stripes.validation.ValidationMetadataProvider;
@@ -107,9 +104,6 @@
     /** The Configuration Key for looking up the comma separated list of 
interceptor classes. */
     public static final String INTERCEPTOR_LIST = "Interceptor.Classes";
     
-    /** The Configuration Key for looking up the comma separated list of 
extension packages. */
-    public static final String EXTENSION_LIST = "Extension.Packages";
-
     /** Looks for a true/false value in config. */
     @Override protected Boolean initDebugMode() {
         try {
@@ -195,7 +189,7 @@
         if (classList == null)
             return super.initCoreInterceptors();
         else
-            return initInterceptors(classList, false);
+            return 
initInterceptors(getBootstrapPropertyResolver().getClassPropertyList(CORE_INTERCEPTOR_LIST));
     }
 
     /**
@@ -208,8 +202,7 @@
      */
        @Override
     protected Map<LifecycleStage, Collection<Interceptor>> initInterceptors() {
-        String classList = 
getBootstrapPropertyResolver().getProperty(INTERCEPTOR_LIST);
-        return initInterceptors(classList, true);
+        return 
initInterceptors(getBootstrapPropertyResolver().getClassPropertyList(INTERCEPTOR_LIST,
 Interceptor.class));
     }
 
     /**
@@ -220,39 +213,13 @@
      * @return a Map of [EMAIL PROTECTED] LifecycleStage} to Collection of 
[EMAIL PROTECTED] Interceptor}
      */
     @SuppressWarnings("unchecked")
-    protected Map<LifecycleStage, Collection<Interceptor>> initInterceptors(
-            String classList, boolean searchExtensionPackages) {
-        Set<Class<? extends Interceptor>> classes = new HashSet<Class<? 
extends Interceptor>>();
-        if (classList != null) {
-            String[] classNames = StringUtil.standardSplit(classList);
-            for (String className : classNames) {
-                className = className.trim();
-                try {
-                    classes.add(ReflectUtil.findClass(className));
-                }
-                catch (ClassNotFoundException e) {
-                    throw new StripesRuntimeException("Could not find 
configured Interceptor ["
-                            + className + "]. The " + "property '" + 
INTERCEPTOR_LIST
-                            + "' contained [" + classList
-                            + "]. This value must contain fully qualified 
class names separated "
-                            + "by commas.");
-                }
-            }
-        }
+    protected Map<LifecycleStage, Collection<Interceptor>> 
initInterceptors(List classes) {
 
-        if (searchExtensionPackages) {
-            ResolverUtil<Interceptor> resolver = new 
ResolverUtil<Interceptor>();
-            String[] packages = 
StringUtil.standardSplit(getBootstrapPropertyResolver()
-                    .getProperty(EXTENSION_LIST));
-            resolver.findImplementations(Interceptor.class, packages);
-            classes.addAll(resolver.getClasses());
-        }
-        
         Map<LifecycleStage, Collection<Interceptor>> map = new 
HashMap<LifecycleStage, Collection<Interceptor>>();
 
-        for (Class<? extends Interceptor> type : classes) {
+        for (Object type : classes) {
             try {
-                Interceptor interceptor = type.newInstance();
+                Interceptor interceptor = (Interceptor) ((Class) 
type).newInstance();
                 addInterceptor(map, interceptor);
             }
             catch (Exception e) {
@@ -275,98 +242,71 @@
     @SuppressWarnings("unchecked")
        protected <T extends ConfigurableComponent> T 
initializeComponent(Class<T> componentType,
                                                                       String 
propertyName) {
-        T component = null;
+        Class clazz = null;
         
-        String componentTypeName = componentType.getSimpleName();
-        String className = 
getBootstrapPropertyResolver().getProperty(propertyName);
-
-        try {
-            if (className != null) {
-                log.info("Found configured ", componentTypeName, " class [", 
className,
-                        "], attempting to instantiate and initialize.");
-
-                component = (T) ReflectUtil.findClass(className).newInstance();
-            }
-            else {
-                ResolverUtil<T> resolver = new ResolverUtil<T>();
-                String[] packages = 
StringUtil.standardSplit(getBootstrapPropertyResolver().getProperty(EXTENSION_LIST));
-                resolver.findImplementations(componentType, packages);
-                Set<Class<? extends T>> classes = resolver.getClasses();
-                if (classes.size() == 1) {
-                    Class<? extends T> clazz = classes.iterator().next();
-                    className = clazz.getName();
-                    log.info("Automatically found configured ", 
componentTypeName, " class [", className,
-                            "], attempting to instantiate and initialize.");
-                    component = clazz.newInstance();
-                }
-                else if (classes.size() > 1) {
-                    throw new StripesRuntimeException("Found too many classes 
implementing " + componentTypeName + ": " + classes);
-                }
-            }
-
-            if (component != null) {
+        clazz = getBootstrapPropertyResolver().getClassProperty(propertyName, 
componentType);
+        
+        if (clazz != null) {
+            try {
+                T component = (T) clazz.newInstance();
                 component.init(this);
                 return component;
             }
-            else {
-                return null;
+            catch (Exception e) {
+                throw new StripesRuntimeException("Could not instantiate 
configured "
+                        + componentType.getSimpleName() + " of type [" + 
clazz.getSimpleName()
+                        + "]. Please check "
+                        + "the configuration parameters specified in your 
web.xml.", e);
+
             }
         }
-        catch (Exception e) {
-            throw new StripesRuntimeException("Could not instantiate 
configured "
-                    + componentTypeName + " of type [" + className
-                    + "]. Please check "
-                    + "the configuration parameters specified in your 
web.xml.", e);
-        }
+        else
+            return null;
     }
 
     /**
      * Calls super.init() then adds Formatters and TypeConverters found in
      * packages listed in [EMAIL PROTECTED] #EXTENSION_LIST} to their 
respective factories.
      */
+    @SuppressWarnings("unchecked")
     @Override
     public void init() {
         super.init();
         
-        String[] packages = 
StringUtil.standardSplit(getBootstrapPropertyResolver().getProperty(
-                EXTENSION_LIST));
-
-        Set<Class<? extends Formatter<?>>> formatters = new 
ResolverUtil<Formatter<?>>()
-                .findImplementations(Formatter.class, packages).getClasses();
-        for (Class<? extends Formatter<?>> formatter : formatters) {
+        List<Class<? extends Formatter>> formatters = 
getBootstrapPropertyResolver().getClassPropertyList(Formatter.class);
+        for (Class<? extends Formatter> formatter : formatters) {
             Type[] typeArguments = 
ReflectUtil.getActualTypeArguments(formatter, Formatter.class);
             log.trace("Found Formatter [", formatter, "] - type parameters: ", 
typeArguments);
             if ((typeArguments != null) && (typeArguments.length == 1)
                     && !typeArguments[0].equals(Object.class)) {
                 log.debug("Adding auto-discovered Formatter [", formatter, "] 
for [", typeArguments[0], "] (from type parameter)");
-                getFormatterFactory().add((Class<?>) typeArguments[0], 
formatter);
+                getFormatterFactory().add((Class<?>) typeArguments[0], 
(Class<? extends Formatter<?>>) formatter);
             }
             
             TargetTypes targetTypes = 
formatter.getAnnotation(TargetTypes.class);
             if (targetTypes != null) {
                 for (Class<?> targetType : targetTypes.value()) {
                     log.debug("Adding auto-discovered Formatter [", formatter, 
"] for [", targetType, "] (from TargetTypes annotation)");
-                    getFormatterFactory().add(targetType, formatter);
+                    getFormatterFactory().add(targetType, (Class<? extends 
Formatter<?>>) formatter);
                 }
             }
         }
 
-        Set<Class<? extends TypeConverter<?>>> typeConverters = new 
ResolverUtil<TypeConverter<?>>()
-                .findImplementations(TypeConverter.class, 
packages).getClasses();
-        for (Class<? extends TypeConverter<?>> typeConverter : typeConverters) 
{
+        List<Class<? extends TypeConverter>> typeConverters = 
getBootstrapPropertyResolver().getClassPropertyList(TypeConverter.class);;
+        for (Class<? extends TypeConverter> typeConverter : typeConverters) {
             Type[] typeArguments = 
ReflectUtil.getActualTypeArguments(typeConverter, TypeConverter.class);
             log.trace("Found TypeConverter [", typeConverter, "] - type 
parameters: ", typeArguments);
             if ((typeArguments != null) && (typeArguments.length == 1)
                     && !typeArguments[0].equals(Object.class)) {
                 log.debug("Adding auto-discovered TypeConverter [", 
typeConverter, "] for [", typeArguments[0], "] (from type parameter)");
-                getTypeConverterFactory().add((Class<?>) typeArguments[0], 
typeConverter);
+                getTypeConverterFactory().add((Class<?>) typeArguments[0], 
(Class<? extends TypeConverter<?>>) typeConverter);
             }
             
             TargetTypes targetTypes = 
typeConverter.getAnnotation(TargetTypes.class);
             if (targetTypes != null) {
                 for (Class<?> targetType : targetTypes.value()) {
                     log.debug("Adding auto-discovered TypeConverter [", 
typeConverter, "] for [", targetType, "] (from TargetTypes annotation)");
-                    getTypeConverterFactory().add(targetType, typeConverter);
+                    getTypeConverterFactory().add(targetType, (Class<? extends 
TypeConverter<?>>) typeConverter);
                 }
             }
         }


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: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to