KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > templating > Engine


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.module.templating;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.beans.config.Paragraph;
17 import info.magnolia.cms.beans.config.Template;
18 import info.magnolia.cms.core.Content;
19 import info.magnolia.cms.core.HierarchyManager;
20 import info.magnolia.cms.core.ItemType;
21 import info.magnolia.cms.module.Module;
22 import info.magnolia.cms.module.ModuleConfig;
23 import info.magnolia.cms.module.RegisterException;
24
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.jar.JarFile JavaDoc;
28
29 import javax.jcr.RepositoryException;
30 import javax.jcr.observation.Event;
31 import javax.jcr.observation.EventIterator;
32 import javax.jcr.observation.EventListener;
33 import javax.jcr.observation.ObservationManager;
34
35 import org.apache.log4j.Logger;
36
37
38 /**
39  * Module "templating" main class.
40  * @author Sameer Charles
41  * @author Fabrizio Giustina
42  * @version 2.0
43  */

44 public class Engine implements Module {
45
46     /**
47      * Logger.
48      */

49     private static Logger log = Logger.getLogger(Engine.class);
50
51     /**
52      * base path jcr property.
53      */

54     private static final String JavaDoc ATTRIBUTE_BASE_PATH = "basePath"; //$NON-NLS-1$
55

56     /**
57      * Module name.
58      */

59     protected String JavaDoc moduleName;
60
61     /**
62      * Base path in configuration.
63      */

64     protected String JavaDoc basePath;
65
66     /*
67      * (non-Javadoc)
68      * @see info.magnolia.cms.module.Module#register(java.lang.String, java.lang.String, info.magnolia.cms.core.Content,
69      * java.util.jar.JarFile, int)
70      */

71     public void register(String JavaDoc moduleName, String JavaDoc version, Content moduleNode, JarFile JavaDoc jar, int registerState)
72         throws RegisterException {
73         // nothing to do
74
}
75
76     /**
77      * @see info.magnolia.cms.module.Module#init(info.magnolia.cms.module.ModuleConfig)
78      */

79     public void init(ModuleConfig config) {
80         this.moduleName = config.getModuleName();
81         this.basePath = (String JavaDoc) config.getInitParameters().get(ATTRIBUTE_BASE_PATH);
82
83         // set local store to be accessed via admin interface classes or JSP
84

85         Store.getInstance().setStore(config.getLocalStore());
86
87         log.info("Module: " + this.moduleName); //$NON-NLS-1$
88
log.info(this.moduleName + ": updating Template list"); //$NON-NLS-1$
89
Template.update(this.basePath);
90         log.info(this.moduleName + ": updating Paragraph list"); //$NON-NLS-1$
91
registerParagraphs();
92
93         registerEventListeners();
94     }
95
96     /**
97      * @see info.magnolia.cms.module.Module#destroy()
98      */

99     public void destroy() {
100         // do nothing
101
// @todo remove event listeners?
102
}
103
104     /**
105      * Add jcr event listeners for automatic reloading of templates and paragraphs when content changes.
106      */

107     private void registerEventListeners() {
108
109         // automatically reload paragraphs
110
registerEventListeners("/" + this.basePath + "/Paragraphs", new EventListener() { //$NON-NLS-1$ //$NON-NLS-2$
111

112                 public void onEvent(EventIterator iterator) {
113                     // reload everything, should we handle single-paragraph reloading?
114
registerParagraphs();
115                 }
116             });
117
118         // automatically reload templates
119
registerEventListeners("/" + this.basePath + "/Templates", new EventListener() { //$NON-NLS-1$ //$NON-NLS-2$
120

121                 public void onEvent(EventIterator iterator) {
122                     // reload everything, should we handle single-template reloading?
123
Template.reload();
124                 }
125             });
126     }
127
128     /**
129      * Register a single event listener, bound to the given path.
130      * @param observationPath repository path
131      * @param listener event listener
132      */

133     private void registerEventListeners(String JavaDoc observationPath, EventListener listener) {
134
135         log.info("Registering event listener for path [" + observationPath + "]"); //$NON-NLS-1$ //$NON-NLS-2$
136

137         try {
138
139             ObservationManager observationManager = ContentRepository
140                 .getHierarchyManager(ContentRepository.CONFIG)
141                 .getWorkspace()
142                 .getObservationManager();
143
144             observationManager.addEventListener(listener, Event.NODE_ADDED
145                 | Event.PROPERTY_ADDED
146                 | Event.PROPERTY_CHANGED, observationPath, true, null, null, false);
147         }
148         catch (RepositoryException e) {
149             log.error("Unable to add event listeners for " + observationPath, e); //$NON-NLS-1$
150
}
151
152     }
153
154     /**
155      * Load all paragraph definitions available as a collection of Content objects.
156      */

157     protected void registerParagraphs() {
158         // simply overwrite (it's a map, clear is not needed)
159
// Paragraph.cachedContent.clear();
160

161         log.info(this.moduleName + ": initializing Paragraph info"); //$NON-NLS-1$
162
HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
163         try {
164             log.info(this.moduleName + ": loading Paragraph info - " + this.basePath); //$NON-NLS-1$
165
Content startPage = configHierarchyManager.getContent(this.basePath);
166             Content paragraphDefinition = startPage.getContent("Paragraphs"); //$NON-NLS-1$
167

168             cacheParagraphsContent(paragraphDefinition);
169             log.info(this.moduleName + ": Paragraph info loaded - " + this.basePath); //$NON-NLS-1$
170
}
171         catch (RepositoryException re) {
172             log.error(this.moduleName + ": Failed to load Paragraph info - " + this.basePath); //$NON-NLS-1$
173
log.error(re.getMessage(), re);
174         }
175     }
176
177     /**
178      * Adds paragraph definition to ParagraphInfo cache.
179      * @param paragraphs iterator as read from the repository
180      */

181     private void addParagraphsToCache(Iterator JavaDoc paragraphs) {
182         while (paragraphs.hasNext()) {
183             Content c = (Content) paragraphs.next();
184             Paragraph pi = Paragraph.addParagraphToCache(c, this.basePath);
185
186             // @todo inter-module dependency! should this be removed? how to handle this situation?
187
if (pi.getDialogContent() != null) {
188                 info.magnolia.module.admininterface.Store.getInstance().registerParagraphDialogHandler(
189                     pi.getName(),
190                     pi.getDialogContent());
191             }
192         }
193     }
194
195     /**
196      * Load content of this paragraph info page in a hash table caching at the system load, this will save lot of time
197      * on every request while matching paragraph info.
198      * @param content paragraph node
199      */

200     private void cacheParagraphsContent(Content content) {
201         Collection JavaDoc contentNodes = content.getChildren(ItemType.CONTENTNODE);
202         Iterator JavaDoc definitions = contentNodes.iterator();
203         addParagraphsToCache(definitions);
204         Collection JavaDoc subDefinitions = content.getChildren(ItemType.CONTENT);
205         Iterator JavaDoc it = subDefinitions.iterator();
206         while (it.hasNext()) {
207             Content c = (Content) it.next();
208             cacheParagraphsContent(c);
209         }
210     }
211
212 }
Popular Tags