You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ap...@apache.org on 2006/12/14 14:07:12 UTC

svn commit: r487201 - in /struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2: ./ util/

Author: apetrelli
Date: Thu Dec 14 05:07:11 2006
New Revision: 487201

URL: http://svn.apache.org/viewvc?view=rev&rev=487201
Log:
STR-2986
Finished converting to Tiles 2, now everything needs to be tested.

Added:
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java   (with props)
Removed:
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/AttributeDefinition.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/DefinitionAttribute.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/DefinitionNameAttribute.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/DefinitionsFactoryConfig.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/DirectStringAttribute.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/PathAttribute.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesException.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesUtil.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesUtilStrutsImpl.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesUtilStrutsModulesImpl.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/UntypedAttribute.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/UrlController.java
Modified:
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesPlugin.java
    struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesRequestProcessor.java

Modified: struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesPlugin.java
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesPlugin.java?view=diff&rev=487201&r1=487200&r2=487201
==============================================================================
--- struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesPlugin.java (original)
+++ struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesPlugin.java Thu Dec 14 05:07:11 2006
@@ -21,11 +21,7 @@
 
 package org.apache.struts.tiles2;
 
-import java.util.Map;
-
-import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
-import javax.servlet.UnavailableException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -36,7 +32,12 @@
 import org.apache.struts.config.ControllerConfig;
 import org.apache.struts.config.ModuleConfig;
 import org.apache.struts.config.PlugInConfig;
+import org.apache.struts.tiles2.util.PlugInConfigContextAdapter;
 import org.apache.struts.util.RequestUtils;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.TilesException;
+import org.apache.tiles.access.TilesAccess;
+import org.apache.tiles.factory.TilesContainerFactory;
 
 /**
  * Tiles Plugin used to initialize Tiles.
@@ -59,8 +60,11 @@
  * is always the default one if it exists.
  * The plugin should be declared in each struts-config.xml file in order to
  * properly initialize the request processor.
+ *
+ * @version $Rev$ $Date$
  * @since Struts 1.1
  */
