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 2009/05/06 20:24:54 UTC

svn commit: r772392 - in /velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view: JeeConfig.java JeeContextConfig.java JeeFilterConfig.java JeeServletConfig.java ServletUtils.java VelocityView.java

Author: nbubna
Date: Wed May  6 18:24:53 2009
New Revision: 772392

URL: http://svn.apache.org/viewvc?rev=772392&view=rev
Log:
VELTOOLS-116 refactor JeeConfig to be an interface (thanks to Antonio Petrelli)

Added:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeContextConfig.java   (with props)
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeFilterConfig.java   (with props)
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeServletConfig.java   (with props)
Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeConfig.java
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeConfig.java?rev=772392&r1=772391&r2=772392&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeConfig.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeConfig.java Wed May  6 18:24:53 2009
@@ -20,128 +20,65 @@
  */
 
 import java.util.Enumeration;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 
 /**
  * <p>Because sometimes you don't care about the difference between a
  * servlet and a filter.  Why isn't there a common interface for
  * {@link FilterConfig} and {@link ServletConfig} already? Sheesh.</p>
- * <p>Anyway, this also adds the ability to fake everything if you 
- * don't have either a FilterConfig or a ServletConfig handy. Just
- * subclass it and override the methods that return things you care
- * about.  Oh, and if you don't have any init-params at all, just use
- * the constructor that just takes a {@link ServletContext} as that's
- * the only really essential thing for creating a {@link VelocityView}.
+ * </p>
+ * <p>
+ * Anyway, this also adds the ability to fake everything if you don't have
+ * either a FilterConfig or a ServletConfig handy. Just implement it and
+ * override the methods that return things you care about. Oh, and if you don't
+ * have any init-params at all, just use {@link JeeContextConfig} as
+ * {@link ServletContext} is the only really essential thing for creating a
+ * {@link VelocityView}.</p>
  *
- * @version $Id: ServletUtils.java 471244 2006-11-04 18:34:38Z henning $
+ * @version $Id$
+ * @since 2.0
  */
-public class JeeConfig
+public interface JeeConfig
 {
-    protected FilterConfig filter;
-    protected ServletConfig servlet;
-    protected ServletContext context;
-
     /**
-     * If your subclass uses this, you better make sure
-     * that {@link #getServletContext()} doesn't return null!
+     * Returns an initialization parameter.
+     *
+     * @param name The name of the initialization parameter.
+     * @return The value of the parameter.
      */
-    protected JeeConfig(){}
-
-    public JeeConfig(FilterConfig filter)
-    {
-        if (filter == null)
-        {
-            throw new NullPointerException("FilterConfig should not be null; there must be a way to get a ServletContext");
-        }
-        this.filter = filter;
-    }
-
-    public JeeConfig(ServletConfig servlet)
-    {
-        if (servlet == null)
-        {
-            throw new NullPointerException("ServletConfig should not be null; there must be a way to get a ServletContext");
-        }
-        this.servlet = servlet;
-    }
-
-    public JeeConfig(ServletContext context)
-    {
-        if (context == null)
-        {
-            throw new NullPointerException("ServletContext must not be null");
-        }
-        this.context = context;
-    }
-
-    public String getInitParameter(String name)
-    {
-        if (filter != null)
-        {
-            return filter.getInitParameter(name);
-        }
-        if (servlet != null)
-        {
-            return servlet.getInitParameter(name);
-        }
-        return null;
-    }
+    String getInitParameter(String name);
 
     /**
      * Looks for the specified init-param in the servlet/filter config
      * (i.e. calls {@link #getInitParameter}). If no such init-param is
      * found there, it checks the {@link ServletContext}'s init-params
      * for the specified parameter.
+     *
+     * @param key The name of the initialization parameter.
+     * @return The value of the initialization parameter.
+     */
+    String findInitParameter(String key);
+
+    /**
+     * Returns all the parameter names.
+     *
+     * @return The enumeration containing the parameter names.
+     */
+    @SuppressWarnings("unchecked")
+    Enumeration getInitParameterNames();
+
+    /**
+     * Returns the name of the servlet (or filter) being used.
+     *
+     * @return The name of the configuration.
+     */
+    String getName();
+
+    /**
+     * Returns the servlet context.
+     *
+     * @return The servlet context.
      */
-    public String findInitParameter(String key)
-    {
-        String param = getInitParameter(key);
-        if (param == null)
-        {
-            param = getServletContext().getInitParameter(key);
-        }
-        return param;
-    }
-
-    public Enumeration getInitParameterNames()
-    {
-        if (filter != null)
-        {
-            return filter.getInitParameterNames();
-        }
-        if (servlet != null)
-        {
-            return servlet.getInitParameterNames();
-        }
-        return null;
-    }
-
-    public String getName()
-    {
-        if (filter != null)
-        {
-            return filter.getFilterName();
-        }
-        if (servlet != null)
-        {
-            return servlet.getServletName();
-        }
-        return null;
-    }
-
-    public ServletContext getServletContext()
-    {
-        if (filter != null)
-        {
-            return filter.getServletContext();
-        }
-        if (servlet != null)
-        {
-            return servlet.getServletContext();
-        }
-        return context;
-    }
+    ServletContext getServletContext();
 
 }

