Author: nbubna
Date: Thu Sep 25 17:07:01 2008
New Revision: 699135

URL: http://svn.apache.org/viewvc?rev=699135&view=rev
Log:
allow tool classes/configs to have individual property setters skipped during 
actual configuration of the tool instance

Added:
    
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
   (with props)
Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolInfo.java
    
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java

Modified: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolInfo.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolInfo.java?rev=699135&r1=699134&r2=699135&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolInfo.java 
(original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolInfo.java 
Thu Sep 25 17:07:01 2008
@@ -25,6 +25,7 @@
 import java.util.Map;
 import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.velocity.tools.ClassUtils;
+import org.apache.velocity.tools.config.SkipSetters;
 
 /**
  * Manages data needed to create instances of a tool. New instances
@@ -44,6 +45,7 @@
     private boolean restrictToIsExact;
     private String restrictTo;
     private Map<String,Object> properties;
+    private Boolean skipSetters;
     private transient Method configure = null;
 
     /**
@@ -119,6 +121,11 @@
         }
     }
 
+    public void setSkipSetters(boolean cfgOnly)
+    {
+        this.skipSetters = cfgOnly;
+    }
+
     /**
      * Adds a map of properties from a parent scope to the properties
      * for this tool.  Only new properties will be added; any that
@@ -183,6 +190,15 @@
         return (getConfigure() != null);
     }
 
+    public boolean isSkipSetters()
+    {
+        if (skipSetters == null)
+        {
+            skipSetters = (clazz.getAnnotation(SkipSetters.class) != null);
+        }
+        return skipSetters;
+    }
+
     /**
      * @param path the path of a template requesting this tool
      * @return <code>true</code> if the specified
@@ -246,17 +262,30 @@
     /**
      * Actually performs configuration of the newly instantiated tool
      * using the combined final set of configuration properties. First,
+     * if the class lacks the [EMAIL PROTECTED] SkipSetters} annotation, then 
any
      * specific setters matching the configuration keys are called, then
      * the general configure(Map) method (if any) is called.
      */
     protected void configure(Object tool, Map<String,Object> configuration)
     {
-        if (configuration != null)
+        if (!isSkipSetters() && configuration != null)
         {
-            // look for specific setters
-            for (Map.Entry<String,Object> conf : configuration.entrySet())
+            try
             {
-                setProperty(tool, conf.getKey(), conf.getValue());
+                // look for specific setters
+                for (Map.Entry<String,Object> conf : configuration.entrySet())
+                {
+                    setProperty(tool, conf.getKey(), conf.getValue());
+                }
+            }
+            catch (RuntimeException re)
+            {
+                throw re;
+            }
+            catch (Exception e)
+            {
+                // convert to a runtime exception, and re-throw
+                throw new RuntimeException(e);
             }
         }
 
@@ -348,21 +377,13 @@
     }
 
 
-    protected void setProperty(Object tool, String name, Object value)
+    protected void setProperty(Object tool, String name, Object value) throws 
Exception
     {
-        try
+        if (PropertyUtils.isWriteable(tool, name))
         {
-            if (PropertyUtils.isWriteable(tool, name))
-            {
-                //TODO? support property conversion here?
-                //      heavy-handed way is BeanUtils.copyProperty(...)
-                PropertyUtils.setProperty(tool, name, value);
-            }
-        }
-        catch (Exception e)
-        {
-            // convert to a runtime exception, and re-throw
-            throw new RuntimeException(e);
+            //TODO? support property conversion here?
+            //      heavy-handed way is BeanUtils.copyProperty(...)
+            PropertyUtils.setProperty(tool, name, value);
         }
     }
 

Added: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java?rev=699135&view=auto
==============================================================================
--- 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
 (added)
+++ 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
 Thu Sep 25 17:07:01 2008
@@ -0,0 +1,44 @@
+package org.apache.velocity.tools.config;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation specifying that [EMAIL PROTECTED] ToolInfo} should only
+ * use the public void configure(Map) method for configuration of
+ * a tool, and skip over the step of trying to find individual
+ * property setters.
+ *
+ * @author Nathan Bubna
+ * @version $Id: SkipSetters.java 511959 2007-02-26 19:24:39Z nbubna $
+ */
[EMAIL PROTECTED]
[EMAIL PROTECTED](ElementType.TYPE)
[EMAIL PROTECTED](RetentionPolicy.RUNTIME)
[EMAIL PROTECTED]
+public @interface SkipSetters
+{
+}

Propchange: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Propchange: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/SkipSetters.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java?rev=699135&r1=699134&r2=699135&view=diff
==============================================================================
--- 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
 (original)
+++ 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
 Thu Sep 25 17:07:01 2008
@@ -47,6 +47,7 @@
     private String key;
     private String classname;
     private String restrictTo;
+    private boolean skipSetters;
     private Status status;
     private Throwable problem;
 
@@ -77,6 +78,11 @@
         this.restrictTo = path;
     }
 
+    public void setSkipSetters(boolean cfgOnly)
+    {
+        this.skipSetters = cfgOnly;
+    }
+
     /**
      * Returns the key set for this tool. If no key has been explicitly
      * set, this will return the result of [EMAIL PROTECTED] #getDefaultKey()}.
@@ -256,6 +262,11 @@
         return this.restrictTo;
     }
 
+    public boolean getSkipSetters()
+    {
+        return this.skipSetters;
+    }
+
     public ToolInfo createInfo()
     {
         ToolInfo info = null;
@@ -273,6 +284,7 @@
         }
 
         info.restrictTo(getRestrictTo());
+        info.setSkipSetters(getSkipSetters());
         // it's ok to use this here, because we know it's the
         // first time properties have been added to this ToolInfo
         info.addProperties(getPropertyMap());


Reply via email to