You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2006/03/11 18:48:36 UTC

svn commit: r385114 - in /jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view: ViewToolInfo.java servlet/ServletToolInfo.java tools/Configurable.java tools/ViewTool.java

Author: nbubna
Date: Sat Mar 11 09:48:34 2006
New Revision: 385114

URL: http://svn.apache.org/viewcvs?rev=385114&view=rev
Log:
use reflection instead of interfaces to determine if tools are init'able or configurable

Modified:
    jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/ViewToolInfo.java
    jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
    jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/Configurable.java
    jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ViewTool.java

Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/ViewToolInfo.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/ViewToolInfo.java?rev=385114&r1=385113&r2=385114&view=diff
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/ViewToolInfo.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/ViewToolInfo.java Sat Mar 11 09:48:34 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,18 +16,20 @@
 
 package org.apache.velocity.tools.view;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.velocity.tools.view.tools.Configurable;
-import org.apache.velocity.tools.view.tools.ViewTool;
 
 /**
  * ToolInfo implementation for view tools. New instances
  * are returned for every call to getInstance(obj), and tools
- * that implement {@link ViewTool} are initialized with the
- * given object before being returned.
+ * that have an init(Object) method are initialized with the
+ * given object before being returned. And tools that have a
+ * configure(Map) method will be configured before being returned
+ * if there are parameters specified for the tool.
  *
  * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
  * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
@@ -40,8 +42,8 @@
     private String key;
     private Class clazz;
     private Map parameters;
-    private boolean initializable = false;
-    private boolean configurable = false;
+    private Method init = null;
+    private Method configure = null;
 
     public ViewToolInfo() {}
 
@@ -87,15 +89,25 @@
         if (classname != null && classname.length() != 0)
         {
             this.clazz = getApplicationClass(classname);
-            /* create an instance and see if it is a ViewTool or Configurable */
+            // create an instance to make sure we can
             Object instance = clazz.newInstance();
-            if (instance instanceof ViewTool)
+            try
             {
-                this.initializable = true;
+                // try to get an init(Object) method
+                this.init = clazz.getMethod("init", new Class[]{ Object.class });
             }
-            if (instance instanceof Configurable)
+            catch (NoSuchMethodException nsme)
             {
-                this.configurable = true;
+                // ignore
+            }
+            try
+            {
+                // check for a configure(Map) method
+                this.configure = clazz.getMethod("configure", new Class[]{ Map.class });
+            }
+            catch (NoSuchMethodException nsme)
+            {
+                // ignore
             }
         }
         else
@@ -153,8 +165,10 @@
 
     /**
      * Returns a new instance of the tool. If the tool
-     * implements {@link ViewTool}, the new instance
-     * will be initialized using the given data.
+     * has an init(Object) method, the new instance
+     * will be initialized using the given data. If parameters
+     * have been specified and the tool has a configure(Map)
+     * method, the tool will be passed the parameters also.
      */
     public Object getInstance(Object initData)
     {
@@ -164,6 +178,7 @@
             return null;
         }
 
+        /* Get the tool instance */
         Object tool = null;
         try
         {
@@ -176,21 +191,50 @@
         catch (IllegalAccessException e)
         {
             LOG.error("Exception while instantiating instance of \"" +
-                    getClassname() + "\": " + e);
+                    getClassname() + "\"", e);
         }
         catch (InstantiationException e)
         {
             LOG.error("Exception while instantiating instance of \"" +
-                    getClassname() + "\": " + e);
+                    getClassname() + "\"", e);
         }
-        if (configurable && parameters != null)
+
+        /* if the tool is configurable and we have parameters... */
+        if (configure != null && parameters != null)
         {
-            ((Configurable)tool).configure(parameters);
+            try
+            {
+                // call the configure method on the instance
+                configure.invoke(tool, new Object[]{ parameters });
+            }
+            catch (IllegalAccessException iae)
+            {
+                LOG.error("Exception when calling configure(Map) on "+tool, iae);
+            }
+            catch (InvocationTargetException ite)
+            {
+                LOG.error("Exception when calling configure(Map) on "+tool, ite);
+            }
         }
-        if (initializable)
+
+        /* if the tool is initializable... */
+        if (init != null)
         {
-            ((ViewTool)tool).init(initData);
+            try
+            {
+                // call the init method on the instance
+                init.invoke(tool, new Object[]{ initData });
+            }
+            catch (IllegalAccessException iae)
+            {
+                LOG.error("Exception when calling init(Object) on "+tool, iae);
+            }
+            catch (InvocationTargetException ite)
+            {
+                LOG.error("Exception when calling init(Object) on "+tool, ite);
+            }
         }
         return tool;
     }
+
 }

Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java?rev=385114&r1=385113&r2=385114&view=diff
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java Sat Mar 11 09:48:34 2006
@@ -48,7 +48,7 @@
  *
  * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
  *
- * @version $Id: ServletToolInfo.java,v 1.7 2004/03/12 20:50:38 nbubna Exp $
+ * @version $Id$
  */
 public class ServletToolInfo extends ViewToolInfo
 {

Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/Configurable.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/Configurable.java?rev=385114&r1=385113&r2=385114&view=diff
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/Configurable.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/Configurable.java Sat Mar 11 09:48:34 2006
@@ -24,6 +24,7 @@
  * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
  * @version $Id$
  * @since VelocityTools 1.2
+ * @deprecated Your tools now only need to have a configure(Map) method.
  */
 public interface Configurable
 {

Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ViewTool.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ViewTool.java?rev=385114&r1=385113&r2=385114&view=diff
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ViewTool.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ViewTool.java Sat Mar 11 09:48:34 2006
@@ -26,8 +26,8 @@
  * {@link org.apache.velocity.tools.view.ViewToolInfo} for more on this.
  *
  * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
- *
- * @version $Id: ViewTool.java,v 1.3 2004/02/18 20:06:11 nbubna Exp $
+ * @version $Id$
+ * @deprecated Your tools now only need to have an init(Object) method.
  */
 public interface ViewTool
 {



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org