Added: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeContextConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeContextConfig.java?rev=772392&view=auto
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeContextConfig.java (added)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeContextConfig.java Wed May  6 18:24:53 2009
@@ -0,0 +1,77 @@
+package org.apache.velocity.tools.view;
+
+/*
+ * 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.util.Enumeration;
+import javax.servlet.ServletContext;
+
+/**
+ * Implements {@link JeeConfig} with only a {@link ServletContext}.
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class JeeContextConfig implements JeeConfig
+{
+    protected ServletContext context;
+
+    public JeeContextConfig(ServletContext context)
+    {
+        if (context == null)
+        {
+            throw new NullPointerException("ServletContext should not be null; there must be a way to get a ServletContext");
+        }
+        this.context = context;
+    }
+
+    /** {@inheritDoc} */
+    public String getInitParameter(String name)
+    {
+        return null;
+    }
+
+    /**
+     * Only checks the {@link ServletContext}'s init-params
+     * for the specified parameter.
+     */
+    public String findInitParameter(String key)
+    {
+        return context.getInitParameter(key);
+    }
+
+    /** {@inheritDoc} */
+    public Enumeration getInitParameterNames()
+    {
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    public String getName()
+    {
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    public ServletContext getServletContext()
+    {
+        return context;
+    }
+
+}

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

Added: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeFilterConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeFilterConfig.java?rev=772392&view=auto
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeFilterConfig.java (added)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeFilterConfig.java Wed May  6 18:24:53 2009
@@ -0,0 +1,80 @@
+package org.apache.velocity.tools.view;
+
+/*
+ * 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.util.Enumeration;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+
+/**
+ * Implements {@link JeeConfig} with a {@link FilterConfig}.
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class JeeFilterConfig implements JeeConfig
+{
+    protected FilterConfig filter;
+
+    public JeeFilterConfig(FilterConfig filter)
+    {
+        if (filter == null)
+        {
+            throw new NullPointerException("FilterConfig should not be null; there must be a way to get a ServletContext");
+        }
+        this.filter = filter;
+    }
+
+    /** {@inheritDoc} */
+    public String getInitParameter(String name)
+    {
+        return filter.getInitParameter(name);
+    }
+
+    /** {@inheritDoc} */
+    public String findInitParameter(String key)
+    {
+        String param = getInitParameter(key);
+        if (param == null)
+        {
+            param = getServletContext().getInitParameter(key);
+        }
+        return param;
+    }
+
+    /** {@inheritDoc} */
+    public Enumeration getInitParameterNames()
+    {
+        return filter.getInitParameterNames();
+    }
+
+    /** {@inheritDoc} */
+    public String getName()
+    {
+        return filter.getFilterName();
+    }
+
+    /** {@inheritDoc} */
+    public ServletContext getServletContext()
+    {
+        return filter.getServletContext();
+    }
+
+}

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

Added: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeServletConfig.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeServletConfig.java?rev=772392&view=auto
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeServletConfig.java (added)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/JeeServletConfig.java Wed May  6 18:24:53 2009
@@ -0,0 +1,80 @@
+package org.apache.velocity.tools.view;
+
+/*
+ * 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.util.Enumeration;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+/**
+ * Implements {@link JeeConfig} with a {@link ServletConfig}.
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class JeeServletConfig implements JeeConfig
+{
+    protected ServletConfig servlet;
+
+    public JeeServletConfig(ServletConfig servlet)
+    {
+        if (servlet == null)
+        {
+            throw new NullPointerException("ServletConfig should not be null; there must be a way to get a ServletContext");
+        }
+        this.servlet = servlet;
+    }
+
+    /** {@inheritDoc} */
+    public String getInitParameter(String name)
+    {
+        return servlet.getInitParameter(name);
+    }
+
+    /** {@inheritDoc} */
+    public String findInitParameter(String key)
+    {
+        String param = getInitParameter(key);
+        if (param == null)
+        {
+            param = getServletContext().getInitParameter(key);
+        }
+        return param;
+    }
+
+    /** {@inheritDoc} */
+    public Enumeration getInitParameterNames()
+    {
+        return servlet.getInitParameterNames();
+    }
+
+    /** {@inheritDoc} */
+    public String getName()
+    {
+        return servlet.getServletName();
+    }
+
+    /** {@inheritDoc} */
+    public ServletContext getServletContext()
+    {
+        return servlet.getServletContext();
+    }
+
+}

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

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java?rev=772392&r1=772391&r2=772392&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java Wed May  6 18:24:53 2009
@@ -101,7 +101,7 @@
      */
     public static VelocityView getVelocityView(ServletConfig config)
     {
-        return getVelocityView(new JeeConfig(config));
+        return getVelocityView(new JeeServletConfig(config));
     }
 
 
@@ -112,7 +112,7 @@
      */
     public static VelocityView getVelocityView(FilterConfig config)
     {
-        return getVelocityView(new JeeConfig(config));
+        return getVelocityView(new JeeFilterConfig(config));
     }
 
     /**
@@ -193,7 +193,7 @@
      */
     public static VelocityView getVelocityView(ServletContext application)
     {
-        return getVelocityView(new JeeConfig(application));
+        return getVelocityView(new JeeContextConfig(application));
     }
 
     /**

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java?rev=772392&r1=772391&r2=772392&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java Wed May  6 18:24:53 2009
@@ -191,17 +191,17 @@
 
     public VelocityView(ServletConfig config)
     {
-        this(new JeeConfig(config));
+        this(new JeeServletConfig(config));
     }
 
     public VelocityView(FilterConfig config)
     {
-        this(new JeeConfig(config));
+        this(new JeeFilterConfig(config));
     }
 
     public VelocityView(ServletContext context)
     {
-        this(new JeeConfig(context));
+        this(new JeeContextConfig(context));
     }
 
     public VelocityView(JeeConfig config)