You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by gv...@apache.org on 2005/09/13 08:07:43 UTC

svn commit: r280512 - in /struts/shale/trunk/clay-plugin/src: java/org/apache/shale/clay/config/ java/org/apache/shale/clay/config/beans/ test/org/apache/shale/clay/config/

Author: gvanmatre
Date: Mon Sep 12 23:07:30 2005
New Revision: 280512

URL: http://svn.apache.org/viewcvs?rev=280512&view=rev
Log:
Added HTML template caching sensitive to the last modified date.

Added:
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayConfigParser.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayTemplateParser.java
Modified:
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayXmlParser.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/Globals.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ComponentConfigBean.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBean.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBeanFactory.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigDefinitionsWatchdogFilter.java
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/TemplateConfigBean.java
    struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/ConfigTestCase.java

Added: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayConfigParser.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayConfigParser.java?rev=280512&view=auto
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayConfigParser.java (added)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayConfigParser.java Mon Sep 12 23:07:30 2005
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ *
+ * $Id$
+ */
+
+package org.apache.shale.clay.config;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.shale.clay.config.beans.ConfigBean;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>This interfaces is used by a {@link ConfigBean} instance to load
+ * a configuration file.  These files come in two flavors XML and HTML.  
+ * The {@link org.apache.shale.clay.config.beans.ComponentConfigBean} 
+ * handles materializing XML documents into a graph of 
+ * {@link org.apache.shale.clay.config.beans.ComponentBean} and the 
+ * {@link org.apache.shale.clay.config.beans.TemplateConfigBean}
+ * loads an HTML template into {@link org.apache.shale.clay.config.beans.ComponentBean} 
+ * object representations.
+ * </p>
+ */
+public interface ClayConfigParser {
+
+    /**
+     * <p>Sets an instance of the {@link ConfigBean} that pools a 
+     * collection of faces meta-component definitions.
+     * </p>
+     */
+    public void setConfig(ConfigBean config);
+
+    /**
+     * <p>Returns an instance of the {@link ConfigBean} that pools a 
+     * collection of faces meta-component definitions.
+     * </p>
+     */    
+    public ConfigBean getConfig();
+    
+    /**
+     * <p>Loads the <code>inputStream</code> identified by the <code>watchDogName</code>
+     * into the {@link ConfigBean} object pool.</p>
+     */
+    public void loadConfigFile(InputStream inputStream, String watchDogName) 
+           throws IOException, SAXException;
+}

Added: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayTemplateParser.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayTemplateParser.java?rev=280512&view=auto
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayTemplateParser.java (added)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayTemplateParser.java Mon Sep 12 23:07:30 2005
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ *
+ * $Id$
+ */
+package org.apache.shale.clay.config;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.shale.clay.config.beans.ComponentBean;
+import org.apache.shale.clay.config.beans.ComponentConfigBean;
+import org.apache.shale.clay.config.beans.ConfigBean;
+import org.apache.shale.clay.config.beans.ElementBean;
+import org.apache.shale.clay.parser.Node;
+import org.apache.shale.clay.parser.Parser;
+import org.apache.shale.clay.parser.builder.Builder;
+import org.apache.shale.util.Messages;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>This class is responsible for loading an HTML template into a graph 
+ * of {@link ComponentBean}'s that represents a JSF component tree.  It 
+ * is used by {@link org.apache.shale.clay.config.beans.TemplateConfigBean},
+ * a subclass of {@link org.apache.shale.clay.config.beans.ComponentConfigBean}.
+ * </p>
+ */
+public class ClayTemplateParser implements ClayConfigParser {
+
+    /**
+     * <p>Commons logging utility object static instance.</p>
+     */
+    private static Log log;
+    static {
+        log = LogFactory.getLog(org.apache.shale.clay.config.ClayXmlParser.class);
+    }
+
+    /**
+     * <p>Message resources for this class.</p>
+     */
+    private static Messages messages = new Messages("org.apache.shale.clay.Bundle",
+            ClayConfigureListener.class.getClassLoader());
+
+    
+    /**
+     * <p>Object pool for HTML template configuration files.</p>
+     */
+    private ConfigBean config = null;
+
+    /**
+     * <p>Sets an object pool for HTML template configuration files.</p>
+     */
+    public void setConfig(ConfigBean config) {
+        this.config = config;
+    }
+
+    /**
+     * <p>Returns an object pool for HTML template configuration files.</p>
+     */
+    public ConfigBean getConfig() {
+        return config;
+    }
+
+    /**
+     * <p>Loads the <code>inputStream</code> identified by the <code>templateName</code>
+     * into a graph of {@link ComponentBean}'s.</p> 
+     */
+    public void loadConfigFile(InputStream inputStream, String templateName) throws IOException,
+            SAXException {
+
+        ((ComponentConfigBean) config).addChild(generateElement(inputStream, templateName));
+    }
+    
+    
+    /**
+     * <p>Loads the template file and parses it into a composition
+     * of metadata that's used by the {@link org.apache.shale.clay.component.Clay} 
+     * component.  This metadata is used to construct a JSF subtree 
+     * within target view.
+     * </p> 
+     */
+    public ComponentBean generateElement(InputStream inputStream, String templateName) throws IOException {
+        
+        if (log.isInfoEnabled())
+            log.info(messages.getMessage("loading.template",  new Object[] {templateName}));
+            
+            ComponentBean root = new ComponentBean();
+            root.setJsfid(templateName);
+            root.setComponentType("javax.faces.NamingContainer");
+            
+            // generate the document
+            StringBuffer buffer = loadTemplate(inputStream, templateName);
+            
+            List roots = new Parser().parse(buffer);
+            Iterator ri = roots.iterator();
+            while (ri.hasNext()) {
+                Node node = (Node) ri.next();
+                Builder renderer = node.getBuilder();
+                ElementBean child = renderer.createElement(node);
+                
+                root.addChild(child);
+                if (renderer.isChildrenAllowed()) {
+                    renderer.encode(node, child, child);
+                } else {
+                    renderer.encode(node, child, root);
+                }
+            }
+            
+            roots = null;
+            buffer = null;
+            ri = null;
+            
+            return root;
+    }
+
+    
+    /**
+     * <p>Loads the template into a <code>StringBuffer</code> using
+     * the <code>inputStream</code> and <code>templateName<code> parameters.
+     * </p>
+     */
+    protected StringBuffer loadTemplate(InputStream inputStream, String templateName) throws IOException {
+        
+        
+        StringBuffer buff = new StringBuffer();
+        
+        try {
+            int c = 0;
+            done: while (true) {
+                c = inputStream.read();
+                if (c > -1)
+                    buff.append((char) c);
+                else
+                    break done;
+                
+            }
+        } catch (IOException e) {
+            log.error(messages.getMessage("loading.template.exception", new Object[] {templateName}), e);
+            throw e;
+        }
+        
+        return buff;
+        
+    }
+
+
+}

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayXmlParser.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayXmlParser.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayXmlParser.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/ClayXmlParser.java Mon Sep 12 23:07:30 2005
@@ -26,6 +26,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.shale.clay.config.beans.ComponentConfigBean;
+import org.apache.shale.clay.config.beans.ConfigBean;
 import org.apache.shale.util.Messages;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -42,14 +43,14 @@
  */
 
 
