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/03/31 00:02:08 UTC

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

Author: nbubna
Date: Fri Mar 30 15:02:06 2007
New Revision: 524290

URL: http://svn.apache.org/viewvc?view=rev&rev=524290
Log:
add validation for configurations

Added:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java   (with props)
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java   (with props)
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java   (with props)
Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/CompoundConfiguration.java
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Configuration.java
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/FactoryConfiguration.java
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolboxConfiguration.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/CompoundConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/CompoundConfiguration.java?view=diff&rev=524290&r1=524289&r2=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/CompoundConfiguration.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/CompoundConfiguration.java Fri Mar 30 15:02:06 2007
@@ -69,4 +69,16 @@
             }
         }
     }
+
+    @Override
+    public void validate()
+    {
+        super.validate();
+
+        for (C child : getChildren())
+        {
+            child.validate();
+        }
+    }
+
 }

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Configuration.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Configuration.java?view=diff&rev=524290&r1=524289&r2=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Configuration.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Configuration.java Fri Mar 30 15:02:06 2007
@@ -82,4 +82,13 @@
             setProperty(name, simples.get(name));
         }
     }
+
+    public void validate()
+    {
+        for (Property property : getConvertableProperties())
+        {
+            property.validate();
+        }
+    }
+
 }

Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java?view=auto&rev=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java (added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java Fri Mar 30 15:02:06 2007
@@ -0,0 +1,61 @@
+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.
+ */
+
+/**
+ * 
+ *
+ * @author Nathan Bubna
+ * @version $Id: ConfigurationException.java 511959 2007-02-26 19:24:39Z nbubna $
+ */
+public class ConfigurationException extends RuntimeException
+{
+    private Object source;
+
+    public ConfigurationException(Data data, Throwable cause)
+    {
+        super(cause);
+        this.source = data;
+    }
+
+    public ConfigurationException(Data data, String message)
+    {
+        super(message);
+        this.source = data;
+    }
+
+    public ConfigurationException(Configuration config, Throwable cause)
+    {
+        super(cause);
+        this.source = config;
+    }
+
+    public ConfigurationException(Configuration config, String message)
+    {
+        super(message);
+        this.source = config;
+    }
+
+    public Object getSource()
+    {
+        return source;
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java?view=diff&rev=524290&r1=524289&r2=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java Fri Mar 30 15:02:06 2007
@@ -161,6 +161,32 @@
         }
     }
 
+    public void validate()
+    {
+        // make sure the key is not null
+        if (getKey() == null)
+        {
+            throw new NullKeyException(this);
+        }
+
+        // make sure we have value and that it can be converted
+        if (getValue() == null)
+        {
+            throw new ConfigurationException(this, "No value has been set for '"+getKey()+'\'');
+        }
+        else if (this.converter != null)
+        {
+            try
+            {
+                getConvertedValue();
+            }
+            catch (Throwable t)
+            {
+                throw new ConfigurationException(this, t);
+            }
+        }
+    }
+
 
     protected DataConverter getDataConverter(String type)
     {

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/FactoryConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/FactoryConfiguration.java?view=diff&rev=524290&r1=524289&r2=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/FactoryConfiguration.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/FactoryConfiguration.java Fri Mar 30 15:02:06 2007
@@ -36,6 +36,7 @@
     private List<Data> data = new ArrayList<Data>();
 
 
+    @Override
     protected ToolboxConfiguration findMatchingChild(ToolboxConfiguration newToolbox)
     {
         for (ToolboxConfiguration toolbox : getToolboxes())
@@ -83,12 +84,15 @@
         }
     }
 
-    public boolean isValid()
+    @Override
+    public void validate()
     {
-        //TODO: we need to check ValidScopes annotations,
-        //      make sure that ToolConfigurations convert to ToolInfo
-        //      and anything else we can think of
-        return true;
+        super.validate();
+
+        for (Data data : getData())
+        {
+            data.validate();
+        }
     }
 
     public ToolboxFactory createFactory()

Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java?view=auto&rev=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java (added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java Fri Mar 30 15:02:06 2007
@@ -0,0 +1,47 @@
+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.
+ */
+
+/**
+ * 
+ *
+ * @author Nathan Bubna
+ * @version $Id: InvalidScopeException.java 511959 2007-02-26 19:24:39Z nbubna $
+ */
+public class InvalidScopeException extends ConfigurationException
+{
+    private ToolConfiguration tool;
+
+    public InvalidScopeException(ToolboxConfiguration toolbox,
+                                 ToolConfiguration tool)
+    {
+        super(toolbox, "Toolbox with scope '" +
+                       toolbox.getScope() +
+                       "' may not contain a " +
+                       tool.getClassname() + '.');
+        this.tool = tool;
+    }
+
+    public ToolConfiguration getToolConfiguration()
+    {
+        return tool;
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/InvalidScopeException.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java?view=auto&rev=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java (added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java Fri Mar 30 15:02:06 2007
@@ -0,0 +1,40 @@
+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.
+ */
+
+/**
+ * 
+ *
+ * @author Nathan Bubna
+ * @version $Id: NullKeyException.java 511959 2007-02-26 19:24:39Z nbubna $
+ */
+public class NullKeyException extends ConfigurationException
+{
+    public NullKeyException(Data data)
+    {
+        super(data, "Key is null for data with value of '"+data.getValue()+'\'');
+    }
+
+    public NullKeyException(ToolConfiguration tool)
+    {
+        super(tool, "Key is null for tool whose class is '"+tool.getClassname()+'\'');
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/NullKeyException.java
------------------------------------------------------------------------------
    svn:keywords = Revision

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=524290&r1=524289&r2=524290
==============================================================================
--- 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 Fri Mar 30 15:02:06 2007
@@ -20,6 +20,7 @@
  */
 
 import org.apache.velocity.tools.ToolInfo;
+import org.apache.velocity.tools.Utils;
 
 /**
  * 
@@ -67,6 +68,46 @@
         return this.classname;
     }
 
+    public Class getToolClass()
+    {
+        try
+        {
+            return Utils.getClass(getClassname());
+        }
+        catch (ClassNotFoundException cnfe)
+        {
+            throw new ConfigurationException(this, cnfe);
+        }
+    }
+
+    public String[] getInvalidScopes()
+    {
+        InvalidScope invalid =
+            (InvalidScope)getToolClass().getAnnotation(InvalidScope.class);
+        if (invalid != null)
+        {
+            return invalid.value();
+        }
+        else
+        {
+            return new String[] {};
+        }
+    }
+
+    public String[] getValidScopes()
+    {
+        ValidScope valid =
+            (ValidScope)getToolClass().getAnnotation(ValidScope.class);
+        if (valid != null)
+        {
+            return valid.value();
+        }
+        else
+        {
+            return new String[] {};
+        }
+    }
+
     public String getRestrictTo()
     {
         return this.restrictTo;
@@ -74,11 +115,34 @@
 
     public ToolInfo createInfo()
     {
-        ToolInfo info = new ToolInfo(getKey(), getClassname());
+        ToolInfo info = new ToolInfo(getKey(), getToolClass());
         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
         info.addProperties(getProperties());
         return info;
     }
+
+    @Override
+    public void validate()
+    {
+        super.validate();
+        
+        // make sure the key is not null
+        if (getKey() == null)
+        {
+            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
+        {
+            getToolClass();
+        }
+    }
+
 }

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolboxConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolboxConfiguration.java?view=diff&rev=524290&r1=524289&r2=524290
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolboxConfiguration.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/ToolboxConfiguration.java Fri Mar 30 15:02:06 2007
@@ -67,4 +67,48 @@
         return getChildren();
     }
 
+    @Override
+    public void validate()
+    {
+        super.validate();
+
+        if (getScope() == null)
+        {
+            throw new ConfigurationException(this, "Toolbox scope cannot be null");
+        }
+
+        // validate that all tools are allowed in this scope
+        for (ToolConfiguration tool : getTools())
+        {
+            // check if this toolbox's scope has been declared invalid
+            for (String invalidScope : tool.getInvalidScopes())
+            {
+                if (getScope().equals(invalidScope))
+                {
+                    throw new InvalidScopeException(this, tool);
+                }
+            }
+ 
+            // if the set of valid scopes has been limited, check to be
+            // sure that this toolbox's scope is in the set
+            String[] validScopes = tool.getValidScopes();
+            if (validScopes != null && validScopes.length > 0)
+            {
+                boolean found = false;
+                for (String validScope : validScopes)
+                {
+                    if (getScope().equals(validScope))
+                    {
+                        found = true;
+                        break;
+                    }
+                }
+                if (!found)
+                {
+                    throw new InvalidScopeException(this, tool);
+                }
+            }
+        }
+    }
+
 }