1 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 ; 26 import java.util.Map ; 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 40 public class ModuleLoader { 41 42 45 protected static Logger log = LoggerFactory.getLogger(ModuleLoader.class); 46 47 50 public static final String MODULES_NODE = "modules"; 52 public static final String CONFIG_NODE_VIRTUAL_MAPPING = "virtualURIMapping"; 54 public static final String CONFIG_NODE = "config"; 56 59 private Map modules = new HashMap (); 60 61 64 public ModuleLoader() { 65 } 66 67 70 public static ModuleLoader getInstance() { 71 return (ModuleLoader) FactoryUtil.getSingleton(ModuleLoader.class); 72 } 73 74 78 protected void init() throws ConfigurationException { 79 log.info("Loading modules"); try { 81 Content modulesNode = getModulesNode(); 82 init(modulesNode); 83 log.info("Finished loading modules"); } 85 catch (Exception e) { 86 log.error("Failed to initialize module loader"); log.error(e.getMessage(), e); 88 throw new ConfigurationException(e.getMessage()); 89 } 90 } 91 92 96 private void init(Content modulesNode) { 97 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 if (module == null) { 129 try { 130 String moduleClassName = moduleNode.getNodeData("class").getString(); 132 module = (Module) ClassUtil.newInstance(moduleClassName); 133 this.addModuleInstance(def.getName(), module); 134 } 135 catch (InstantiationException ie) { 136 log.error("Module {} failed to load", moduleNode.getName()); log.error(ie.getMessage()); 138 } 139 catch (IllegalAccessException ae) { 140 log.error(ae.getMessage()); 141 } 142 } 143 144 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()); } 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 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 toString() { 171 return getClass().getName() + " " + m; 172 } 173 }); 174 175 } 176 catch (Exception e) { 177 log.error("can't initialize module " + moduleNode.getHandle(), e); } 179 } 180 181 public void reload() throws ConfigurationException { 182 init(); 183 } 184 185 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 206 public Module getModuleInstance(String name) { 207 return (Module) this.modules.get(name); 208 } 209 210 213 public Map getModuleInstances() { 214 return this.modules; 215 } 216 217 222 public void addModuleInstance(String name, Module module) { 223 this.modules.put(name, module); 224 } 225 226 } | Popular Tags |