You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/05/17 04:55:23 UTC

svn commit: r538793 - /velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java

Author: nbubna
Date: Wed May 16 19:55:22 2007
New Revision: 538793

URL: http://svn.apache.org/viewvc?view=rev&rev=538793
Log:
create more nuanced and thorough validation errors and better prevent toString() from throwing exceptions and errors

Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java?view=diff&rev=538793&r1=538792&r2=538793
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java Wed May 16 19:55:22 2007
@@ -125,26 +125,45 @@
         }
     }
 
-    private final boolean isOldTool()
+    private enum Status {
+        VALID, OLD, NONE, MISSING, UNSUPPORTED;
+    }
+
+    private final Status getStatus()
     {
+        if (getClassname() == null)
+        {
+            return Status.NONE;
+        }
+
         // check for mere presence of init() or configure()
-        Class clazz = getToolClass();
         try
         {
+            Class clazz = Utils.getClass(getClassname());
+
             Method init = clazz.getMethod("init", new Class[]{ Object.class });
+
             // if init is deprecated, then we'll consider it a
             // new tool with BC support, not an old tool
             Deprecated bc = init.getAnnotation(Deprecated.class);
             if (bc == null)
             {
-                return true;
+                return Status.OLD;
             }
         }
         catch (NoSuchMethodException nsme)
         {
             // ignore this
         }
-        return false;
+        catch (ClassNotFoundException cnfe)
+        {
+            return Status.MISSING;
+        }
+        catch (NoClassDefFoundError ncdfe)
+        {
+            return Status.UNSUPPORTED;
+        }
+        return Status.VALID;
     }
 
     public String getRestrictTo()
@@ -154,15 +173,20 @@
 
     public ToolInfo createInfo()
     {
-        ToolInfo info;
-        if (isOldTool())
-        {
-            info = new OldToolInfo(getKey(), getToolClass());
-        }
-        else
-        {
-            info = new ToolInfo(getKey(), getToolClass());
+        ToolInfo info = null;
+        Status status = getStatus();
+        switch (status)
+        {
+            case VALID:
+                info = new ToolInfo(getKey(), getToolClass());
+                break;
+            case OLD:
+                info = new OldToolInfo(getKey(), getToolClass());
+                break;
+            default:
+                throw new ConfigurationException(this, getError(status));
         }
+
         info.restrictTo(getRestrictTo());
         // it's ok to use this here, because we know it's the
         // first time properties have been added to this ToolInfo
@@ -170,6 +194,21 @@
         return info;
     }
 
+    private final String getError(Status status)
+    {
+        switch (status)
+        {
+            case NONE:
+                return "No classname set for tool: "+this;
+            case MISSING:
+                return "Couldn't find class '"+getClassname()+"' in the classpath for tool: "+this;
+            case UNSUPPORTED:
+                return "Couldn't find necessary supporting classes for tool: "+this;
+            default:
+                return "";
+        }
+    }
+
     @Override
     public void validate()
     {
@@ -181,14 +220,14 @@
             throw new NullKeyException(this);
         }
 
-        // make sure that we can have an accessible tool class
-        if (getClassname() == null)
-        {
-            throw new ConfigurationException(this, "No tool classname has been set for '"+getKey()+'\'');
-        }
-        else
+        Status status = getStatus();
+        switch (status)
         {
-            getToolClass();
+            case VALID:
+            case OLD:
+                break;
+            default:
+                throw new ConfigurationException(this, getError(status));
         }
     }
 
@@ -202,9 +241,19 @@
         }
         else
         {
-            if (isOldTool())
+            switch (getStatus())
             {
-                out.append("Old ");
+                case VALID:
+                    break;
+                case OLD:
+                    out.append("Old ");
+                    break;
+                case NONE:
+                case MISSING:
+                    out.append("Invalid ");
+                    break;
+                case UNSUPPORTED:
+                    out.append("Unsupported ");
             }
             out.append("Tool '");
             out.append(getKey());