1 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 ; 19 import java.io.IOException ; 20 import java.util.Iterator ; 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 34 public abstract class AbstractModule implements Module { 35 36 39 protected ModuleDefinition definition; 40 41 44 protected Content moduleNode; 45 46 49 private boolean restartNeeded = false; 50 51 54 private boolean initialized = false; 55 56 59 private static Logger log = LoggerFactory.getLogger(AbstractModule.class); 60 61 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 moduleName = this.getName(); 72 73 registerServlets(def); 74 75 registerRepositories(def); 76 77 bootstrap(moduleName); 78 79 installFiles(moduleName, def.getModuleRoot()); 80 81 onRegister(registerState); 83 } 84 catch (Exception e) { 85 throw new RegisterException("can't register module " + this.definition.getName(), e); 86 } 87 } 88 } 89 90 94 public void unregister(ModuleDefinition def, Content moduleNode) { 95 onUnRegister(); 97 } 100 101 105 protected void onRegister(int registerState) throws RegisterException { 106 107 } 108 109 112 protected void onUnRegister() { 113 } 114 115 118 public boolean isRestartNeeded() { 119 return this.restartNeeded; 120 } 121 122 125 protected void setRestartNeeded(boolean restartNeeded) { 126 this.restartNeeded = restartNeeded; 127 } 128 129 132 public ModuleDefinition getModuleDefinition() { 133 return this.definition; 134 } 135 136 139 protected void setDefinition(ModuleDefinition definition) { 140 this.definition = definition; 141 } 142 143 146 public boolean isInitialized() { 147 return this.initialized; 148 } 149 150 153 protected void setInitialized(boolean initialized) { 154 this.initialized = initialized; 155 } 156 157 161 public String getName() { 162 if (definition != null) { 163 return definition.getName(); 164 } 165 return null; 166 } 167 168 171 public Content getModuleNode() { 172 return this.moduleNode; 173 } 174 175 178 protected void setModuleNode(Content moduleNode) { 179 this.moduleNode = moduleNode; 180 } 181 182 185 public void destroy() { 186 187 } 188 189 195 protected void installFiles(final String moduleName, File moduleRoot) throws Exception { 196 String [] moduleFiles = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() { 198 199 public boolean accept(String 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 214 protected void registerRepositories(ModuleDefinition def) throws RegisterException { 215 boolean restartNeeded = false; 216 217 for (Iterator iter = def.getRepositories().iterator(); iter.hasNext();) { 219 RepositoryDefinition repDef = (RepositoryDefinition) iter.next(); 220 String repository = repDef.getName(); 221 222 String nodetypeFile = repDef.getNodeTypeFile(); 223 boolean repositoryAdded = ModuleUtil.registerRepository(repository, nodetypeFile); 224 if (repositoryAdded) { 225 restartNeeded = true; 226 } 227 228 for (Iterator iterator = repDef.getWorkspaces().iterator(); iterator.hasNext();) { 229 String workspace = (String ) 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 248 protected void registerServlets(ModuleDefinition def) throws JDOMException, IOException { 249 boolean restartNeeded = false; 250 251 for (Iterator 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 270 protected void bootstrap(final String moduleName) throws IOException , RegisterException { 271 String [] moduleBootstrap = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() { 273 274 public boolean accept(String name) { 275 return name.startsWith("/mgnl-bootstrap/" + moduleName) && name.endsWith(".xml"); 276 } 277 }); 278 279 ModuleUtil.bootstrap(moduleBootstrap); 280 } 281 282 285 public String toString() { 286 return getClass().getName() + " (" + getName() + " module)"; 287 } 288 289 } 290 | Popular Tags |