velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r521391 [2/2] - in /velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view: ./ context/ servlet/
Date Thu, 22 Mar 2007 18:14:08 GMT
Added: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java?view=auto&rev=521391
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
(added)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
Thu Mar 22 11:14:06 2007
@@ -0,0 +1,268 @@
+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.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.ServletContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.tools.ToolContext;
+import org.apache.velocity.tools.Toolbox;
+
+/**
+ * <p>Velocity context implementation specific to the Servlet environment.</p>
+ *
+ * <p>It provides the following special features:</p>
+ * <ul>
+ *   <li>puts the request, response, session, and servlet context objects
+ *       into the Velocity context for direct access, and keeps them
+ *       read-only</li>
+ *   <li>supports a read-only toolbox of view tools</li>
+ *   <li>auto-searches servlet request attributes, session attributes and
+ *       servlet context attribues for objects</li>
+ * </ul>
+ *
+ * <p>The {@link #internalGet(String key)} method implements the following search order
+ * for objects:</p>
+ * <ol>
+ *   <li>toolbox</li>
+ *   <li>servlet request, servlet response, servlet session, servlet context</li>
+ *   <li>local hashtable of objects (traditional use)</li>
+ *   <li>servlet request attribues, servlet session attribute, servlet context
+ *     attributes</li>
+ * </ol>
+ *
+ * <p>The purpose of this class is to make it easy for web designer to work
+ * with Java servlet based web applications. They do not need to be concerned
+ * with the concepts of request, session or application attributes and the
+ * lifetime of objects in these scopes.</p>
+ *
+ * <p>Note that the put() method always puts objects into the local hashtable.
+ * </p>
+ *
+ * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
+ * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
+ * @author Nathan Bubna
+ *
+ * @version $Id: ViewContext.java 514727 2007-03-05 16:49:03Z nbubna $
+ */
+public class ViewToolContext extends ToolContext implements ViewContext
+{
+    private HttpServletRequest request;
+    private HttpServletResponse response;
+    private ServletContext application;
+    private VelocityEngine velocity;
+
+    public ViewToolContext(VelocityEngine velocity,
+                           HttpServletRequest request,
+                           HttpServletResponse response,
+                           ServletContext application)
+    {
+        super(null, null);
+
+        this.velocity = velocity;
+        this.request = request;
+        this.response = response;
+        this.application = application;
+
+        // automagically set common tool properties
+        putToolProperties();
+    }
+
+    protected void putToolProperties()
+    {
+        //TODO!! put these strings as constants somewhere!!
+        putToolProperty("servletRequest", getRequest());
+        putToolProperty("servletResponse", getResponse());
+        putToolProperty("httpSession", getSession());
+        putToolProperty("servletContext", getServletContext());
+        putToolProperty(PATH_KEY, ServletUtils.getPath(getRequest()));
+        putToolProperty("velocityContext", getVelocityContext());
+        putToolProperty("velocityEngine", getVelocityEngine());
+        putToolProperty("log", getVelocityEngine().getLog());
+    }
+
+    protected List<Toolbox> getToolboxes()
+    {
+        // this means once we find any toolbox,
+        // then we stop looking.  adding boxes to
+        // the request/session/application attributes
+        // later will not work.  once one is in, any
+        // later additions must be direct via addToolbox()
+        if (super.getToolboxes().isEmpty())
+        {
+            Toolbox reqTools = (Toolbox)getRequest().getAttribute(TOOLBOX_KEY);
+            if (reqTools != null)
+            {
+                addToolbox(reqTools);
+            }
+
+            if (getSession() != null)
+            {
+                Toolbox sessTools = (Toolbox)getSession().getAttribute(TOOLBOX_KEY);
+                if (sessTools != null)
+                {
+                    addToolbox(sessTools);
+                }
+            }
+
+            Toolbox appTools = (Toolbox)getServletContext().getAttribute(TOOLBOX_KEY);
+            if (appTools != null)
+            {
+                addToolbox(appTools);
+            }
+        }
+        return super.getToolboxes();
+    }
+
+    /**
+     * <p>Looks up and returns the object with the specified key.</p>
+     * <p>See the class documentation for more details.</p>
+     *
+     * @param key the key of the object requested
+     * @return the requested object or null if not found
+     */
+    public Object get(String key)
+    {
+        /* search for a tool first */
+        Object o = findTool(key);
+        if (o != null)
+        {
+            return o;
+        }
+
+        /* make the four scopes of the Apocalypse Read only */
+        if (key.equals(REQUEST))
+        {
+            return request;
+        }
+        else if(key.equals(RESPONSE))
+        {
+            return response;
+        }
+        else if (key.equals(SESSION))
+        {
+            return getSession();
+        }
+        else if (key.equals(APPLICATION))
+        {
+            return application;
+        }
+
+        /* try the local context */
+        o = internalGet(key);
+        if (o != null)
+        {
+            return o;
+        }
+
+        /* if not found, wander down the scopes... */
+        return getAttribute(key);
+    }
+
+
+    /**
+     * <p>Searches for the named attribute in request, session (if valid),
+     * and application scope(s) in order and returns the value associated
+     * or null.</p>
+     *
+     * @since VelocityTools 1.1
+     */
+    public Object getAttribute(String key)
+    {
+        Object o = request.getAttribute(key);
+        if (o == null)
+        {
+            if (getSession() != null)
+            {
+                try
+                {
+                    o = getSession().getAttribute(key);
+                }
+                catch (IllegalStateException ise)
+                {
+                    // Handle invalidated session state
+                    o = null;
+                }
+            }
+
+            if (o == null)
+            {
+                o = application.getAttribute(key);
+            }
+        }
+        return o;
+    }
+
+
+    /**
+     * <p>Returns the current servlet request.</p>
+     */
+    public HttpServletRequest getRequest()
+    {
+        return request;
+    }
+
+    /**
+     * <p>Returns the current servlet response.</p>
+     */
+    public HttpServletResponse getResponse()
+    {
+        return response;
+    }
+
+    /**
+     * <p>Returns the current session, if any.</p>
+     */
+    public HttpSession getSession()
+    {
+        return getRequest().getSession(false);
+    }
+
+    /**
+     * <p>Returns the servlet context.</p>
+     */
+    public ServletContext getServletContext()
+    {
+        return application;
+    }
+
+    /**
+     * <p>Returns a reference to the Velocity context (this object).</p>
+     */
+    public Context getVelocityContext()
+    {
+        return this;
+    }
+
+    /**
+     * <p>Returns a reference to the VelocityEngine.</p>
+     */
+    public VelocityEngine getVelocityEngine()
+    {
+        return velocity;
+    }
+
+}

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

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

