KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > Template


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.config;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ContentHandler;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.core.ItemType;
19 import info.magnolia.cms.security.AccessManager;
20 import info.magnolia.cms.security.Permission;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.jcr.PathNotFoundException;
30 import javax.jcr.RepositoryException;
31
32 import org.apache.log4j.Logger;
33
34
35 /**
36  * @author Sameer Charles
37  * @version 1.1
38  */

39 public class Template {
40
41     /**
42      * Logger.
43      */

44     private static Logger log = Logger.getLogger(Template.class);
45
46     private static List JavaDoc visibleTemplates = new ArrayList JavaDoc();
47
48     private static Map JavaDoc cachedContent = new Hashtable JavaDoc();
49
50     /**
51      * Template name.
52      */

53     private String JavaDoc name;
54
55     /**
56      * Template path.
57      */

58     private String JavaDoc path;
59
60     private Map JavaDoc alternativePaths;
61
62     /**
63      * Mandatatory.
64      */

65     private String JavaDoc type;
66
67     /**
68      * Mandatatory.
69      */

70     private boolean visible;
71
72     /**
73      * Optional fields.
74      */

75     private String JavaDoc description;
76
77     private String JavaDoc image;
78
79     private String JavaDoc title;
80
81     private String JavaDoc location;
82
83     /**
84      * Load all temple definitions available as a collection of Content objects.
85      */

86     public static void init() {
87         log.info("Config : initializing Template info"); //$NON-NLS-1$
88
Template.cachedContent.clear();
89         Template.visibleTemplates.clear();
90     }
91
92     public static void update(String JavaDoc modulePath) {
93         HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
94         try {
95             log.info("Config : loading Template info - " + modulePath); //$NON-NLS-1$
96
Content startPage = configHierarchyManager.getContent(modulePath);
97             Collection JavaDoc children = startPage.getContent("Templates") //$NON-NLS-1$
98
.getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE);
99
100             if ((children != null) && !(children.isEmpty())) {
101                 Iterator JavaDoc templates = children.iterator();
102                 Template.cacheContent(templates);
103             }
104             log.info("Config : Template info loaded - " + modulePath); //$NON-NLS-1$
105
}
106         catch (RepositoryException re) {
107             log.error("Config : Failed to load Template info - " + modulePath); //$NON-NLS-1$
108
log.error(re.getMessage(), re);
109         }
110     }
111
112     public static void reload() {
113         log.info("Config : re-initializing Template info"); //$NON-NLS-1$
114
Template.init();
115         update("modules/templating"); //$NON-NLS-1$
116
}
117
118     /**
119      * Get templates collection.
120      * @return Collection list containing templates as Template objects
121      */

122     public static Iterator JavaDoc getAvailableTemplates() {
123         return Template.visibleTemplates.iterator();
124     }
125
126     /**
127      * Get templates collection after access control filter applied using specified AccessManager
128      * @return Collection list containing templates as Template objects
129      */

130     public static Iterator JavaDoc getAvailableTemplates(AccessManager accessManager) {
131         List JavaDoc templateList = new ArrayList JavaDoc();
132         Iterator JavaDoc it = Template.visibleTemplates.iterator();
133         while (it.hasNext()) {
134             Template template = (Template) it.next();
135             if (accessManager.isGranted(template.getLocation(), Permission.READ)) {
136                 templateList.add(template);
137             }
138         }
139         return templateList.iterator();
140     }
141
142     /**
143      * Load content of this template info page in a hash table caching at the system load, this will save lot of time on
144      * every request while matching template info.
145      */

146     private static void cacheContent(Iterator JavaDoc templates) {
147         if (templates != null) {
148             addTemplatesToCache(templates, Template.visibleTemplates);
149         }
150     }
151
152     /**
153      * Adds templates definition to TemplatesInfo cache.
154      * @param templates iterator as read from the repository
155      * @param visibleTemplates List in with all visible templates will be added
156      */