+// TODO Complete the plugin to be module-aware.
 public class TilesPlugin implements PlugIn {
 
     /**
@@ -74,21 +78,16 @@
     protected boolean moduleAware = false;
 
     /**
-     * Tiles util implementation classname. This property can be set
-     * by user in the plugin declaration.
-     */
-    protected String tilesUtilImplClassname = null;
-
-    /**
-     * Associated definition factory.
-     */
-    protected DefinitionsFactory definitionFactory = null;
-
-    /**
      * The plugin config object provided by the ActionServlet initializing
      * this plugin.
      */
     protected PlugInConfig currentPlugInConfigObject=null;
+    
+    /**
+     * The plugin config object adapted to become a context-like object, that
+     * exposes init parameters methods.
+     */
+    protected PlugInConfigContextAdapter currentPlugInConfigContextAdapter=null;
 
     /**
      * Get the module aware flag.
@@ -125,203 +124,32 @@
     public void init(ActionServlet servlet, ModuleConfig moduleConfig)
         throws ServletException {
 
-        // Create factory config object
-        DefinitionsFactoryConfig factoryConfig =
-            readFactoryConfig(servlet, moduleConfig);
-
-        // Set the module name in the config. This name will be used to compute
-        // the name under which the factory is stored.
-        factoryConfig.setFactoryName(moduleConfig.getPrefix());
-
         // Set RequestProcessor class
         this.initRequestProcessorClass(moduleConfig);
-
-        this.initTilesUtil();
-
-        this.initDefinitionsFactory(servlet.getServletContext(), moduleConfig, factoryConfig);
-    }
-
-    /**
-     * Set TilesUtil implementation according to properties 'tilesUtilImplClassname'
-     * and 'moduleAware'.  These properties are taken into account only once. A
-     * side effect is that only the values set in the first initialized plugin are
-     * effectively taken into account.
-     * @throws ServletException
-     */
-    private void initTilesUtil() throws ServletException {
-
-        if (TilesUtil.isTilesUtilImplSet()) {
-            return;
-        }
-
-        // Check if user has specified a TilesUtil implementation classname or not.
-        // If no implementation is specified, check if user has specified one
-        // shared single factory for all module, or one factory for each module.
-
-        if (this.getTilesUtilImplClassname() == null) {
-
-            if (isModuleAware()) {
-                TilesUtil.setTilesUtil(new TilesUtilStrutsModulesImpl());
-            } else {
-                TilesUtil.setTilesUtil(new TilesUtilStrutsImpl());
-            }
-
-        } else { // A classname is specified for the tilesUtilImp, use it.
-            try {
-                TilesUtilStrutsImpl impl =
-                    (TilesUtilStrutsImpl) RequestUtils
-                        .applicationClass(getTilesUtilImplClassname())
-                        .newInstance();
-                TilesUtil.setTilesUtil(impl);
-
-            } catch (ClassCastException ex) {
-                throw new ServletException(
-                    "Can't set TilesUtil implementation to '"
-                        + getTilesUtilImplClassname()
-                        + "'. TilesUtil implementation should be a subclass of '"
-                        + TilesUtilStrutsImpl.class.getName()
-                        + "'");
-
-            } catch (Exception ex) {
-                throw new ServletException(
-                    "Can't set TilesUtil implementation.",
-                    ex);
-            }
-        }
-
-    }
-
-    /**
-     * Initialize the DefinitionsFactory this module will use.
-     * @param servletContext
-     * @param moduleConfig
-     * @param factoryConfig
-     * @throws ServletException
-     */
-    private void initDefinitionsFactory(
-        ServletContext servletContext,
-        ModuleConfig moduleConfig,
-        DefinitionsFactoryConfig factoryConfig)
-        throws ServletException {
-
-        // Check if a factory already exist for this module
-        definitionFactory =
-            ((TilesUtilStrutsImpl) TilesUtil.getTilesUtil()).getDefinitionsFactory(
-                servletContext,
-                moduleConfig);
-
-        if (definitionFactory != null) {
-            log.info(
-                "Factory already exists for module '"
-                    + moduleConfig.getPrefix()
-                    + "'. The factory found is from module '"
-                    + definitionFactory.getConfig().getFactoryName()
-                    + "'. No new creation.");
-
-            return;
-        }
-
-        // Create configurable factory
+        
+        // Initialize Tiles
         try {
-            definitionFactory =
-                TilesUtil.createDefinitionsFactory(
-                    servletContext,
-                    factoryConfig);
-
-        } catch (DefinitionsFactoryException ex) {
-            log.error(
-                "Can't create Tiles definition factory for module '"
-                    + moduleConfig.getPrefix()
-                    + "'.");
-
-            throw new ServletException(ex);
+            TilesContainerFactory factory = TilesContainerFactory
+                    .getFactory(currentPlugInConfigContextAdapter);
+            TilesContainer container = factory.createContainer(
+                    currentPlugInConfigContextAdapter);
+            TilesAccess.setContainer(currentPlugInConfigContextAdapter,
+                    container);
+        } catch (TilesException e) {
+            log.fatal("Unable to retrieve tiles factory.", e);
+            throw new IllegalStateException("Unable to instantiate container.");
         }
-
-        log.info(
-            "Tiles definition factory loaded for module '"
-                + moduleConfig.getPrefix()
-                + "'.");
     }
 
     /**
      * End plugin.
      */
     public void destroy() {
-        definitionFactory.destroy();
-        definitionFactory = null;
-    }
-
-    /**
-     * Create FactoryConfig and initialize it from web.xml and struts-config.xml.
-     *
-     * @param servlet ActionServlet that is managing all the modules
-     *  in this web application.
-     * @param config ModuleConfig for the module with which
-     *  this plugin is associated.
-     * @exception ServletException if this <code>PlugIn</code> cannot
-     *  be successfully initialized.
-     */
-    protected DefinitionsFactoryConfig readFactoryConfig(
-        ActionServlet servlet,
-        ModuleConfig config)
-        throws ServletException {
-
-        // Create tiles definitions config object
-        DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
-        // Get init parameters from web.xml files
-        try {
-            DefinitionsUtil.populateDefinitionsFactoryConfig(
-                factoryConfig,
-                servlet.getServletConfig());
-
-        } catch (Exception ex) {
-            if (log.isDebugEnabled()){
-                log.debug("", ex);
-            }
-            ex.printStackTrace();
-            throw new UnavailableException(
-                "Can't populate DefinitionsFactoryConfig class from 'web.xml': "
-                    + ex.getMessage());
-        }
-
-        // Get init parameters from struts-config.xml
         try {
-            Map strutsProperties = findStrutsPlugInConfigProperties(servlet, config);
-            factoryConfig.populate(strutsProperties);
-
-        } catch (Exception ex) {
-            if (log.isDebugEnabled()) {
-                log.debug("", ex);
-            }
-
-            throw new UnavailableException(
-                "Can't populate DefinitionsFactoryConfig class from '"
-                    + config.getPrefix()
-                    + "/struts-config.xml':"
-                    + ex.getMessage());
+            TilesAccess.setContainer(currentPlugInConfigContextAdapter, null);
+        } catch (TilesException e) {
+            log.warn("Unable to remove tiles container from service.");
         }
-
-        return factoryConfig;
-    }
-
-    /**
-     * Find original properties set in the Struts PlugInConfig object.
-     * First, we need to find the index of this plugin. Then we retrieve the array of configs
-     * and then the object for this plugin.
-     * @param servlet ActionServlet that is managing all the modules
-     *  in this web application.
-     * @param config ModuleConfig for the module with which
-     *  this plug in is associated.
-     *
-     * @exception ServletException if this <code>PlugIn</code> cannot
-     *  be successfully initialized.
-     */
-    protected Map findStrutsPlugInConfigProperties(
-        ActionServlet servlet,
-        ModuleConfig config)
-        throws ServletException {
-
-        return currentPlugInConfigObject.getProperties();
     }
 
     /**
@@ -384,29 +212,13 @@
     }
 
     /**
-     * Set Tiles util implemention classname.
-     * If this property is set, the flag <code>moduleAware</code> will not be used anymore.
-     * @param tilesUtilImplClassname Classname.
-     */
-    public void setTilesUtilImplClassname(String tilesUtilImplClassname) {
-        this.tilesUtilImplClassname = tilesUtilImplClassname;
-    }
-
-    /**
-     * Get Tiles util implemention classname.
-     * @return The classname or <code>null</code> if none is set.
-     */
-    public String getTilesUtilImplClassname() {
-        return tilesUtilImplClassname;
-    }
-
-    /**
      * Method used by the ActionServlet initializing this plugin.
      * Set the plugin config object read from module config.
      * @param plugInConfigObject PlugInConfig.
      */
     public void setCurrentPlugInConfigObject(PlugInConfig plugInConfigObject) {
         this.currentPlugInConfigObject = plugInConfigObject;
+        currentPlugInConfigContextAdapter = new PlugInConfigContextAdapter(
+                this.currentPlugInConfigObject);
     }
-
 }

