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/17 17:50:36 UTC

svn commit: r488006 - in /struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles: factory/TilesContainerFactory.java impl/BasicTilesContainer.java

Author: apetrelli
Date: Sun Dec 17 08:50:35 2006
New Revision: 488006

URL: http://svn.apache.org/viewvc?view=rev&rev=488006
Log:
SB-101
Refactorings to allow easier inheritance.

Modified:
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java?view=diff&rev=488006&r1=488005&r2=488006
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java Sun Dec 17 08:50:35 2006
@@ -93,8 +93,28 @@
      */
     public static TilesContainerFactory getFactory(Object context)
         throws TilesException {
+        return getFactory(context, DEFAULTS);
+    }
+
+    /**
+     * Retrieve a factory instance as configured through the
+     * specified context.
+     * <p/>
+     * The context will be queried and if a init parameter
+     * named 'org.apache.tiles.CONTAINER_FACTORY' is discovered
+     * this class will be instantiated and returned. Otherwise,
+     * the factory will attempt to utilize one of it's internal
+     * factories.
+     *
+     * @param context the executing applications context.
+     *                Typically a ServletContext or PortletContext
+     * @return a tiles container
+     * @throws TilesException if an error occurs creating the factory.
+     */
+    public static TilesContainerFactory getFactory(Object context,
+    		Map<String, String> defaults) throws TilesException {
         return (TilesContainerFactory) TilesContainerFactory
-            .createFactory(context, CONTAINER_FACTORY_INIT_PARAM);
+            .createFactory(context, CONTAINER_FACTORY_INIT_PARAM, defaults);
     }
 
     public TilesContainer createContainer(Object context) throws TilesException {
@@ -108,30 +128,44 @@
 
     public TilesContainer createTilesContainer(Object context)
         throws TilesException {
+        return createTilesContainer(context, DEFAULTS);
+    }
+
+    public TilesContainer createTilesContainer(Object context,
+    		Map<String, String> defaults) throws TilesException {
         BasicTilesContainer container = new BasicTilesContainer();
-        initializeContainer(context, container);
+        initializeContainer(context, container, defaults);
         return container;
     }
 
     public MutableTilesContainer createMutableTilesContainer(Object context)
         throws TilesException {
+    	return createMutableTilesContainer(context, DEFAULTS);
+    }
+
+    public MutableTilesContainer createMutableTilesContainer(Object context,
+    		Map<String, String> defaults) throws TilesException {
         CachingTilesContainer container = new CachingTilesContainer();
-        initializeContainer(context, container);
+        initializeContainer(context, container, defaults);
         return container;
     }
 
-    public void initializeContainer(Object context,
-                                    BasicTilesContainer container)
+    protected void initializeContainer(Object context,
+                                    BasicTilesContainer container,
+                                    Map<String, String> defaults)
         throws TilesException {
 
         TilesContextFactory contextFactory =
-            (TilesContextFactory) createFactory(context, CONTEXT_FACTORY_INIT_PARAM);
+            (TilesContextFactory) createFactory(context,
+            		CONTEXT_FACTORY_INIT_PARAM, defaults);
 
         DefinitionsFactory defsFactory =
-            (DefinitionsFactory) createFactory(context, DEFINITIONS_FACTORY_INIT_PARAM);
+            (DefinitionsFactory) createFactory(context,
+            		DEFINITIONS_FACTORY_INIT_PARAM, defaults);
 
         PreparerFactory prepFactory =
-            (PreparerFactory) createFactory(context, PREPARER_FACTORY_INIT_PARAM);
+            (PreparerFactory) createFactory(context,
+            		PREPARER_FACTORY_INIT_PARAM, defaults);
 
         TilesApplicationContext tilesContext =
             contextFactory.createApplicationContext(context);
@@ -168,15 +202,21 @@
     }
 
 
-    public static Object createFactory(Object context, String initParameterName)
+    protected static Object createFactory(Object context,
+    		String initParameterName, Map<String, String> defaults)
         throws TilesException {
-        String factoryName = resolveFactoryName(context, initParameterName);
+        String factoryName = resolveFactoryName(context, initParameterName,
+        		defaults);
         return ClassUtil.instantiate(factoryName);
     }
 
-    public static String resolveFactoryName(Object context, String parameterName)
+    protected static String resolveFactoryName(Object context,
+    		String parameterName, Map<String, String> defaults)
         throws TilesException {
         Object factoryName = getInitParameter(context, parameterName);
+        if (factoryName == null && defaults != null) {
+        	factoryName = defaults.get(parameterName);
+        }
         return factoryName == null
             ? DEFAULTS.get(parameterName)
             : factoryName.toString();

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=488006&r1=488005&r2=488006
==============================================================================
--- 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 Sun Dec 17 08:50:35 2006
@@ -101,12 +101,42 @@
         }
 
         contextFactory.init(initParameters);
-        definitionsFactory.init(initParameters);
 
         //Everything is now initialized.  We will populate
         // our definitions
-        String resourceString = getResourceString();
+        initializeDefinitionsFactory(definitionsFactory, getResourceString(),
+        		initParameters);
+    }
+
+    /**
+     * Determine whether or not the container has been
+     * initialized. Utility method used for methods which
+     * can not be invoked after the container has been
+     * started.
+     *
+     * @throws IllegalStateException if the container has already been initialized.
+     */
+    protected void checkInit() {
+        if (initialized) {
+            throw new IllegalStateException("Container allready initialized");
+        }
+    }
+    
+    /**
+     * Initializes a definitions factory.
+     *
+     * @param definitionsFactory The factory to initializes. 
+     * @param resourceString The string containing a comma-separated-list of
+     * resources.
+     * @param initParameters A map containing the initialization parameters.
+     * @throws TilesException If something goes wrong.
+     */
+    protected void initializeDefinitionsFactory(
+    		DefinitionsFactory definitionsFactory, String resourceString,
+    		Map<String, String> initParameters) throws TilesException {
         List<String> resources = getResourceNames(resourceString);
+        definitionsFactory.init(initParameters);
+        
         try {
             for (String resource : resources) {
                 URL resourceUrl = context.getResource(resource);
@@ -129,20 +159,6 @@
     }
 
     /**
-     * Determine whether or not the container has been
-     * initialized. Utility method used for methods which
-     * can not be invoked after the container has been
-     * started.
-     *
-     * @throws IllegalStateException if the container has already been initialized.
-     */
-    private void checkInit() {
-        if (initialized) {
-            throw new IllegalStateException("Container allready initialized");
-        }
-    }
-
-    /**
      * Standard Getter
      *
      * @return the application context for this container.
@@ -442,7 +458,19 @@
      * @return resource string to be parsed.
      */
     protected String getResourceString() {
-        Map<String, String> parms = context.getInitParams();
+        return getResourceString(context.getInitParams());
+    }
+
+    /**
+     * Derive the resource string from the initialization parameters.
+     * If no parameter {@link #DEFINITIONS_CONFIG} is available, attempts
+     * to retrieve {@link #LEGACY_DEFINITIONS_CONFIG}.  If niether are
+     * available, returns "/WEB-INF/tiles.xml".
+     *
+     * @param parms The initialization parameters.
+     * @return resource string to be parsed.
+     */
+    protected String getResourceString(Map<String, String> parms) {
         String resourceStr = parms.get(DEFINITIONS_CONFIG);
         if (resourceStr == null) {
             resourceStr = parms.get(LEGACY_DEFINITIONS_CONFIG);



Re: [tiles2] Re: svn commit: r488006 - in /struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles: factory/TilesContainerFactory.java impl/BasicTilesContainer.java

Posted by "David H. DeWolf" <dd...@apache.org>.
Ok, I think I understand the use case, and using a map is definitely 
cleaner than what I had done in the struts2 plugin - create a "wrapper" 
for the actual context which allowed me to inject my own defaults (see: 
http://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java)

<rattling off the top of my head>

I still think that ideally we'd only provide one of the two, but
unfortunately, we can't remove the context in lieu of the map, because 
then we won't have it to create the context factory, and the opposite 
isn't that nice either.

Also, I still wonder if what's provided in the map are really defaults, 
or if it's just a different way to provide "custom" behavior.

</rattling off the top of my head>

I don't really have any better ideas, I was just curios what your 
thoughts were. . . I'll think about it a little harder and see what I 
come up with.


David

Antonio Petrelli wrote:
> David H. DeWolf ha scritto:
>> Antonio,
>>
>> I'm not sure I understand why we need to support both the context and 
>> a map of defaults for retrieving config info for factories.  It seems 
>> redundant and thus confusing.
>>
>> If we want to use a map, shouldn't we just remove the context all 
>> together and have the client push all of the init parameters into the 
>> map prior to invocation?
> 
> I made that change (the use of "custom" defaults) because some container 
> "creators" (such as the TilesPlugin for Struts 1) need to have default 
> classes when they are not specified explicitly.
> In particular, in the TilesPlugin I am wrapping the plugin context and 
> the ServletContext as a unique ServletContext (giving precedence to the 
> plugin context init params).
> Now, if the Struts application is configured to use modules, it will use 
> the "keyed" container as a default, otherwise it will use 
> BasicTilesContainer (this is what I have in mind, there is no code 
> currently in the Struts-Tiles2 plugin).
> So I decided to use a custom "defaults" map. I thought also to provide a 
> "default" class inside the ServletContext provided by TilesPlugin, but 
> the code could become a bit messy.
> 
> This was I was thinking when I made those changes, anyway if you have a 
> better idea let me know.
> 
> Thank you
> Antonio
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
> 
> 

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


Re: [tiles2] Re: svn commit: r488006 - in /struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles: factory/TilesContainerFactory.java impl/BasicTilesContainer.java

Posted by Antonio Petrelli <ap...@apache.org>.
David H. DeWolf ha scritto:
> Antonio,
>
> I'm not sure I understand why we need to support both the context and 
> a map of defaults for retrieving config info for factories.  It seems 
> redundant and thus confusing.
>
> If we want to use a map, shouldn't we just remove the context all 
> together and have the client push all of the init parameters into the 
> map prior to invocation?

I made that change (the use of "custom" defaults) because some container 
"creators" (such as the TilesPlugin for Struts 1) need to have default 
classes when they are not specified explicitly.
In particular, in the TilesPlugin I am wrapping the plugin context and 
the ServletContext as a unique ServletContext (giving precedence to the 
plugin context init params).
Now, if the Struts application is configured to use modules, it will use 
the "keyed" container as a default, otherwise it will use 
BasicTilesContainer (this is what I have in mind, there is no code 
currently in the Struts-Tiles2 plugin).
So I decided to use a custom "defaults" map. I thought also to provide a 
"default" class inside the ServletContext provided by TilesPlugin, but 
the code could become a bit messy.

This was I was thinking when I made those changes, anyway if you have a 
better idea let me know.

Thank you
Antonio

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


[tiles2] Re: svn commit: r488006 - in /struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles: factory/TilesContainerFactory.java impl/BasicTilesContainer.java

Posted by "David H. DeWolf" <dd...@apache.org>.
Antonio,

I'm not sure I understand why we need to support both the context and a 
map of defaults for retrieving config info for factories.  It seems 
redundant and thus confusing.

If we want to use a map, shouldn't we just remove the context all 
together and have the client push all of the init parameters into the 
map prior to invocation?

Thoughts?

David

apetrelli@apache.org wrote:
> Author: apetrelli
> Date: Sun Dec 17 08:50:35 2006
> New Revision: 488006
> 
> URL: http://svn.apache.org/viewvc?view=rev&rev=488006
> Log:
> SB-101
> Refactorings to allow easier inheritance.
> 
> Modified:
>     struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java
>     struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
> 
> Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java
> URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java?view=diff&rev=488006&r1=488005&r2=488006
> ==============================================================================
> --- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java (original)
> +++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/factory/TilesContainerFactory.java Sun Dec 17 08:50:35 2006
> @@ -93,8 +93,28 @@
>       */
>      public static TilesContainerFactory getFactory(Object context)
>          throws TilesException {
> +        return getFactory(context, DEFAULTS);
> +    }
> +
> +    /**
> +     * Retrieve a factory instance as configured through the
> +     * specified context.
> +     * <p/>
> +     * The context will be queried and if a init parameter
> +     * named 'org.apache.tiles.CONTAINER_FACTORY' is discovered
> +     * this class will be instantiated and returned. Otherwise,
> +     * the factory will attempt to utilize one of it's internal
> +     * factories.
> +     *
> +     * @param context the executing applications context.
> +     *                Typically a ServletContext or PortletContext
> +     * @return a tiles container
> +     * @throws TilesException if an error occurs creating the factory.
> +     */
> +    public static TilesContainerFactory getFactory(Object context,
> +    		Map<String, String> defaults) throws TilesException {
>          return (TilesContainerFactory) TilesContainerFactory
> -            .createFactory(context, CONTAINER_FACTORY_INIT_PARAM);
> +            .createFactory(context, CONTAINER_FACTORY_INIT_PARAM, defaults);
>      }
>  
>      public TilesContainer createContainer(Object context) throws TilesException {
> @@ -108,30 +128,44 @@
>  
>      public TilesContainer createTilesContainer(Object context)
>          throws TilesException {
> +        return createTilesContainer(context, DEFAULTS);
> +    }
> +
> +    public TilesContainer createTilesContainer(Object context,
> +    		Map<String, String> defaults) throws TilesException {
>          BasicTilesContainer container = new BasicTilesContainer();
> -        initializeContainer(context, container);
> +        initializeContainer(context, container, defaults);
>          return container;
>      }
>  
>      public MutableTilesContainer createMutableTilesContainer(Object context)
>          throws TilesException {
> +    	return createMutableTilesContainer(context, DEFAULTS);
> +    }
> +
> +    public MutableTilesContainer createMutableTilesContainer(Object context,
> +    		Map<String, String> defaults) throws TilesException {
>          CachingTilesContainer container = new CachingTilesContainer();
> -        initializeContainer(context, container);
> +        initializeContainer(context, container, defaults);
>          return container;
>      }
>  
> -    public void initializeContainer(Object context,
> -                                    BasicTilesContainer container)
> +    protected void initializeContainer(Object context,
> +                                    BasicTilesContainer container,
> +                                    Map<String, String> defaults)
>          throws TilesException {
>  
>          TilesContextFactory contextFactory =
> -            (TilesContextFactory) createFactory(context, CONTEXT_FACTORY_INIT_PARAM);
> +            (TilesContextFactory) createFactory(context,
> +            		CONTEXT_FACTORY_INIT_PARAM, defaults);
>  
>          DefinitionsFactory defsFactory =
> -            (DefinitionsFactory) createFactory(context, DEFINITIONS_FACTORY_INIT_PARAM);
> +            (DefinitionsFactory) createFactory(context,
> +            		DEFINITIONS_FACTORY_INIT_PARAM, defaults);
>  
>          PreparerFactory prepFactory =
> -            (PreparerFactory) createFactory(context, PREPARER_FACTORY_INIT_PARAM);
> +            (PreparerFactory) createFactory(context,
> +            		PREPARER_FACTORY_INIT_PARAM, defaults);
>  
>          TilesApplicationContext tilesContext =
>              contextFactory.createApplicationContext(context);
> @@ -168,15 +202,21 @@
>      }
>  
>  
> -    public static Object createFactory(Object context, String initParameterName)
> +    protected static Object createFactory(Object context,
> +    		String initParameterName, Map<String, String> defaults)
>          throws TilesException {
> -        String factoryName = resolveFactoryName(context, initParameterName);
> +        String factoryName = resolveFactoryName(context, initParameterName,
> +        		defaults);
>          return ClassUtil.instantiate(factoryName);
>      }
>  
> -    public static String resolveFactoryName(Object context, String parameterName)
> +    protected static String resolveFactoryName(Object context,
> +    		String parameterName, Map<String, String> defaults)
>          throws TilesException {
>          Object factoryName = getInitParameter(context, parameterName);
> +        if (factoryName == null && defaults != null) {
> +        	factoryName = defaults.get(parameterName);
> +        }
>          return factoryName == null
>              ? DEFAULTS.get(parameterName)
>              : factoryName.toString();
> 
> 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=488006&r1=488005&r2=488006
> ==============================================================================
> --- 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 Sun Dec 17 08:50:35 2006
> @@ -101,12 +101,42 @@
>          }
>  
>          contextFactory.init(initParameters);
> -        definitionsFactory.init(initParameters);
>  
>          //Everything is now initialized.  We will populate
>          // our definitions
> -        String resourceString = getResourceString();
> +        initializeDefinitionsFactory(definitionsFactory, getResourceString(),
> +        		initParameters);
> +    }
> +
> +    /**
> +     * Determine whether or not the container has been
> +     * initialized. Utility method used for methods which
> +     * can not be invoked after the container has been
> +     * started.
> +     *
> +     * @throws IllegalStateException if the container has already been initialized.
> +     */
> +    protected void checkInit() {
> +        if (initialized) {
> +            throw new IllegalStateException("Container allready initialized");
> +        }
> +    }
> +    
> +    /**
> +     * Initializes a definitions factory.
> +     *
> +     * @param definitionsFactory The factory to initializes. 
> +     * @param resourceString The string containing a comma-separated-list of
> +     * resources.
> +     * @param initParameters A map containing the initialization parameters.
> +     * @throws TilesException If something goes wrong.
> +     */
> +    protected void initializeDefinitionsFactory(
> +    		DefinitionsFactory definitionsFactory, String resourceString,
> +    		Map<String, String> initParameters) throws TilesException {
>          List<String> resources = getResourceNames(resourceString);
> +        definitionsFactory.init(initParameters);
> +        
>          try {
>              for (String resource : resources) {
>                  URL resourceUrl = context.getResource(resource);
> @@ -129,20 +159,6 @@
>      }
>  
>      /**
> -     * Determine whether or not the container has been
> -     * initialized. Utility method used for methods which
> -     * can not be invoked after the container has been
> -     * started.
> -     *
> -     * @throws IllegalStateException if the container has already been initialized.
> -     */
> -    private void checkInit() {
> -        if (initialized) {
> -            throw new IllegalStateException("Container allready initialized");
> -        }
> -    }
> -
> -    /**
>       * Standard Getter
>       *
>       * @return the application context for this container.
> @@ -442,7 +458,19 @@
>       * @return resource string to be parsed.
>       */
>      protected String getResourceString() {
> -        Map<String, String> parms = context.getInitParams();
> +        return getResourceString(context.getInitParams());
> +    }
> +
> +    /**
> +     * Derive the resource string from the initialization parameters.
> +     * If no parameter {@link #DEFINITIONS_CONFIG} is available, attempts
> +     * to retrieve {@link #LEGACY_DEFINITIONS_CONFIG}.  If niether are
> +     * available, returns "/WEB-INF/tiles.xml".
> +     *
> +     * @param parms The initialization parameters.
> +     * @return resource string to be parsed.
> +     */
> +    protected String getResourceString(Map<String, String> parms) {
>          String resourceStr = parms.get(DEFINITIONS_CONFIG);
>          if (resourceStr == null) {
>              resourceStr = parms.get(LEGACY_DEFINITIONS_CONFIG);
> 
> 
> 

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