157     private static void addTemplatesToCache(Iterator JavaDoc templates, List JavaDoc visibleTemplates) {
158         while (templates.hasNext()) {
159             Content c = (Content) templates.next();
160             try {
161                 Template ti = new Template();
162                 ti.name = c.getNodeData("name").getValue().getString(); //$NON-NLS-1$
163
ti.path = c.getNodeData("path").getValue().getString(); //$NON-NLS-1$
164
Template.addAlternativePaths(c, ti);
165                 ti.type = c.getNodeData("type").getValue().getString(); //$NON-NLS-1$
166
ti.visible = c.getNodeData("visible").getBoolean(); //$NON-NLS-1$
167
ti.title = c.getNodeData("title").getString(); //$NON-NLS-1$
168
ti.description = c.getNodeData("description").getString(); //$NON-NLS-1$
169
ti.image = c.getNodeData("image").getString(); //$NON-NLS-1$
170
Template.cachedContent.put(ti.name, ti);
171                 ti.setLocation(c.getHandle());
172                 if (ti.visible) {
173                     visibleTemplates.add(ti);
174                 }
175             }
176             catch (RepositoryException re) {
177                 log.fatal("Failed to cache TemplateInfo"); //$NON-NLS-1$
178
}
179         }
180     }
181
182     /**
183      * Add alternative extention paths to templates cache.
184      * @param node
185      * @param ti TemplateInfo
186      */

187     private static void addAlternativePaths(Content node, Template ti) {
188         try {
189             Content cl = node.getContent("SubTemplates"); //$NON-NLS-1$
190
Iterator JavaDoc it = cl.getChildren().iterator();
191             ti.alternativePaths = new Hashtable JavaDoc();
192             while (it.hasNext()) {
193                 Content c = (Content) it.next();
194                 ti.alternativePaths.put(c.getNodeData("extension").getString(), c.getNodeData("path").getString()); //$NON-NLS-1$ //$NON-NLS-2$
195
}
196         }
197         catch (PathNotFoundException e) {
198             // ignore, SubTemplates not set
199
}
200         catch (RepositoryException re) {
201             log.error("RepositoryException caught while loading alternative templates path configuration: " //$NON-NLS-1$
202
+ re.getMessage(), re);
203         }
204     }
205
206     /**
207      * Returns the cached content of the requested template. TemplateInfo properties:
208      * <ol>
209      * <li> title - title describing template</li>
210      * <li> type - jsp / servlet</li>
211      * <li> path - jsp / servlet path</li>
212      * <li> description - description of a template</li>
213      * </ol>
214      * @return TemplateInfo
215      */

216     public static Template getInfo(String JavaDoc key) {
217         return (Template) Template.cachedContent.get(key);
218     }
219
220     /**
221      *
222      */

223     public String JavaDoc getName() {
224         return this.name;
225     }
226
227     /**
228      *
229      */

230     public String JavaDoc getTitle() {
231         return this.title;
232     }
233
234     /**
235      *
236      */

237     public String JavaDoc getDescription() {
238         return this.description;
239     }
240
241     /**
242      * @return default template path
243      */

244     public String JavaDoc getPath() {
245         return this.path;
246     }
247
248     /**
249      * @param extension
250      * @return template path for the specified extension
251      */

252     public String JavaDoc getPath(String JavaDoc extension) {
253         try {
254             String JavaDoc path = (String JavaDoc) this.alternativePaths.get(extension);
255             if (path == null) {
256                 return this.getPath();
257             }
258             return path;
259         }
260         catch (Exception JavaDoc e) {
261             return this.getPath();
262         }
263     }
264
265     /**
266      *
267      */

268     public String JavaDoc getType() {
269         return this.type;
270     }
271
272     /**
273      *
274      */

275     public String JavaDoc getImage() {
276         return this.image;
277     }
278
279     /**
280      *
281      */

282     public boolean isVisible() {
283         return this.visible;
284     }
285
286     public String JavaDoc getLocation() {
287         return location;
288     }
289
290     public void setLocation(String JavaDoc location) {
291         this.location = location;
292     }
293
294 }
295
Popular Tags