KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > module > AbstractModule


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.module;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.util.ClasspathResourcesUtil;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.jdom.JDOMException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 /**
29  * Default implementation. Imports bootstrap files, registers servlets, registers repositories and extracts files from
30  * the jar. For a more sophisticated version see the AbstractAdminModule from the admin interface sub-project.
31  * @author Philipp Bracher
32  * @version $Revision: 7365 $ ($Author: gjoseph $)
33  */

34 public abstract class AbstractModule implements Module {
35
36     /**
37      * The modules definition built by the modules xml file
38      */

39     protected ModuleDefinition definition;
40
41     /**
42      * The node of this module
43      */

44     protected Content moduleNode;
45
46     /**
47      * True after a registration.
48      */

49     private boolean restartNeeded = false;
50
51     /**
52      * True after initialization.
53      */

54     private boolean initialized = false;
55
56     /**
57      * Logger.
58      */

59     private static Logger log = LoggerFactory.getLogger(AbstractModule.class);
60
61     /**
62      * Calles onRegister if not yet installed after it loaded the bootstrapfiles of this module
63      */

64     public final void register(ModuleDefinition def, Content moduleNode, int registerState) throws RegisterException {
65         this.setDefinition(def);
66         this.setModuleNode(moduleNode);
67
68         if (registerState == REGISTER_STATE_INSTALLATION || registerState == REGISTER_STATE_NEW_VERSION) {
69             try {
70                 
71                 final String JavaDoc moduleName = this.getName();
72
73                 registerServlets(def);
74
75                 registerRepositories(def);
76
77                 bootstrap(moduleName);
78                 
79                 installFiles(moduleName, def.getModuleRoot());
80
81                 // let the module do it's stuff
82
onRegister(registerState);
83             }
84             catch (Exception JavaDoc e) {
85                 throw new RegisterException("can't register module " + this.definition.getName(), e);
86             }
87         }
88     }
89
90     /**
91      * @see info.magnolia.cms.module.Module#unregister(info.magnolia.cms.module.ModuleDefinition,
92      * info.magnolia.cms.core.Content)
93      */

94     public void unregister(ModuleDefinition def, Content moduleNode) {
95         // TODO implement the unrigister
96
onUnRegister();
97         // now delete the jar if existing
98
// set the restart needed
99
}
100
101     /**
102      * Template pattern. Implement to performe some module specific stuff
103      * @param registerState
104      */

105     protected void onRegister(int registerState) throws RegisterException {
106
107     }
108
109     /**
110      * Template pattern. Implement to perform some module specific stuff.
111      */

112     protected void onUnRegister() {
113     }
114
115     /**
116      * @return Returns the restartNeeded.
117      */

118     public boolean isRestartNeeded() {
119         return this.restartNeeded;
120     }
121
122     /**
123      * @param restartNeeded The restartNeeded to set.
124      */

125     protected void setRestartNeeded(boolean restartNeeded) {
126         this.restartNeeded = restartNeeded;
127     }
128
129     /**
130      * @return Returns the definition.
131      */

132     public ModuleDefinition getModuleDefinition() {
133         return this.definition;
134     }
135
136     /**
137      * @param definition The definition to set.
138      */

139     protected void setDefinition(ModuleDefinition definition) {
140         this.definition = definition;
141     }
142
143     /**
144      * @return Returns the initialized.
145      */

146     public boolean isInitialized() {
147         return this.initialized;
148     }
149
150     /**
151      * @param initialized The initialized to set.
152      */

153     protected void setInitialized(boolean initialized) {
154         this.initialized = initialized;
155     }
156
157     /**
158      * Delegate to the modules definition getName() method
159      * @return
160      */

161     public String JavaDoc getName() {
162         if (definition != null) {
163             return definition.getName();
164         }
165         return null;
166     }
167
168     /**
169      * @return Returns the moduleNode.
170      */

171     public Content getModuleNode() {
172         return this.moduleNode;
173     }
174
175     /**
176      * @param moduleNode The moduleNode to set.
177      */

178     protected void setModuleNode(Content moduleNode) {
179         this.moduleNode = moduleNode;
180     }
181
182     /**
183      * @see info.magnolia.cms.module.Module#destroy()
184      */

185     public void destroy() {
186
187     }
188
189     /**
190      * Copy the files from /mgnl-files/* to the filesystem.
191      * @param moduleName
192      * @param moduleRoot module root dir or jar file.
193      * @throws Exception
194      */

195     protected void installFiles(final String JavaDoc moduleName, File JavaDoc moduleRoot) throws Exception JavaDoc {
196         // copy the content of mgnl-files to the webapp
197
String JavaDoc[] moduleFiles = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() {
198
199             public boolean accept(String JavaDoc name) {
200                 return name.startsWith("/mgnl-files/") && StringUtils.contains(name, "/" + moduleName + "/");
201             }
202         });
203
204         log.info("installing files for module {}", moduleName);
205
206         ModuleUtil.installFiles(moduleFiles, "/mgnl-files/");
207     }
208
209     /**
210      * Register the repositories defined in the descriptor.
211      * @param def
212      * @throws RegisterException
213      */

