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