Modified: struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesRequestProcessor.java
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesRequestProcessor.java?view=diff&rev=487201&r1=487200&r2=487201
==============================================================================
--- struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesRequestProcessor.java (original)
+++ struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/TilesRequestProcessor.java Thu Dec 14 05:07:11 2006
@@ -23,6 +23,7 @@
 
 import java.io.IOException;
 
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -33,6 +34,9 @@
 import org.apache.struts.action.RequestProcessor;
 import org.apache.struts.config.ForwardConfig;
 import org.apache.struts.config.ModuleConfig;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.TilesException;
+import org.apache.tiles.access.TilesAccess;
 
 /**
  * <p><strong>RequestProcessor</strong> contains the processing logic that
@@ -55,14 +59,11 @@
 public class TilesRequestProcessor extends RequestProcessor {
 
     /**
-     * Definitions factory.
-     */
-    protected DefinitionsFactory definitionsFactory = null;
-
-    /**
      * Commons Logging instance.
      */
     protected static Log log = LogFactory.getLog(TilesRequestProcessor.class);
+    
+    protected ServletContext servletContext;
 
     /**
      * Initialize this request processor instance.
@@ -75,68 +76,6 @@
         throws ServletException {
 
         super.init(servlet, moduleConfig);
-        this.initDefinitionsMapping();
-    }
-
-    /**
-     * Read component instance mapping configuration file.
-     * This is where we read files properties.
-     */
-    protected void initDefinitionsMapping() throws ServletException {
-        // Retrieve and set factory for this modules
-        definitionsFactory =
-            (
-                (TilesUtilStrutsImpl) TilesUtil
-                    .getTilesUtil())
-                    .getDefinitionsFactory(
-                getServletContext(),
-                moduleConfig);
-
-        if (definitionsFactory == null) { // problem !
-
-            log.info(
-                "Definition Factory not found for module '"
-                    + moduleConfig.getPrefix()
-                    + "'. "
-                    + "Have you declared the appropriate plugin in struts-config.xml ?");
-
-            return;
-        }
-
-        log.info(
-            "Tiles definition factory found for request processor '"
-                + moduleConfig.getPrefix()
-                + "'.");
-
-    }
-
-    /**
-     * Process a Tile definition name.
-     * This method tries to process the parameter <code>definitionName</code>
-     * as a definition name.
-     * It returns <code>true</code> if a definition has been processed, or
-     * <code>false</code> otherwise.
-     * This method is deprecated; the method without the
-     * <code>contextRelative</code> parameter should be used instead.
-     *
-     * @param definitionName Definition name to insert.
-     * @param contextRelative Is the definition marked contextRelative ?
-     * @param request Current page request.
-     * @param response Current page response.
-     * @return <code>true</code> if the method has processed uri as a
-     * definition name, <code>false</code> otherwise.
-     * @deprecated use processTilesDefinition(definitionName, request, response)
-     *  instead.  This method will be removed in a version after 1.3.0.
-     */
-    protected boolean processTilesDefinition(
-        String definitionName,
-        boolean contextRelative,
-        HttpServletRequest request,
-        HttpServletResponse response)
-        throws IOException, ServletException {
-
-        return processTilesDefinition(definitionName, request, response);
-
     }
 
     /**
@@ -158,120 +97,31 @@
         HttpServletResponse response)
         throws IOException, ServletException {
 
-        // Do we do a forward (original behavior) or an include ?
-        boolean doInclude = false;
-
-        // Controller associated to a definition, if any
-        Controller controller = null;
-
-        // Computed uri to include
-        String uri = null;
-
-        ComponentContext tileContext = null;
-
-        try {
-            // Get current tile context if any.
-            // If context exist, we will do an include
-            tileContext = ComponentContext.getContext(request);
-            doInclude = (tileContext != null);
-            ComponentDefinition definition = null;
-
-            // Process tiles definition names only if a definition factory exist,
-            // and definition is found.
-            if (definitionsFactory != null) {
-                // Get definition of tiles/component corresponding to uri.
-                try {
-                    definition =
-                        definitionsFactory.getDefinition(
-                            definitionName,
-                            request,
-                            getServletContext());
-                } catch (NoSuchDefinitionException ex) {
-                    // Ignore not found
-                    log.debug("NoSuchDefinitionException " + ex.getMessage());
-                }
-                if (definition != null) { // We have a definition.
-                    // We use it to complete missing attribute in context.
-                    // We also get uri, controller.
-                    uri = definition.getPath();
-                    controller = definition.getOrCreateController();
-
-                    if (tileContext == null) {
-                        tileContext =
-                            new ComponentContext(definition.getAttributes());
-                        ComponentContext.setContext(tileContext, request);
-
-                    } else {
-                        tileContext.addMissing(definition.getAttributes());
-                    }
-                }
-            }
-
-            // Process definition set in Action, if any.
-            definition = DefinitionsUtil.getActionDefinition(request);
-            if (definition != null) { // We have a definition.
-                // We use it to complete missing attribute in context.
-                // We also overload uri and controller if set in definition.
-                if (definition.getPath() != null) {
-                    uri = definition.getPath();
-                }
-
-                if (definition.getOrCreateController() != null) {
-                    controller = definition.getOrCreateController();
-                }
-
-                if (tileContext == null) {
-                    tileContext =
-                        new ComponentContext(definition.getAttributes());
-                    ComponentContext.setContext(tileContext, request);
-                } else {
-                    tileContext.addMissing(definition.getAttributes());
-                }
-            }
-
-        } catch (java.lang.InstantiationException ex) {
-
-            log.error("Can't create associated controller", ex);
-
-            throw new ServletException(
-                "Can't create associated controller",
-                ex);
-        } catch (DefinitionsFactoryException ex) {
-            throw new ServletException(ex);
-        }
-
-        // Have we found a definition ?
-        if (uri == null) {
+        TilesContainer container = TilesAccess.getContainer(servlet
+                .getServletContext());
+        if (container == null) {
+            log.debug("Tiles container not found, so pass to next command.");
             return false;
         }
-
-        // Execute controller associated to definition, if any.
-        if (controller != null) {
+        
+        boolean retValue = false;
+        
+        if (container.isValidDefinition(request, response, definitionName)) {
+            retValue = response.isCommitted();
             try {
-                controller.execute(
-                    tileContext,
-                    request,
-                    response,
-                    getServletContext());
-
-            } catch (Exception e) {
-                throw new ServletException(e);
+                container.render(request, response, definitionName);
+            } catch (TilesException e) {
+                throw new ServletException("Cannot render definition '"
+                        + definitionName + "'");
             }
-        }
-
-        // If request comes from a previous Tile, do an include.
-        // This allows to insert an action in a Tile.
-        if (log.isDebugEnabled()) {
-            log.debug("uri=" + uri + " doInclude=" + doInclude);
-        }
-
-        if (doInclude) {
-            doInclude(uri, request, response);
         } else {
-            doForward(uri, request, response); // original behavior
+            // ignore not found
+            if (log.isDebugEnabled()) {
+                log.debug("Cannot find definition '" + definitionName + "'");
+            }
         }
-
-        return true;
+        
+        return retValue;
     }
 
     /**
@@ -397,12 +247,4 @@
 
         super.internalModuleRelativeInclude(uri, request, response);
     }
-
-    /**
-     * Get associated definition factory.
-     */
-    public DefinitionsFactory getDefinitionsFactory() {
-        return definitionsFactory;
-    }
-
 }

