1 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 ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.jar.JarFile ; 30 import java.util.jar.Manifest ; 31 32 import javax.jcr.PathNotFoundException; 33 34 import org.apache.log4j.Logger; 35 36 37 42 public final class ModuleFactory { 43 44 47 private ModuleFactory() { 48 } 49 50 53 private static Logger log = Logger.getLogger(ModuleFactory.class); 54 55 58 private static Map instantiatedModules = new HashMap (); 59 60 64 public static void init() throws ConfigurationException { 65 log.info("Loading module jars"); 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"); } 72 catch (Exception e) { 73 log.fatal("Failed to load the module jar"); log.fatal(e.getMessage(), e); 75 throw new ConfigurationException(e.getMessage()); 76 } 77 } 78 79 82 private static void init(Content modulesNode) { 83 try { 85 List jars = getJarFiles(); 86 87 for (Iterator iter = jars.iterator(); iter.hasNext();) { 88 JarFile jar = (JarFile ) iter.next(); 89 try { 90 Manifest manifest = jar.getManifest(); 91 if (manifest != null) { 92 String moduleName = manifest.getMainAttributes().getValue("Magnolia-Module-Name"); String version = manifest.getMainAttributes().getValue("Magnolia-Module-Version"); String moduleClassName = jar 96 .getManifest() 97 .getMainAttributes() 98 .getValue("Magnolia-Module-Class"); 100 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 if (!version.equals(moduleNode.getNodeData("version").getString())) { registerState = Module.REGISTER_STATE_NEW_VERSION; 113 } 114 } 115 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 module.register(moduleName, version, moduleNode, jar, registerState); 129 if (registerState == Module.REGISTER_STATE_NEW_VERSION) { 130 moduleNode.getNodeData("version", true).setValue(version); } 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); break; 139 case Module.REGISTER_STATE_NEW_VERSION: 140 log.error("can't update module [" + moduleName + "] to version " + version, e); 142 break; 143 default: 144 log.error("error during registering an already installed module [" + moduleName + "]", e); break; 147 } 148 } 149 } 150 151 catch (Exception e) { 152 log.error("can't register module [" + moduleName + "]", e); } 154 } 155 } 156 } 157 catch (IOException e) { 158 log.error("can't read manifest", e); } 160 } 161 162 } 163 catch (IOException e) { 164 log.error("can't load module jars", e); } 166 } 167 168 173 public static Module getModuleInstance(String name) { 174 return (Module) instantiatedModules.get(name); 175 } 176 177 181 private static List getJarFiles() throws IOException { 182 List jars = new ArrayList (); 183 184 File dir = new File (Path.getAbsoluteFileSystemPath("WEB-INF/lib")); if (dir != null) { 186 File [] files = dir.listFiles(); 187 if (files != null) { 188 for (int i = 0; i < files.length; i++) { 189 File jarFile = files[i]; 190 if (jarFile.getName().endsWith(".jar")) { JarFile jar = new JarFile (jarFile); 192 jars.add(jar); 193 } 194 } 195 } 196 } 197 return jars; 198 } 199 200 209 public static void initModule(ModuleConfig config, String moduleClassName) throws InstantiationException , 210 IllegalAccessException , ClassNotFoundException , 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 |