KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.config.ConfigurationException;
16 import info.magnolia.cms.beans.config.ContentRepository;
17 import info.magnolia.cms.beans.config.ModuleLoader;
18 import info.magnolia.cms.core.Content;
19 import info.magnolia.cms.core.HierarchyManager;
20 import info.magnolia.cms.core.Path;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31
32 import javax.jcr.PathNotFoundException;
33
34 import org.apache.log4j.Logger;
35
36
37 /**
38  * Responsible for initialization and registration of modules.
39  * @author philipp
40  * @version $Revision$ ($Author$)
41  */

42 public final class ModuleFactory {
43
44     /**
45      * Util has no public constructor
46      */

47     private ModuleFactory() {
48     }
49
50     /**
51      * Logger
52      */

53     private static Logger log = Logger.getLogger(ModuleFactory.class);
54
55     /**
56      * Instantiate a module once.
57      */

58     private static Map JavaDoc instantiatedModules = new HashMap JavaDoc();
59
60     /**
61      * Register all jars with a magnolia module manifets
62      * @throws ConfigurationException if a module has an not handled error during registration
63      */

64     public static void init() throws ConfigurationException {
65         log.info("Loading module jars"); //$NON-NLS-1$
66
try {
67             HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
68             Content startPage = hm.getContent(ModuleLoader.CONFIG_PAGE);
69             init(startPage);
70             log.info("Finished loading module jars"); //$NON-NLS-1$
71
}
72         catch (Exception JavaDoc e) {
73             log.fatal("Failed to load the module jar"); //$NON-NLS-1$
74
log.fatal(e.getMessage(), e);
75             throw new ConfigurationException(e.getMessage());
76         }
77     }
78
79     /**
80      * @param modulesNode root node
81      */

82     private static void init(Content modulesNode) {
83         // load all module jars
84
try {
85             List JavaDoc jars = getJarFiles();
86
87             for (Iterator JavaDoc iter = jars.iterator(); iter.hasNext();) {
88                 JarFile JavaDoc jar = (JarFile JavaDoc) iter.next();
89                 try {
90                     Manifest JavaDoc manifest = jar.getManifest();
91                     if (manifest != null) {
92                         // read from manifest
93
String JavaDoc moduleName = manifest.getMainAttributes().getValue("Magnolia-Module-Name"); //$NON-NLS-1$
94
String JavaDoc version = manifest.getMainAttributes().getValue("Magnolia-Module-Version"); //$NON-NLS-1$
95
String JavaDoc moduleClassName = jar
96                             .getManifest()
97                             .getMainAttributes()
98                             .getValue("Magnolia-Module-Class"); //$NON-NLS-1$
99

100                         // if everything is provided
101
if (moduleName != null && moduleClassName != null && version != null) {
102                             try {
103                                 Module module = (Module) Class.forName(moduleClassName).newInstance();
104                                 int registerState = Module.REGISTER_STATE_NONE;
105                                 instantiatedModules.put(moduleName, module);
106                                 Content moduleNode;
107
108                                 try {
109                                     moduleNode = modulesNode.getContent(moduleName);
110                                     // node exists: is it a new version ?
111
if (!version.equals(moduleNode.getNodeData("version").getString())) { //$NON-NLS-1$
112
registerState = Module.REGISTER_STATE_NEW_VERSION;
113                                     }
114                                 }
115                                 // first installation
116
catch (PathNotFoundException e1) {
117                                     moduleNode = modulesNode.createContent(moduleName);
118                                     ModuleUtil.createMinimalConfiguration(
119                                         moduleNode,
120                                         moduleName,
121                                         moduleClassName,
122                                         version);
123                                     registerState = Module.REGISTER_STATE_INSTALLATION;
124                                 }
125
126                                 try {
127                                     // call register: this is always done not only during the first startup
128
module.register(moduleName, version, moduleNode, jar, registerState);
129                                     if (registerState == Module.REGISTER_STATE_NEW_VERSION) {
130                                         moduleNode.getNodeData("version", true).setValue(version); //$NON-NLS-1$
131
}
132                                     modulesNode.save();
133                                 }
134                                 catch (RegisterException e) {
135                                     switch (registerState) {
136                                         case Module.REGISTER_STATE_INSTALLATION:
137                                             log.error("can't install module [" + moduleName + "]" + version, e); //$NON-NLS-1$ //$NON-NLS-2$
138
break;
139                                         case Module.REGISTER_STATE_NEW_VERSION:
140                                             log.error("can't update module [" + moduleName + "] to version " + version, //$NON-NLS-1$ //$NON-NLS-2$
141
e);
142                                             break;
143                                         default:
144                                             log.error("error during registering an already installed module [" //$NON-NLS-1$
145
+ moduleName + "]", e); //$NON-NLS-1$
146
break;
147                                     }
148                                 }
149                             }
150
151                             catch (Exception JavaDoc e) {
152                                 log.error("can't register module [" + moduleName + "]", e); //$NON-NLS-1$ //$NON-NLS-2$
153
}
154                         }
155                     }
156                 }
157                 catch (IOException JavaDoc e) {
158                     log.error("can't read manifest", e); //$NON-NLS-1$
159
}
160             }
161
162         }
163         catch (IOException JavaDoc e) {
164             log.error("can't load module jars", e); //$NON-NLS-1$
165
}
166     }
167
168     /**
169      * Returns a single instance of a module
170      * @param name module name
171      * @return module
172      */

173     public static Module getModuleInstance(String JavaDoc name) {
174         return (Module) instantiatedModules.get(name);
175     }
176
177     /**
178      * @return all the jar files from the lib directory containingn a magnolia module manifest
179      * @throws IOException io exception
180      */

181     private static List JavaDoc getJarFiles() throws IOException JavaDoc {
182         List JavaDoc jars = new ArrayList JavaDoc();
183
184         File JavaDoc dir = new File JavaDoc(Path.getAbsoluteFileSystemPath("WEB-INF/lib")); //$NON-NLS-1$
185
if (dir != null) {
186             File JavaDoc[] files = dir.listFiles();
187             if (files != null) {
188                 for (int i = 0; i < files.length; i++) {
189                     File JavaDoc jarFile = files[i];
190                     if (jarFile.getName().endsWith(".jar")) { //$NON-NLS-1$
191
JarFile JavaDoc jar = new JarFile JavaDoc(jarFile);
192                         jars.add(jar);
193                     }
194                 }
195             }
196         }
197         return jars;
198     }
199
200     /**
201      * Call the init method of a module. If there is not yet an instance a new instance is created.
202      * @param config configuration of the module
203      * @param moduleClassName classname to initialize
204      * @throws InstantiationException exception
205      * @throws IllegalAccessException exception
206      * @throws ClassNotFoundException exception
207      * @throws InvalidConfigException exception
208      */

209     public static void initModule(ModuleConfig config, String JavaDoc moduleClassName) throws InstantiationException JavaDoc,
210         IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc, InvalidConfigException {
211         Module module = getModuleInstance(config.getModuleName());
212         if (module == null) {
213             module = (Module) Class.forName(moduleClassName).newInstance();
214         }
215         module.init(config);
216     }
217
218 }
Popular Tags