You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by gr...@apache.org on 2006/02/21 00:40:18 UTC

svn commit: r379284 - in /struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener: ./ TilesListener.java

Author: greddin
Date: Mon Feb 20 15:40:16 2006
New Revision: 379284

URL: http://svn.apache.org/viewcvs?rev=379284&view=rev
Log:
Ticket #38255.  Added TilesListener class to replace TilesServlet in 2.3+ environments.
Patch submitted by Matt Raible.


Added:
    struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/
    struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java   (with props)

Added: struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java?rev=379284&view=auto
==============================================================================
--- struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java (added)
+++ struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java Mon Feb 20 15:40:16 2006
@@ -0,0 +1,180 @@
+package org.apache.tiles.listener;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Enumeration;
+import java.util.logging.Logger;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.servlet.UnavailableException;
+import org.apache.tiles.DefinitionsFactory;
+import org.apache.tiles.DefinitionsFactoryConfig;
+import org.apache.tiles.DefinitionsFactoryException;
+import org.apache.tiles.TilesUtil;
+import org.apache.tiles.TilesUtilImpl;
+
+/**
+ * @author mraible
+ * @version $Revision$ $Date$
+ */
+public class TilesListener implements ServletContextListener {
+
+    /**
+     * The logger for this class
+    */
+   protected static Logger logger = Logger.getLogger(TilesListener.class.
+                                                                      getName());
+
+
+    /**
+     * The default name of a context init parameter that specifies the Tiles configuration file
+    */
+    private static final String DEFAULT_CONFIG_FILE_PARAM = "definitions-config";
+
+
+    /**
+     * The default name of the Tiles configuration file
+    */
+    private static final String DEFAULT_CONFIG_FILE = "/WEB-INF/tiles.xml";
+
+
+    /**
+     * An error message stating that something went wrong during initialization
+    */
+    private static final String CANT_POPULATE_FACTORY_ERROR =
+         "CAN'T POPULATE TILES DEFINITION FACTORY";
+
+
+    /**
+     * The Tiles definition factory
+    */
+   protected DefinitionsFactory definitionFactory = null;
+
+
+    /**
+     * A comma-separated list of filenames representing the
+     * application's Tiles configuration files.
+    */
+    private String configFiles = null;
+
+    public void contextInitialized(ServletContextEvent event) {
+        logger.info("Initializing TilesListener");
+        configFiles = event.getServletContext().getInitParameter(DEFAULT_CONFIG_FILE_PARAM);
+
+        try {
+            ServletContext context = event.getServletContext();
+
+            // Create factory config object
+            DefinitionsFactoryConfig fconfig = readFactoryConfig(context);
+            fconfig.setModuleAware(false);
+
+            TilesUtil.setTilesUtil(new TilesUtilImpl());
+            initDefinitionsFactory(context, fconfig);
+        }
+        catch(Exception ex) {
+            saveExceptionMessage(event.getServletContext(), ex);
+                    throw new RuntimeException(ex.getMessage(), ex);
+        }
+
+    }
+
+    public void contextDestroyed(ServletContextEvent event) {
+        this.definitionFactory = null;
+    }
+
+
+    /**
+     * Populates the tiles factory configuration. If a
+     * context init param named <i>definitions-config</i>
+     * was defined, that param's value is assumed to be
+     * a comma-separated list of configuration file names,
+     * all of which are processed. If a
+     * <i>definitions-config</i> context param was not
+     * specified, Tiles assumes that your Tiles definition
+     * file is <code>/WEB-INF/tiles.xml</code>.
+     */
+    protected DefinitionsFactoryConfig readFactoryConfig(ServletContext context)
+        throws ServletException {
+        DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
+        Map map = new HashMap();
+
+        try {
+            if(configFiles != null) {
+                logger.info("CONFIG FILES DEFINED IN WEB.XML");
+               map.put(DEFAULT_CONFIG_FILE_PARAM, configFiles);
+           }
+            else {
+                logger.info("CONFIG FILES WERE NOT DEFINED IN WEB.XML, " +
+                              "LOOKING FOR " + DEFAULT_CONFIG_FILE);
+               map.put(DEFAULT_CONFIG_FILE_PARAM, DEFAULT_CONFIG_FILE);
+            }
+
+            populateConfigParameterMap(context, map);
+            factoryConfig.populate(map);
+        }
+        catch (Exception ex) {
+            saveExceptionMessage(context, ex);
+           throw new UnavailableException(CANT_POPULATE_FACTORY_ERROR + ex.getMessage());
+        }
+        return factoryConfig;
+    }
+
+
+    /**
+     * Initializes the Tiles definitions factory.
+     *
+     * @param servletContext The servlet context
+     * @param factoryConfig The definitions factory config
+     */
+   private void initDefinitionsFactory(ServletContext servletContext,
+                                       DefinitionsFactoryConfig factoryConfig)
+                                                    throws ServletException {
+        logger.info("initializing definitions factory...");
+        // Create configurable factory
+        try {
+            definitionFactory = TilesUtil.createDefinitionsFactory(
+                                            servletContext, factoryConfig);
+        } catch (DefinitionsFactoryException ex) {
+                    ex.printStackTrace();
+            throw new ServletException(ex.getMessage(), ex);
+        }
+    }
+
+
+    /**
+     * Stores the message associated with any exception thrown in this
+     * servlet in application scope. Tiles later accesses that message
+     * if an exception is thrown when the tiles:insert tag is
+     * activated.
+     *
+     * @param context The servlet configuration
+     * @param ex An exception
+     */
+    private void saveExceptionMessage(ServletContext context, Exception ex) {
+       logger.warning("Caught exception when initializing definitions factory");
+       logger.warning(ex.getMessage());
+       logger.warning(ex.toString());
+       context.setAttribute("TILES_INIT_EXCEPTION", ex.getMessage());
+    }
+
+    /**
+     * Populates a map with the parameters contained in the context configuration.
+     *
+     * @param context The servlet context
+     * @param paramMap The map to fill
+     */
+    private void populateConfigParameterMap(ServletContext context, Map paramMap) {
+        Enumeration enumeration;
+        String paramName;
+
+        enumeration = context.getInitParameterNames();
+        while (enumeration.hasMoreElements()) {
+            paramName = (String) enumeration.nextElement();
+            if (!paramMap.containsKey(paramName)) {
+                paramMap.put(paramName, context.getInitParameter(paramName));
+            }
+        }
+    }
+}