Added: struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java?view=auto&rev=487201
==============================================================================
--- struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java (added)
+++ struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java Thu Dec 14 05:07:11 2006
@@ -0,0 +1,68 @@
+/*
+ * $Id$
+ *
+ * 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.
+ */
+
+package org.apache.struts.tiles2.util;
+
+import java.util.Enumeration;
+
+import org.apache.commons.collections.iterators.IteratorEnumeration;
+import org.apache.struts.config.PlugInConfig;
+
+/**
+ * Adapts a {@link PlugInConfig} object to become a context-like object,
+ * exposing init parameters methods.
+ */
+public class PlugInConfigContextAdapter {
+
+    /**
+     * The internal plugin config object.
+     */
+    private PlugInConfig plugInConfigObject;
+
+    /**
+     * Constructor.
+     *
+     * @param plugInConfigObject The plugin config object to use.
+     */
+    public PlugInConfigContextAdapter(PlugInConfig plugInConfigObject) {
+        this.plugInConfigObject = plugInConfigObject;
+    }
+
+    /**
+     * Returns an initialization parameter.
+     *
+     * @param parameterName The name of the parameter.
+     * @return The value of the parameter.
+     */
+    public String getInitParameter(String parameterName) {
+        return plugInConfigObject.getProperties().get(parameterName).toString();
+    }
+
+    /**
+     * Returns the names of all initialization parameters.
+     *
+     * @return The names of all initialization parameters.
+     */
+    public Enumeration getInitParameterNames() {
+        return new IteratorEnumeration(plugInConfigObject.getProperties()
+                .keySet().iterator());
+    }
+}
\ No newline at end of file

Propchange: struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/struts1/trunk/tiles2/src/main/java/org/apache/struts/tiles2/util/PlugInConfigContextAdapter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL