2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: TemplateComponentConfigBean.java 511456 2007-02-25 06:21:04Z gvanmatre $
20   */
21  package org.apache.shale.clay.config.beans;
22  
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import org.apache.shale.clay.config.ClayXmlParser;
29  import org.apache.shale.clay.config.Globals;
30  
31  /***
32   * <p>This ConfigBean is responsible for handling full XML views.  For full XML views,
33   * the viewId will correspond to an XML file that defines meta-component declarations
34   * specific for the page.  It's assumed that here will be a top-level component with a
35   * jsfid matching the viewId.</p>
36   */
37  public class TemplateComponentConfigBean extends TemplateConfigBean {
38  
39      /***
40       * <p>Returns an integer value use to order the registered {@link ConfigBean} instances
41       * with the {@link ConfigBeanFactory}.
42       * </p>
43       *
44       * @return a weight of 2
45       */
46      public int getWeight() {
47          return 2;
48      }
49  
50  
51      /***
52       * <p>This is an overridden method called from the init method.
53       * It loads an instance of the {@link ClayXmlParser} and
54       * establishes a Map collection to hold the resource
55       * {@link org.apache.shale.clay.config.beans.ComponentConfigBean$WatchDog}'s.</p>
56       */
57      protected void loadConfigFiles() {
58  
59          parser = new ClayXmlParser();
60          parser.setConfig(this);
61  
62          // a comma delimited value list of config files
63          String param = context.getInitParameter(Globals.CLAY_FULLXML_CONFIG_FILES);
64  
65          // pass the config bean to the parser
66          parser.setConfig(this);
67  
68          // map holding the resource watchers
69          watchDogs = Collections.synchronizedMap(new TreeMap());
70  
71  
72          if (param != null && param.length() > 0) {
73              // create the watch dog with a list of config files to look for changes
74              WatchDog watchDog = new WatchDog(getConfigDefinitions(param),
75                      Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG);
76  
77              // adds the watcher to a map identified by name
78              watchDogs.put(watchDog.getName(), watchDog);
79  
80              // loads the config files
81              watchDog.refresh(true);
82  
83          }
84  
85  
86          param = null;
87      }
88  
89  
90      /***
91       * <p>If the <code>forceReload</code> is <code>true</code>,
92       * the <code>displayElements</code> cache is invalidated.
93       * A <code>true</code> value is returned if cache has
94       * been cleared.
95       * <br/><br/>
96       * All files loaded on-demand are purged.  Full view definitions
97       * loaded using the <code>Globals.CLAY_FULLXML_CONFIG_FILES</code>
98       * initialization parameter are reloaded if modified or if a
99       * <code>forceReload</code> is specified.</p>
100      *
101      * @param forceReload <code>true</code> if the group of associated resources should be reloaded
102      * @return <code>true</code> if one of the resources changed
103      */
104     public boolean refresh(boolean forceReload) {
105         boolean wasDirty = forceReload;
106 
107         //look for a default watch of full view config files
108         WatchDog defaultWatchDog = (WatchDog) watchDogs.get(Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG);
109 
110         //clear out all the on-demand beans
111         if (forceReload) {
112 
113             //remove from the list without the clean-up
114             if (defaultWatchDog != null) {
115                watchDogs.remove(Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG);
116             }
117 
118             // remove all old templates
119             Iterator wi = watchDogs.entrySet().iterator();
120             while (wi.hasNext()) {
121                 Map.Entry e = (Map.Entry) wi.next();
122                 WatchDog watchDog = (WatchDog) e.getValue();
123                 clear(watchDog.getName());
124                 if (watchDog != null) {
125                     watchDog.destroy();
126                 }
127             }
128             watchDogs.clear();
129 
130             //push back into the list
131             if (defaultWatchDog != null) {
132                watchDogs.put(Globals.DEFAULT_COMPONENT_CONFIG_WATCHDOG, defaultWatchDog);
133             }
134 
135         }
136 
137         //check for dirty cache
138         if (defaultWatchDog != null) {
139             wasDirty = defaultWatchDog.refresh(forceReload);
140         }
141 
142         return wasDirty;
143     }
144 
145 
146     /***
147      * <p>Overrides the super call to change the condition of the filter.  This
148      * {@link ConfigBean} can create components where the id end in the suffix
149      * defined in the web deployment descriptor as a initialization parameter with
150      * the name defined by <code>Globals.CLAY_XML_TEMPLATE_SUFFIX</code>  Or, using
151      * the default defined by <code>Globals.CLAY_DEFAULT_XML_TEMPLATE_SUFFIX</code>
152      *
153      * @param id jsfid
154      * @return <code>true</code> if the jsfid can be handle
155      */
156     public boolean validMoniker(String id) {
157         return id.endsWith(suffixes[1]);
158     }
159 
160 
161 }