Copied: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java
(from r521375, velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/WebappLoader.java)
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java?view=diff&rev=521391&p1=velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/WebappLoader.java&r1=521375&p2=velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java&r2=521391
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/WebappLoader.java
(original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java
Thu Mar 22 11:14:06 2007
@@ -1,4 +1,4 @@
-package org.apache.velocity.tools.view.servlet;
+package org.apache.velocity.tools.view;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -22,9 +22,7 @@
 import java.io.File;
 import java.io.InputStream;
 import java.util.HashMap;
-
 import javax.servlet.ServletContext;
-
 import org.apache.commons.collections.ExtendedProperties;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.runtime.resource.Resource;
@@ -53,13 +51,11 @@
  * @author <a href="mailto:claude@savoirweb.com">Claude Brisson</a>
  * @version $Id$  */
 
-public class WebappLoader extends ResourceLoader
+public class WebappResourceLoader extends ResourceLoader
 {
-
     /** The root paths for templates (relative to webapp's root). */
     protected String[] paths = null;
     protected HashMap templatePaths = null;
-
     protected ServletContext servletContext = null;
 
     /**
@@ -74,7 +70,7 @@
      */
     public void init(ExtendedProperties configuration)
     {
-        rsvc.debug("WebappLoader : initialization starting.");
+        log.trace("WebappResourceLoader: initialization starting.");
 
         /* get configured paths */
         paths = configuration.getStringArray("path");
@@ -92,7 +88,7 @@
                 {
                     paths[i] += '/';
                 }
-                rsvc.info("WebappLoader : added template path - '" + paths[i] + "'");
+                log.info("WebappResourceLoader: added template path - '" + paths[i] + "'");
             }
         }
 
@@ -104,13 +100,13 @@
         }
         else
         {
-            rsvc.error("WebappLoader : unable to retrieve ServletContext");
+            log.error("WebappResourceLoader: unable to retrieve ServletContext");
         }
 
         /* init the template paths map */
         templatePaths = new HashMap();
 
-        rsvc.debug("WebappLoader : initialization complete.");
+        log.trace("WebappResourceLoader: initialization complete.");
     }
 
     /**
@@ -122,14 +118,14 @@
      * @throws ResourceNotFoundException if template not found
      *         in  classpath.
      */
-    public synchronized InputStream getResourceStream( String name )
+    public synchronized InputStream getResourceStream(String name)
         throws ResourceNotFoundException
     {
         InputStream result = null;
 
         if (name == null || name.length() == 0)
         {
-            throw new ResourceNotFoundException ("WebappLoader : No template name provided");
+            throw new ResourceNotFoundException("WebappResourceLoader: No template name provided");
         }
 
         /* since the paths always ends in '/',
@@ -169,7 +165,7 @@
             String msg;
             if (exception == null)
             {
-                msg = "WebappLoader : Resource '" + name + "' not found.";
+                msg = "WebappResourceLoader: Resource '" + name + "' not found.";
             }
             else
             {

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java?view=diff&rev=521391&r1=521390&r2=521391
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
(original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
Thu Mar 22 11:14:06 2007
@@ -92,7 +92,7 @@
         {
             // match some paths
             exactPath = false;
-            this.path = path.substring(0, scope.length() - 1);
+            this.path = path.substring(0, path.length() - 1);
         }
         else
         {



Mime
View raw message