You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2007/02/23 16:41:14 UTC

svn commit: r510985 - in /tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles: context/ context/enhanced/ context/jsp/ context/portlet/ context/servlet/ factory/ impl/

Author: apetrelli
Date: Fri Feb 23 07:41:13 2007
New Revision: 510985

URL: http://svn.apache.org/viewvc?view=rev&rev=510985
Log:
TILES-120
Replaced the BasicTilesContextFactory with ChainedTilesContextFactory.

Added:
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java   (with props)
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java   (with props)
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java   (with props)
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java   (with props)
Removed:
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicTilesContextFactory.java
Modified:
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java

Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java?view=auto&rev=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java Fri Feb 23 07:41:13 2007
@@ -0,0 +1,118 @@
+/*
+ * $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.tiles.context;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.TilesRequestContext;
+
+import java.util.Map;
+
+/**
+ * Default implementation for TilesContextFactory, that creates a chain of
+ * sub-factories, trying each one until it returns a not-null value.
+ * 
+ * @version $Rev$ $Date$
+ */
+public class ChainedTilesContextFactory implements TilesContextFactory {
+
+    public static final String FACTORY_CLASS_NAMES = "org.apache.tiles.context.ChainTilesContextFactory.FACTORY_CLASS_NAMES";
+
+    public static final String[] DEFAULT_FACTORY_CLASS_NAMES = {
+            "org.apache.tiles.context.servlet.ServletTilesContextFactory",
+            "org.apache.tiles.context.portlet.PortletTilesContextFactory",
+            "org.apache.tiles.context.jsp.JspTilesContextFactory" };
+
+    private TilesContextFactory[] factories;
+
+    public void init(Map configParameters) {
+        String[] classNames = null;
+        String classNamesString = (String) configParameters
+                .get(FACTORY_CLASS_NAMES);
+        if (classNamesString != null) {
+            classNames = classNamesString.split("\\s*,\\s*");
+        }
+        if (classNames == null || classNames.length <= 0) {
+            classNames = DEFAULT_FACTORY_CLASS_NAMES;
+        }
+
+        factories = new TilesContextFactory[classNames.length];
+        for (int i = 0; i < classNames.length; i++) {
+            try {
+                Class clazz = Class.forName(classNames[i]);
+                if (TilesContextFactory.class.isAssignableFrom(clazz)) {
+                    factories[i] = (TilesContextFactory) clazz.newInstance();
+                } else {
+                    throw new IllegalArgumentException("The class "
+                            + classNames[i]
+                            + " does not implement TilesContextFactory");
+                }
+            } catch (ClassNotFoundException e) {
+                throw new IllegalArgumentException(
+                        "Cannot find TilesContextFactory class "
+                                + classNames[i], e);
+            } catch (InstantiationException e) {
+                throw new IllegalArgumentException(
+                        "Cannot instantiate TilesFactoryClass " + classNames[i],
+                        e);
+            } catch (IllegalAccessException e) {
+                throw new IllegalArgumentException(
+                        "Cannot access TilesFactoryClass " + classNames[i]
+                                + " default constructor", e);
+            }
+        }
+    }
+
+    /**
+     * Creates a TilesApplicationContext from the given context.
+     */
+    public TilesApplicationContext createApplicationContext(Object context) {
+        TilesApplicationContext retValue = null;
+
+        for (int i = 0; i < factories.length && retValue == null; i++) {
+            retValue = factories[i].createApplicationContext(context);
+        }
+
+        if (retValue == null) {
+            throw new IllegalArgumentException(
+                    "Cannot find a factory to create the application context");
+        }
+
+        return retValue;
+    }
+
+    public TilesRequestContext createRequestContext(
+            TilesApplicationContext context, Object... requestItems) {
+        TilesRequestContext retValue = null;
+
+        for (int i = 0; i < factories.length && retValue == null; i++) {
+            retValue = factories[i].createRequestContext(context, requestItems);
+        }
+
+        if (retValue == null) {
+            throw new IllegalArgumentException(
+                    "Cannot find a factory to create the request context");
+        }
+
+        return retValue;
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/ChainedTilesContextFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java?view=diff&rev=510985&r1=510984&r2=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java Fri Feb 23 07:41:13 2007
@@ -22,13 +22,13 @@
 package org.apache.tiles.context.enhanced;
 
 import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.ChainedTilesContextFactory;
 import org.apache.tiles.context.TilesRequestContext;
-import org.apache.tiles.context.BasicTilesContextFactory;
 
 /**
  * @version $Rev$ $Date$
  */
-public class EnhancedContextFactory extends BasicTilesContextFactory {
+public class EnhancedContextFactory extends ChainedTilesContextFactory {
 
     public TilesApplicationContext createApplicationContext(Object context) {
         TilesApplicationContext root = super.createApplicationContext(context);

Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java?view=auto&rev=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java Fri Feb 23 07:41:13 2007
@@ -0,0 +1,77 @@
+/*
+ * $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.tiles.context.jsp;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.TilesContextFactory;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.context.jsp.JspTilesRequestContext;
+import org.apache.tiles.context.servlet.ServletTilesApplicationContext;
+
+import javax.servlet.ServletContext;
+import javax.servlet.jsp.PageContext;
+import java.util.Map;
+
+/**
+ * Creates an instance of the appropriate TilesApplicationContext implementation.
+ *
+ * @version $Rev$ $Date$
+ */
+public class JspTilesContextFactory implements TilesContextFactory {
+
+    public void init(Map configParameters) {
+    }
+
+    /**
+     * Creates a TilesApplicationContext from the given context.
+     */
+    public TilesApplicationContext createApplicationContext(Object context) {
+        if (context instanceof ServletContext) {
+            ServletContext servletContext = (ServletContext) context;
+            return new ServletTilesApplicationContext(servletContext);
+        }
+        
+        return null;
+    }
+
+    public TilesRequestContext createRequestContext(TilesApplicationContext context,
+                                                    Object... requestItems) {
+        if (requestItems.length == 1) {
+            ServletContext servletContext = getServletContext(context);
+            if (servletContext != null) {
+                return new JspTilesRequestContext(servletContext, (PageContext) requestItems[0]);
+            }
+        }
+        
+        return null;
+    }
+
+    protected ServletContext getServletContext(TilesApplicationContext context) {
+        if (context instanceof ServletTilesApplicationContext) {
+            ServletTilesApplicationContext app = (ServletTilesApplicationContext) context;
+            return app.getServletContext();
+        }
+        return null;
+
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesContextFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java?view=auto&rev=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java Fri Feb 23 07:41:13 2007
@@ -0,0 +1,80 @@
+/*
+ * $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.tiles.context.portlet;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.TilesContextFactory;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.context.portlet.PortletTilesApplicationContext;
+import org.apache.tiles.context.portlet.PortletTilesRequestContext;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletResponse;
+import java.util.Map;
+
+/**
+ * Creates an instance of the appropriate TilesApplicationContext implementation.
+ *
+ * @version $Rev$ $Date$
+ */
+public class PortletTilesContextFactory implements TilesContextFactory {
+
+    public void init(Map configParameters) {
+    }
+
+    /**
+     * Creates a TilesApplicationContext from the given context.
+     */
+    public TilesApplicationContext createApplicationContext(Object context) {
+        if (context instanceof PortletContext) {
+            PortletContext portletContext = (PortletContext) context;
+            return new PortletTilesApplicationContext(portletContext);
+        }
+        
+        return null;
+    }
+
+    public TilesRequestContext createRequestContext(TilesApplicationContext context,
+                                                    Object... requestItems) {
+        if (requestItems.length == 2) {
+            PortletContext portletContext = getPortletContext(context);
+            if (portletContext != null) {
+                PortletTilesApplicationContext app = (PortletTilesApplicationContext) context;
+                return new PortletTilesRequestContext(app.getPortletContext(),
+                    (PortletRequest) requestItems[0],
+                    (PortletResponse) requestItems[1]);
+            }
+        }
+        
+        return null;
+    }
+
+    protected PortletContext getPortletContext(TilesApplicationContext context) {
+        if (context instanceof PortletTilesApplicationContext) {
+            PortletTilesApplicationContext app = (PortletTilesApplicationContext) context;
+            return app.getPortletContext();
+        }
+        return null;
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesContextFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java?view=auto&rev=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java Fri Feb 23 07:41:13 2007
@@ -0,0 +1,79 @@
+/*
+ * $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.tiles.context.servlet;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.TilesContextFactory;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.context.servlet.ServletTilesApplicationContext;
+import org.apache.tiles.context.servlet.ServletTilesRequestContext;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Map;
+
+/**
+ * Creates an instance of the appropriate TilesApplicationContext implementation.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ServletTilesContextFactory implements TilesContextFactory {
+
+    public void init(Map configParameters) {
+    }
+
+    /**
+     * Creates a TilesApplicationContext from the given context.
+     */
+    public TilesApplicationContext createApplicationContext(Object context) {
+        if (context instanceof ServletContext) {
+            ServletContext servletContext = (ServletContext) context;
+            return new ServletTilesApplicationContext(servletContext);
+        }
+        return null;
+    }
+
+    public TilesRequestContext createRequestContext(TilesApplicationContext context,
+                                                    Object... requestItems) {
+        if (requestItems.length == 2) {
+            ServletContext servletContext = getServletContext(context);
+            if (servletContext != null) {
+                return new ServletTilesRequestContext(servletContext,
+                    (HttpServletRequest) requestItems[0],
+                    (HttpServletResponse) requestItems[1]);
+            }
+        }
+        
+        return null;
+    }
+
+    protected ServletContext getServletContext(TilesApplicationContext context) {
+        if (context instanceof ServletTilesApplicationContext) {
+            ServletTilesApplicationContext app = (ServletTilesApplicationContext) context;
+            return app.getServletContext();
+        }
+        return null;
+
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesContextFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java?view=diff&rev=510985&r1=510984&r2=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java Fri Feb 23 07:41:13 2007
@@ -24,7 +24,7 @@
 import org.apache.tiles.TilesApplicationContext;
 import org.apache.tiles.TilesContainer;
 import org.apache.tiles.TilesException;
-import org.apache.tiles.context.BasicTilesContextFactory;
+import org.apache.tiles.context.ChainedTilesContextFactory;
 import org.apache.tiles.context.TilesContextFactory;
 import org.apache.tiles.definition.DefinitionsFactory;
 import org.apache.tiles.definition.UrlDefinitionsFactory;
@@ -71,7 +71,7 @@
 
     static {
         DEFAULTS.put(CONTAINER_FACTORY_INIT_PARAM, TilesContainerFactory.class.getName());
-        DEFAULTS.put(CONTEXT_FACTORY_INIT_PARAM, BasicTilesContextFactory.class.getName());
+        DEFAULTS.put(CONTEXT_FACTORY_INIT_PARAM, ChainedTilesContextFactory.class.getName());
         DEFAULTS.put(DEFINITIONS_FACTORY_INIT_PARAM, UrlDefinitionsFactory.class.getName());
         DEFAULTS.put(PREPARER_FACTORY_INIT_PARAM, BasicPreparerFactory.class.getName());
     }
@@ -187,6 +187,7 @@
             (PreparerFactory) createFactory(configuration,
                 PREPARER_FACTORY_INIT_PARAM);
 
+        contextFactory.init(configuration);
         TilesApplicationContext tilesContext =
             contextFactory.createApplicationContext(context);
 

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?view=diff&rev=510985&r1=510984&r2=510985
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java Fri Feb 23 07:41:13 2007
@@ -100,8 +100,6 @@
             LOG.info("Initializing Tiles2 container. . .");
         }
 
-        contextFactory.init(initParameters);
-
         //Everything is now initialized.  We will populate
         // our definitions
         initializeDefinitionsFactory(definitionsFactory, getResourceString(),