-public class ClayXmlParser {
+public class ClayXmlParser  implements ClayConfigParser {
     
     /**
      * <p>The {@link ComponentConfigBean} is the container holding all of the
      * component metadata definitions read for the configuration files.
      * </p>
      */
-    private ComponentConfigBean config = null;
+    private ConfigBean config = null;
     
     /**
      * <p>The <code>Digester</code> makes short work of materalizing a XML document
@@ -67,15 +68,15 @@
     }
     
     /**
-     * @return config {@link ComponentConfigBean} instance of the component metadata container
+     * @return config {@link ConfigBean} instance of the component metadata container
      */
-    public ComponentConfigBean getConfig() {
+    public ConfigBean getConfig() {
         return config;
     }
     /**
-     * @param config {@link ComponentConfigBean} instance of the component metadata container
+     * @param config {@link ConfigBean} instance of the component metadata container
      */
-    public void setConfig(ComponentConfigBean config) {
+    public void setConfig(ConfigBean config) {
         this.config = config;
     }
     
@@ -94,9 +95,9 @@
     protected Object[] registrations = {
         new String[] {"-//Apache Software Foundation//DTD Shale Clay View//EN", "/org/apache/shale/clay/config/resources/clay-config_1_0.dtd"}};
         
-        
+          
 
-    public void loadConfigFile(InputStream inputStream) throws IOException, SAXException {
+    public void loadConfigFile(InputStream inputStream, String watchDogName) throws IOException, SAXException {
         
         
         if (digester == null) {

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/Globals.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/Globals.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/Globals.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/Globals.java Mon Sep 12 23:07:30 2005
@@ -192,5 +192,12 @@
         "onselect", "readonly", "size", "style", "styleClass", "tabindex",
         "title", };
     
-          
+      
+    /**
+     * <p>The Map key used by the {@link org.apache.shale.clay.config.beans.ComponentConfigBean} to identify the 
+     * {@link org.apache.shale.clay.config.beans.ComponentConfigBean$WatchDog} 
+     * for the XML configuration files.</p> 
+     */
+    public static final String DEFAULT_COMPONENT_CONFIG_WATCHDOG = "ComponentConfigBean$WatchDog";
+    
 }

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ComponentConfigBean.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ComponentConfigBean.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ComponentConfigBean.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ComponentConfigBean.java Mon Sep 12 23:07:30 2005
@@ -36,6 +36,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.shale.clay.config.ClayConfigParser;
 import org.apache.shale.clay.config.ClayXmlParser;
 import org.apache.shale.clay.config.Globals;
 import org.apache.shale.util.Messages;
@@ -60,7 +61,7 @@
      * into a object graph cached in <code>displayElements</code>.
      * </p>
      */
-    protected ClayXmlParser parser = null;
+    protected ClayConfigParser parser = null;
     
     /**
      * <p>This parameter is initialized from the <code>init</code>
@@ -69,15 +70,15 @@
      * which will trigger reloading the files when a change has occurred.
      * </p>
      */
-    private boolean isWatchDogOn = true;
+    protected boolean isWatchDogOn = true;
     
     
     /**
-     * <p>Watches the configuration files looking for changes. The configuration
-     * files are defined by the {@link ConfigBean.ConfigDefinition} top level
+     * <p>Map of {@link WatchDog} that watches the configuration files looking for changes. 
+     * The configuration files are defined by the {@link ConfigBean.ConfigDefinition} top level
      * interface.</p>
      */
-    protected WatchDog watchDog = null; 
+    protected Map watchDogs = null; 
         
     /**
      * <p>
@@ -136,7 +137,7 @@
      * loaded from the shale-clay java archive.</p>
      */
     protected void loadConfigFiles() {
-        
+              
         parser = new ClayXmlParser();
         parser.setConfig(this);
 
@@ -152,10 +153,17 @@
             configFiles.append(", ").append(param);
 
         // pass the config bean to the parser
-        parser.setConfig((ComponentConfigBean) this);
+        parser.setConfig(this);
+        
+        // map holding the resource watchers
+        watchDogs = new TreeMap();
         
         // create the watch dog with a list of config files to look for changes
-        watchDog = new WatchDog(getConfigDefinitions(configFiles.toString()));
+        WatchDog watchDog = new WatchDog(getConfigDefinitions(configFiles.toString()), Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG);
+        
+        // adds the watcher to a map identified by name
+        watchDogs.put(watchDog.getName(), watchDog);
+        
         // loads the config files
         watchDog.refresh();
         
@@ -826,12 +834,20 @@
         context = null;
         
         if (parser != null)
-           parser.setConfig(null);
+            parser.setConfig(null);
         parser = null;
         
-        if (watchDog != null)
-           watchDog.destroy();
-        watchDog = null;
+        if (watchDogs != null) {
+            Iterator wi = watchDogs.entrySet().iterator();
+            while (wi.hasNext()) {
+                Map.Entry e = (Map.Entry) wi.next();
+                WatchDog watchDog = (WatchDog) e.getValue();
+                if (watchDog != null)
+                    watchDog.destroy();
+            }
+            watchDogs.clear();
+            watchDogs = null;
+        }
         
     }
     
@@ -926,6 +942,18 @@
     protected class WatchDog {
 
         /**
+         * <p>Name assigned to the resource watcher.</p>
+         */
+        private String name = null;
+        
+        /**
+         * <p>Returns the name of the resource watcher.</p>
+         */
+        public String getName() {
+           return name;
+        }
+                
+        /**
          * <p>Array of config file definitions.</p>
          */
         private ConfigBean.ConfigDefinition[] configDefs = null;
@@ -937,10 +965,12 @@
 
         /**
          * <p>Overloaded constructor that is passed the configuration file
-         * definitions as a parameter.</p>
+         * definitions as a parameter.  The name associated with the resource
+         * watcher is also passed as a parameter.</p>
          */
-        public WatchDog(ConfigBean.ConfigDefinition[] configDefs) {
+        public WatchDog(ConfigBean.ConfigDefinition[] configDefs, String name) {
             this.configDefs = configDefs;
+            this.name = name;
         }
 
         /**
@@ -1035,7 +1065,7 @@
                             configDefs[i].setLastModified(connections[i]
                                     .getLastModified());
                             in = connections[i].getInputStream();
-                            parser.loadConfigFile(in);
+                            parser.loadConfigFile(in, getName());
 
                         } catch (IOException e) {
                             log.error(messages.getMessage("parser.load.error",
@@ -1067,9 +1097,13 @@
     /**
      * <p>This method should be called from key points in the application to invoke 
      * automatic reloading of the configuration files if they have been modified since 
-     * last reloaded.</p>
+     * last reloaded.  The parameter <code>watchDogName</code> corresponds to a
+     * HTML template name or the default defined by the static String constant 
+     * <code>Global.DEFAULT_COMPONENT_CONFIG_WATCHDOG</code></p>
      */
-    public void refresh() {
+    public void refresh(String watchDogName) {
+        
+        WatchDog watchDog = (WatchDog) watchDogs.get(watchDogName);
         
         // is auto watch turned off
         if (!ComponentConfigBean.this.isWatchDogOn || watchDog == null)
@@ -1078,6 +1112,8 @@
         synchronized (displayElements) {
             watchDog.refresh();
         }
+        
+        watchDog = null;
     }
 
     

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBean.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBean.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBean.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBean.java Mon Sep 12 23:07:30 2005
@@ -102,8 +102,10 @@
     /**
      * <p>This method should be called from key points in the application to invoke 
      * automatic reloading of the configuration files if they have been modified since 
-     * last reloaded.</p>
+     * last reloaded.  The <code>watchDogName</code> parameter corresponds to the 
+     * instance of the {@link org.apache.shale.clay.config.beans.ComponentConfigBean$WatchDog} 
+     * monitoring a configuration file for change.</p>
      */
-    public void refresh();
+    public void refresh(String watchDogName);
  
 }

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBeanFactory.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBeanFactory.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBeanFactory.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigBeanFactory.java Mon Sep 12 23:07:30 2005
@@ -84,14 +84,20 @@
     /**
      * <p>This method should be called from key points in the application to invoke 
      * automatic reloading of the configuration files if they have been modified since 
-     * last reloaded.</p>
+     * last reloaded.  The <code>watchDogName</code> parameter corresponds to the instance of the 
+     * {@link org.apache.shale.clay.config.beans.ComponentConfigBean.WatchDog} monitoring 
+     * a configuration file for change. The same {@link ConfigBean} interfaces is used for 
+     * {@link ComponentConfigBean} and {@link TemplateConfigBean} but the 
+     * {@link org.apache.shale.clay.config.beans.ComponentConfigBean$WatchDog} for the XML 
+     * configuration files is assumed to be known by <code>Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG</code>. 
+     * </p>
      */
-    public static void refresh() {
+    public static void refresh(String watchDogName) {
         Iterator ci = configBeans.iterator();
         ConfigBean config = null;
         while (ci.hasNext()) {
             config = (ConfigBean) ci.next();
-            config.refresh();
+            config.refresh(watchDogName);
         }
     }
 

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigDefinitionsWatchdogFilter.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigDefinitionsWatchdogFilter.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigDefinitionsWatchdogFilter.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/ConfigDefinitionsWatchdogFilter.java Mon Sep 12 23:07:30 2005
@@ -19,6 +19,7 @@
 package org.apache.shale.clay.config.beans;
 
 import org.apache.shale.application.AbstractRegExpFilter;
+import org.apache.shale.clay.config.Globals;
 import org.apache.shale.faces.ShaleWebContext;
 
 public class ConfigDefinitionsWatchdogFilter extends AbstractRegExpFilter {
@@ -30,7 +31,7 @@
      * @param context <code>Context</code> for the current request
      */
     protected String value(ShaleWebContext context) {
-
+        
         String servletPath = context.getRequest().getServletPath();
         if (servletPath == null) {
             servletPath = "";
@@ -54,12 +55,17 @@
     /**
      * <p>Trigger reloading of the {@link org.apache.shale.clay.component.Clay}'s
      * xml configuration files if the <code>auto-reload-clay-files</code> init 
-     * parameter is set to <code>true</code> in the web.xml.</p>
+     * parameter is set to <code>true</code> in the web.xml.  The HTML templates
+     * are re-cashed on-demand due to their atomicity.  The XML configuration 
+     * files are shared by all {@link org.apache.shale.clay.component.Clay} 
+     * view composition mechanisms so all files must be reloaded if a change 
+     * is made.</p>
      * 
      * @param context <code>Context</code> for the current request
      */
     protected void accept(ShaleWebContext context) throws Exception {
-        ConfigBeanFactory.refresh();
+           
+        ConfigBeanFactory.refresh(Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG);
     }
     
     

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/TemplateConfigBean.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/TemplateConfigBean.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/TemplateConfigBean.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/config/beans/TemplateConfigBean.java Mon Sep 12 23:07:30 2005
@@ -18,19 +18,13 @@
 
 package org.apache.shale.clay.config.beans;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-import java.util.List;
+import java.util.TreeMap;
 
 import javax.servlet.ServletContext;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.shale.clay.config.Globals;
-import org.apache.shale.clay.parser.Node;
-import org.apache.shale.clay.parser.Parser;
-import org.apache.shale.clay.parser.builder.Builder;
+import org.apache.shale.clay.config.ClayTemplateParser;
 
 /**
  * <p>The second type of top-level object pool.  This implementation
@@ -58,81 +52,30 @@
      */
     public ComponentBean getElement(String templateName) {
         
-        // no caching needed for dynamically generated elements
-        ComponentBean root = generateElement(templateName);
-        
-        return root;
-    }
-    
-    /**
-     * <p>Loads the template file and parses it into a composition
-     * of metadata that the {@link org.apache.shale.clay.component.Clay} 
-     * component will use to construct a JSF subtree within the JSP
-     * page.
-     * </p> 
-     */
-    public ComponentBean generateElement(String templateName) {
-        
-        if (log.isInfoEnabled())
-            log.info(messages.getMessage("loading.template",  new Object[] {templateName}));
-            
-            ComponentBean root = new ComponentBean();
-            root.setJsfid(templateName);
-            root.setComponentType("javax.faces.NamingContainer");
+        // look for a watcher identified by the template name
+        WatchDog watchDog = (WatchDog) watchDogs.get(templateName);
+        if (watchDog == null) {
+           //The first time the page is created, create a watcher 
             
-            // generate the document
-            StringBuffer buffer = loadTemplate(templateName);
-            
-            List roots = new Parser().parse(buffer);
-            Iterator ri = roots.iterator();
-            while (ri.hasNext()) {
-                Node node = (Node) ri.next();
-                Builder renderer = node.getBuilder();
-                ElementBean child = renderer.createElement(node);
-                
-                root.addChild(child);
-                if (renderer.isChildrenAllowed()) {
-                    renderer.encode(node, child, child);
-                } else {
-                    renderer.encode(node, child, root);
-                }
-            }
-            
-            roots = null;
-            buffer = null;
-            ri = null;
-            
-            return root;
-    }
-    /**
-     * <p>Loads the template into a <code>StringBuffer</code> using
-     * the <code>ServletContext</code>.
-     * </p>
-     */
-    public StringBuffer loadTemplate(String templateName) {
-        
-        StringBuffer buff = new StringBuffer();
-        InputStream in = null;
-        
-        try {
-            in = getServletContext().getResourceAsStream(templateName);
-            int c = 0;
-            done: while (true) {
-                c = in.read();
-                if (c > -1)
-                    buff.append((char) c);
-                else
-                    break done;
-                
-            }
-        } catch (IOException e) {
-            log.error(messages.getMessage("loading.template.exception", new Object[] {templateName}), e);
+           StringBuffer uri = new StringBuffer(templateName);
+           if (uri.length() > 0 &&  uri.charAt(0) != '/') 
+             uri.insert(0, '/');
+           
+           watchDog = new WatchDog(getConfigDefinitions(uri.toString()), templateName);
+           
+           // register by name
+           watchDogs.put(watchDog.getName(), watchDog);
         }
         
-        return buff;
+        //loads the HTML template the first time and when file 
+        //has been modified
+        watchDog.refresh();
         
+        // returns the cached element
+        return super.getElement(templateName);
     }
     
+    
     /**
      * <p>Returns an integer value use to order the registered {@link ConfigBean} instances 
      * with the {@link ConfigBeanFactory}.
@@ -154,11 +97,29 @@
     }
 
     /**
-     * <p>Override this method to suppress loading configuration files.  This
-     * {@link ConfigBean} doesn't need the cached graph of components.  It's 
-     * purpose is to load {@link ElementBean}'s from a HTML document.
-     * </p>
+     * <p>This is an overridden method called from the init method.
+     * It loads an instance of the {@link ClayTemplateParser} and 
+     * establishes a Map collection to hold the resource 
+     * {@link org.apache.shale.clay.config.beans.ComponentConfigBean$WatchDog}'s.</p>
+     */
+    protected void loadConfigFiles() {
+        parser = new ClayTemplateParser(); 
+        parser.setConfig(this);
+        
+        watchDogs = new TreeMap();
+    }
+
+
+    /**
+     * The HTML templates are loaded on-demand.  Override this method forcing the auto 
+     * load option on.  The XML configuration files are only effected by the 
+     * <code>auto-reload-clay-files</code> initialization parameter.
      */
-    protected void loadConfigFiles() {}
+    public void init(ServletContext context) {
+        super.init(context);
+        
+        
+        isWatchDogOn = true;
+    }
     
 }

Modified: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/ConfigTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/ConfigTestCase.java?rev=280512&r1=280511&r2=280512&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/ConfigTestCase.java (original)
+++ struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/ConfigTestCase.java Mon Sep 12 23:07:30 2005
@@ -16,6 +16,8 @@
 package org.apache.shale.clay.config;
 
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Iterator;
 
 import junit.framework.Test;
@@ -122,7 +124,6 @@
 
         // create an anonymous extension
         servletContext = new MockServletContext() {
-            private String contextRoot = "org/apache/shale/clay/config/";
             public InputStream getResourceAsStream(String path) {
 
                 // Return a corresponding class loader resource
@@ -131,9 +132,27 @@
                 if (classloader == null)
                     classloader = this.getClass().getClassLoader();
 
-                return classloader.getResourceAsStream(contextRoot + path);
+                return classloader.getResourceAsStream(path);
 
             };
+            
+            public URL getResource(String path) throws MalformedURLException {
+
+                StringBuffer tmp = new StringBuffer(path);
+                if (tmp.charAt(0) == '/')
+                   tmp.delete(0, 1);
+                
+                // Return a corresponding class loader resource
+                ClassLoader classloader = Thread.currentThread()
+                        .getContextClassLoader();
+                if (classloader == null)
+                    classloader = this.getClass().getClassLoader();
+                
+                URL url = classloader.getResource(tmp.toString());
+                return url;
+
+            }
+
         };
         
         // sets the default html template suffix ".html"
@@ -169,12 +188,13 @@
         
         servletContext.addInitParameter(Globals.CLAY_CONFIG_FILES, configFiles);        
         standardConfigBean.init(servletContext);
+        templateConfigBean.init(servletContext);
 
     }
     public void testLoadConfigFile() {
 
         // loads the default config from the META-INF jar folder
-        loadConfigFile(Globals.DEFAULT_CLAY_CONFIG_FILE);
+        loadConfigFile("");
         
         //verify the components where loaded
         for (int i = 0; i < COMPONENTS.length; i++) {
@@ -354,7 +374,7 @@
     public void testCustomConfigFile() {
 
         //loads the default and the custom address config files
-        loadConfigFile(Globals.DEFAULT_CLAY_CONFIG_FILE + ", address-config.xml");
+        loadConfigFile("org/apache/shale/clay/config/address-config.xml");
        
         ComponentBean bean = standardConfigBean.getElement("agentAddressForm");
         assertNotNull(bean);
@@ -374,9 +394,9 @@
     public void testLoadHTMLFile() {
 
         //loads the default and the custom address config file
-        loadConfigFile(Globals.DEFAULT_CLAY_CONFIG_FILE + ", address-config.xml");
+        loadConfigFile("org/apache/shale/clay/config/address-config.xml");
 
-        ComponentBean bean = templateConfigBean.getElement("address.html"); 
+        ComponentBean bean = templateConfigBean.getElement("org/apache/shale/clay/config/address.html"); 
         assertNotNull(bean);
 
         Iterator ci = bean.getChildrenIterator();
@@ -391,8 +411,7 @@
     // checks a meta components realized state against the assumed values
     protected void checkComponent(ComponentBean bean, Object[] knownGoodStates) {
        int indx = indexOf(bean.getJsfid(), knownGoodStates);
-       System.out.println(bean);
-       
+             
        int converterCnt = -1;
        int childrenCnt = -1;
        int validatorCnt = -1;
@@ -422,7 +441,7 @@
 
                 AttributeBean attr = bean.getAttribute(valuepair[0]);
                 assertNotNull("check.attribute", attr);
-                System.out.println("\t" + attr.toString());
+                //System.out.println("\t" + attr.toString());
                 assertEquals("check.attribute.value", attr.getValue(), valuepair[1]);
             }
             



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