Author: ddewolf Date: Fri Nov 3 09:32:25 2006 New Revision: 470918 URL: http://svn.apache.org/viewvc?view=rev&rev=470918 Log: Adding support for classpath definitions
Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java (with props) struts/sandbox/trunk/tiles/tiles-test/src/main/resources/ struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/ struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/ struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/ struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml (with props) struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp (with props) Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/BasicTilesContextFactory.java struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/WEB-INF/web.xml struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/index.jsp Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/BasicTilesContextFactory.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/BasicTilesContextFactory.java?view=diff&rev=470918&r1=470917&r2=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/BasicTilesContextFactory.java (original) +++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/BasicTilesContextFactory.java Fri Nov 3 09:32:25 2006 @@ -67,13 +67,13 @@ public TilesRequestContext createRequestContext(TilesApplicationContext context, Object request, Object response) { - if (context instanceof ServletTilesApplicationContext) { - ServletTilesApplicationContext app = (ServletTilesApplicationContext) context; - - return new ServletTilesRequestContext(app.getServletContext(), + ServletContext servletContext = getServletContext(context); + PortletContext portletContext = getPortletContext(context); + if (servletContext != null) { + return new ServletTilesRequestContext(servletContext, (HttpServletRequest) request, (HttpServletResponse) response); - } else if (context instanceof PortletContext) { + } else if (portletContext != null) { PortletTilesApplicationContext app = (PortletTilesApplicationContext) context; return new PortletTilesRequestContext(app.getPortletContext(), (PortletRequest) request, @@ -86,10 +86,27 @@ public TilesRequestContext createRequestContext(TilesApplicationContext context, PageContext pageContext) { + ServletContext servletContext = getServletContext(context); + if (servletContext != null) { + return new JspTilesRequestContext(servletContext, pageContext); + } + throw new IllegalArgumentException("The context/pageContext combination is not supported."); + } + + protected ServletContext getServletContext(TilesApplicationContext context) { if (context instanceof ServletTilesApplicationContext) { ServletTilesApplicationContext app = (ServletTilesApplicationContext) context; - return new JspTilesRequestContext(app.getServletContext(), pageContext); + return app.getServletContext(); } - throw new IllegalArgumentException("The context/pageContext combination is not supported."); + return null; + + } + + protected PortletContext getPortletContext(TilesApplicationContext context) { + if (context instanceof PortletTilesApplicationContext) { + PortletTilesApplicationContext app = (PortletTilesApplicationContext) context; + return app.getPortletContext(); + } + return null; } } Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java?view=auto&rev=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java (added) +++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java Fri Nov 3 09:32:25 2006 @@ -0,0 +1,49 @@ +/* + * 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.enhanced; + +import org.apache.tiles.TilesApplicationContext; +import org.apache.tiles.TilesRequestContext; +import org.apache.tiles.context.BasicTilesContextFactory; + +import javax.servlet.jsp.PageContext; + +public class EnhancedContextFactory extends BasicTilesContextFactory { + + public TilesApplicationContext createApplicationContext(Object context) { + TilesApplicationContext root = super.createApplicationContext(context); + return new EnhancedTilesApplicationContext(root); + } + + + public TilesRequestContext createRequestContext(TilesApplicationContext context, Object request, Object response) { + if (context instanceof EnhancedTilesApplicationContext) { + context = ((EnhancedTilesApplicationContext) context).getRootContext(); + } + return super.createRequestContext(context, request, response); + } + + public TilesRequestContext createRequestContext(TilesApplicationContext context, PageContext pageContext) { + if (context instanceof EnhancedTilesApplicationContext) { + context = ((EnhancedTilesApplicationContext) context).getRootContext(); + } + return super.createRequestContext(context, pageContext); + } +} Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedContextFactory.java ------------------------------------------------------------------------------ svn:keywords = Id Author Date Rev Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java?view=diff&rev=470918&r1=470917&r2=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java (original) +++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java Fri Nov 3 09:32:25 2006 @@ -57,6 +57,15 @@ this.rootContext = rootContext; } + + public TilesApplicationContext getRootContext() { + return rootContext; + } + + public void setRootContext(TilesApplicationContext rootContext) { + this.rootContext = rootContext; + } + public Map<String, Object> getApplicationScope() { return rootContext.getApplicationScope(); } @@ -69,6 +78,7 @@ URL rootUrl = rootContext.getResource(path); if(rootUrl == null) { Set<URL> resources = getResources(path); + resources.remove(null); if(resources.size() > 0) { rootUrl = resources.toArray(new URL[resources.size()])[0]; } @@ -79,11 +89,21 @@ public Set<URL> getResources(String path) throws IOException { Set<URL> resources = new HashSet<URL>(); resources.addAll(rootContext.getResources(path)); + resources.addAll(getClasspathResources(path)); + return resources; + } + + public Set<URL> getClasspathResources(String path) throws IOException { + Set<URL> resources = new HashSet<URL>(); resources.addAll(searchResources(getClass().getClassLoader(), path)); ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); if(contextLoader != null) { resources.addAll(searchResources(contextLoader, path)); + } + + if(resources.size() == 0 && path.startsWith("/")) { + return getClasspathResources(path.substring(1)); } return resources; Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?view=diff&rev=470918&r1=470917&r2=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java (original) +++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java Fri Nov 3 09:32:25 2006 @@ -106,6 +106,9 @@ } definitionsFactory.addSource(resourceUrl); } + else { + LOG.warn("Unable to find configured definition '"+resource+"'"); + } } } catch (IOException e) { throw new DefinitionsFactoryException("Unable to parse definitions from " Added: struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml?view=auto&rev=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml (added) +++ struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml Fri Nov 3 09:32:25 2006 @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="ISO-8859-1" ?> + +<!DOCTYPE tiles-definitions PUBLIC + "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" + "http://struts.apache.org/dtds/tiles-config_2_0.dtd"> + +<!-- Definitions for Tiles documentation --> + +<tiles-definitions> + <definition name="classpath.definition" template="/layout.jsp"> + <put name="title" value="This is the title."/> + <put name="header" value="/header.jsp"/> + <put name="body" value="/body.jsp"/> + </definition> +</tiles-definitions> \ No newline at end of file Propchange: struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/sandbox/trunk/tiles/tiles-test/src/main/resources/org/apache/tiles/classpath-defs.xml ------------------------------------------------------------------------------ svn:keywords = Id Author Date Rev Modified: struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/WEB-INF/web.xml?view=diff&rev=470918&r1=470917&r2=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/WEB-INF/web.xml (original) +++ struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/WEB-INF/web.xml Fri Nov 3 09:32:25 2006 @@ -6,6 +6,11 @@ version="2.4"> <display-name>Tiles 2 Test Application</display-name> + + <context-param> + <param-name>org.apache.tiles.CONTEXT_FACTORY</param-name> + <param-value>org.apache.tiles.context.enhanced.EnhancedContextFactory</param-value> + </context-param> <!-- Standard Action Servlet Configuration --> <servlet> @@ -13,7 +18,7 @@ <servlet-class>org.apache.tiles.servlet.TilesServlet</servlet-class> <init-param> <param-name>definitions-config</param-name> - <param-value>/WEB-INF/tiles-defs.xml</param-value> + <param-value>/WEB-INF/tiles-defs.xml,/org/apache/tiles/classpath-defs.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> Modified: struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/index.jsp URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/index.jsp?view=diff&rev=470918&r1=470917&r2=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/index.jsp (original) +++ struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/index.jsp Fri Nov 3 09:32:25 2006 @@ -26,6 +26,7 @@ <h2>Currently working tests</h2> <a href="testinsertdefinition.jsp">Test Insert Configured Definition</a><br/> + <a href="testinsertdefinition_classpath.jsp">Test Insert Configured Classpath Definition</a><br/> <a href="testinsertdefinition_notype.jsp">Test Insert Configured Definition with no type specified</a><br/> <a href="testinsertdefinition_override.jsp">Test Insert Configured Definition with an overridden content</a><br/> <a href="testinsertdefinition_inline.jsp">Test Insert Configured Definition with an inline content</a><br/> Added: struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp?view=auto&rev=470918 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp (added) +++ struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp Fri Nov 3 09:32:25 2006 @@ -0,0 +1,3 @@ +<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> + +<tiles:insertDefinition name="classpath.definition" /> Propchange: struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/sandbox/trunk/tiles/tiles-test/src/main/webapp/testinsertdefinition_classpath.jsp ------------------------------------------------------------------------------ svn:keywords = Id Author Date Rev