214     protected void registerRepositories(ModuleDefinition def) throws RegisterException {
215         boolean restartNeeded = false;
216
217         // register repositories
218
for (Iterator JavaDoc iter = def.getRepositories().iterator(); iter.hasNext();) {
219             RepositoryDefinition repDef = (RepositoryDefinition) iter.next();
220             String JavaDoc repository = repDef.getName();
221
222             String JavaDoc nodetypeFile = repDef.getNodeTypeFile();
223             boolean repositoryAdded = ModuleUtil.registerRepository(repository, nodetypeFile);
224             if (repositoryAdded) {
225                 restartNeeded = true;
226             }
227
228             for (Iterator JavaDoc iterator = repDef.getWorkspaces().iterator(); iterator.hasNext();) {
229                 String JavaDoc workspace = (String JavaDoc) iterator.next();
230
231                 if (ModuleUtil.registerWorkspace(repository, workspace)) {
232                     restartNeeded = true;
233                 }
234             }
235         }
236
237         if (restartNeeded) {
238             this.setRestartNeeded(true);
239         }
240     }
241
242     /**
243      * Register the servlets defined in the descriptor.
244      * @param def
245      * @throws JDOMException
246      * @throws IOException
247      */

248     protected void registerServlets(ModuleDefinition def) throws JDOMException, IOException JavaDoc {
249         boolean restartNeeded = false;
250
251         // register servlets
252
for (Iterator JavaDoc iter = def.getServlets().iterator(); iter.hasNext();) {
253             ServletDefinition servlet = (ServletDefinition) iter.next();
254             restartNeeded = restartNeeded | ModuleUtil.registerServlet(servlet);
255         }
256
257         if (restartNeeded) {
258             this.setRestartNeeded(true);
259         }
260     }
261     
262     
263
264     /**
265      * Bootsrap the files in mgnl-bootsrap/modulename directory
266      * @param moduleName
267      * @throws IOException
268      * @throws RegisterException
269      */

270     protected void bootstrap(final String JavaDoc moduleName) throws IOException JavaDoc, RegisterException {
271         // bootstrap the module files
272
String JavaDoc[] moduleBootstrap = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() {
273
274             public boolean accept(String JavaDoc name) {
275                 return name.startsWith("/mgnl-bootstrap/" + moduleName) && name.endsWith(".xml");
276             }
277         });
278
279         ModuleUtil.bootstrap(moduleBootstrap);
280     }
281
282     /**
283      * @see java.lang.Object#toString()
284      */

285     public String JavaDoc toString() {
286         return getClass().getName() + " (" + getName() + " module)";
287     }
288
289 }
290
Popular Tags