Propchange: struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: svn commit: r379284 - in /struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener: ./ TilesListener.java

Posted by Matt Raible <mr...@gmail.com>.
I have no problem with removing it.

On 2/21/06, Ted Husted <te...@gmail.com> wrote:
> On 2/21/06, Greg Reddin <gr...@apache.org> wrote:
> > I did acknowledge Matt in the commit message.  I can remove the
> > author tag if I need to.
>
> It would be better to get an affirmative statement from Matt that is
> OK to remove the @author tag. (If for no other reason, than to be sure
> he knows the procedure for his next patch.)
>
> -Ted.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: svn commit: r379284 - in /struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener: ./ TilesListener.java

Posted by Ted Husted <te...@gmail.com>.
On 2/21/06, Greg Reddin <gr...@apache.org> wrote:
> I did acknowledge Matt in the commit message.  I can remove the
> author tag if I need to.

It would be better to get an affirmative statement from Matt that is
OK to remove the @author tag. (If for no other reason, than to be sure
he knows the procedure for his next patch.)

-Ted.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: svn commit: r379284 - in /struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener: ./ TilesListener.java

Posted by Greg Reddin <gr...@apache.org>.
On Feb 20, 2006, at 8:02 PM, Wendy Smoak wrote:

>> +/**
>> + * @author mraible
>>
>
> Is Standalone Tiles using @author tags, or should this be removed
> (with Matt's permission)?

I wasn't sure how to handle that.  I remember the discussions some  
time ago about removing author tags, but I'm not sure if that  
protocol is still in use.  I want to be in sync with whatever the  
rest of the community is doing in that regard.  Grepping the whole  
"current" repository I find @author tags scattered here and there.   
As you might guess there is no consistent standard :-)

I did acknowledge Matt in the commit message.  I can remove the  
author tag if I need to.

Greg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: svn commit: r379284 - in /struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener: ./ TilesListener.java

Posted by Wendy Smoak <ws...@gmail.com>.
On 2/20/06, greddin@apache.org <gr...@apache.org> wrote:
> Author: greddin
> Date: Mon Feb 20 15:40:16 2006
> New Revision: 379284
>
> URL: http://svn.apache.org/viewcvs?rev=379284&view=rev
> Log:
> Ticket #38255.  Added TilesListener class to replace TilesServlet in 2.3+ environments.
> Patch submitted by Matt Raible.
...
> Added: struts/sandbox/trunk/tiles/src/java/org/apache/tiles/listener/TilesListener.java
...
> +/**
> + * @author mraible

Is Standalone Tiles using @author tags, or should this be removed
(with Matt's permission)?

The only other class that has one is TilesServlet -- @author David Geary.

Thanks,
--
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org