KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 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.HierarchyManager;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.module.Module;
19 import info.magnolia.cms.module.ModuleDefinition;
20 import info.magnolia.cms.security.AccessDeniedException;
21 import info.magnolia.cms.util.ClassUtil;
22 import info.magnolia.cms.util.ContentUtil;
23 import info.magnolia.cms.util.FactoryUtil;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.jcr.PathNotFoundException;
29 import javax.jcr.RepositoryException;
30
31 import org.apache.commons.collections.OrderedMap;
32 import org.apache.commons.collections.OrderedMapIterator;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /**
38  * Initialise all configured modules.
39  */

40 public class ModuleLoader {
41
42     /**
43      * Logger.
44      */

45     protected static Logger log = LoggerFactory.getLogger(ModuleLoader.class);
46
47     /**
48      * magnolia module specific keywords
49      */

50     public static final String JavaDoc MODULES_NODE = "modules"; //$NON-NLS-1$
51

52     public static final String JavaDoc CONFIG_NODE_VIRTUAL_MAPPING = "virtualURIMapping"; //$NON-NLS-1$
53

54     public static final String JavaDoc CONFIG_NODE = "config"; //$NON-NLS-1$
55

56     /**
57      * The module instances
58      */

59     private Map JavaDoc modules = new HashMap JavaDoc();
60
61     /**
62      * Don't instantiate.
63      */

64     public ModuleLoader() {
65     }
66
67     /**
68      * @return Returns the instance.
69      */

70     public static ModuleLoader getInstance() {
71         return (ModuleLoader) FactoryUtil.getSingleton(ModuleLoader.class);
72     }
73
74     /**
75      * Init the modules.
76      * @throws ConfigurationException
77      */

78     protected void init() throws ConfigurationException {
79         log.info("Loading modules"); //$NON-NLS-1$
80
try {
81             Content modulesNode = getModulesNode();
82             init(modulesNode);
83             log.info("Finished loading modules"); //$NON-NLS-1$
84
}
85         catch (Exception JavaDoc e) {
86             log.error("Failed to initialize module loader"); //$NON-NLS-1$
87
log.error(e.getMessage(), e);
88             throw new ConfigurationException(e.getMessage());
89         }
90     }
91
92     /**
93      * Init the modules
94      * @param modulesNode node with the module nodes
95      */

96     private void init(Content modulesNode) {
97         // loop over the definitions (following the dependencies)
98
OrderedMap defs = ModuleRegistration.getInstance().getModuleDefinitions();
99         for (OrderedMapIterator iter = defs.orderedMapIterator(); iter.hasNext();) {
100             iter.next();
101             ModuleDefinition def = (ModuleDefinition) iter.getValue();
102             try {
103                 if (modulesNode.hasContent(def.getName())) {
104                     Content moduleNode = modulesNode.getContent(def.getName());
105                     load(def, moduleNode);
106                 }
107                 else {
108                     log.error("can't initialize module [{}]: no module node in the config repository found", def
109                         .getName());
110                 }
111
112             }
113             catch (RepositoryException e) {
114                 log.error("can't initialize module [" + def.getName() + "]", e);
115             }
116         }
117
118         if (ModuleRegistration.getInstance().isRestartNeeded()) {
119             log.warn("stopped module initialization since a restart is needed");
120         }
121     }
122
123     private void load(ModuleDefinition def, Content moduleNode) {
124         try {
125             Module module = this.getModuleInstance(def.getName());
126
127             // instantiate if not yet done (due registraion)
128
if (module == null) {
129                 try {
130                     String JavaDoc moduleClassName = moduleNode.getNodeData("class").getString(); //$NON-NLS-1$
131

132                     module = (Module) ClassUtil.newInstance(moduleClassName);
133                     this.addModuleInstance(def.getName(), module);
134                 }
135                 catch (InstantiationException JavaDoc ie) {
136                     log.error("Module {} failed to load", moduleNode.getName()); //$NON-NLS-1$
137
log.error(ie.getMessage());
138                 }
139                 catch (IllegalAccessException JavaDoc ae) {
140                     log.error(ae.getMessage());
141                 }
142             }
143
144             // init the module
145
if (!module.isInitialized()) {
146                 if (!module.isRestartNeeded()) {
147                     log.info("start initialization of module {}", def.getName());
148                     Content moduleConfigNode = ContentUtil.getCaseInsensitive(moduleNode, CONFIG_NODE);
149                     module.init(moduleConfigNode);
150                     log.info("module {} initialized", def.getName()); //$NON-NLS-1$
151
}
152                 else {
153                     log.warn("won't initialize the module {} since a system restart is needed", module.getName());
154                 }
155
156                 if (module.isRestartNeeded()) {
157                     ModuleRegistration.getInstance().setRestartNeeded(true);
158                 }
159             }
160             final Module m = module;
161             // add destroy method as a shutdown task
162
ShutdownManager.addShutdownTask(new ShutdownTask() {
163
164                 public boolean execute(info.magnolia.context.Context context) {
165                     log.info("Shutting down module: " + m.getName());
166                     m.destroy();
167                     return true;
168                 }
169
170                 public String JavaDoc toString() {
171                     return getClass().getName() + " " + m;
172                 }
173             });
174
175         }
176         catch (Exception JavaDoc e) {
177             log.error("can't initialize module " + moduleNode.getHandle(), e); //$NON-NLS-1$
178
}
179     }
180
181     public void reload() throws ConfigurationException {
182         init();
183     }
184
185     /**
186      * Returns the node containing the modules definition in the config repository
187      * @return the node
188      * @throws PathNotFoundException
189      * @throws RepositoryException
190      * @throws AccessDeniedException
191      */

192     public Content getModulesNode() throws PathNotFoundException, RepositoryException, AccessDeniedException {
193         HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
194         if (!hm.isExist("/" + MODULES_NODE)) {
195             hm.createContent("/", MODULES_NODE, ItemType.CONTENT.getSystemName());
196         }
197         Content modulesNode = hm.getContent(MODULES_NODE);
198         return modulesNode;
199     }
200
201     /**
202      * Get the module instance
203      * @param name
204      * @return the instance
205      */

206     public Module getModuleInstance(String JavaDoc name) {
207         return (Module) this.modules.get(name);
208     }
209
210     /**
211      * @return the map containing the modules
212      */

213     public Map JavaDoc getModuleInstances() {
214         return this.modules;
215     }
216
217     /**
218      * Register this module instance to avoid a second instantiation.
219      * @param name
220      * @param module
221      */

222     public void addModuleInstance(String JavaDoc name, Module module) {
223         this.modules.put(name, module);
224     }
225
226 }
Popular Tags