1 13 package info.magnolia.commands; 14 15 import java.util.Iterator ; 16 17 import info.magnolia.cms.beans.config.ObservedManager; 18 import info.magnolia.cms.core.Content; 19 import info.magnolia.cms.core.ItemType; 20 import info.magnolia.cms.util.FactoryUtil; 21 22 import javax.jcr.RepositoryException; 23 24 import org.apache.commons.chain.Catalog; 25 import org.apache.commons.chain.CatalogFactory; 26 import org.apache.commons.chain.Command; 27 import org.apache.commons.lang.StringUtils; 28 29 30 35 public class CommandsManager extends ObservedManager { 36 37 public static final String DEFAULT_CATALOG = "default"; 38 39 public static final String COMMAND_DELIM = "-"; 40 41 44 protected void onRegister(Content node) { 45 if(node.getChildren(ItemType.CONTENT).size() == 0){ 47 registerCatalog(node); 48 } 49 else{ 50 for (Iterator iter = node.getChildren(ItemType.CONTENT).iterator(); iter.hasNext();) { 51 onRegister((Content) iter.next()); 52 } 53 } 54 } 55 56 59 protected void registerCatalog(Content node) { 60 Catalog catalog = new MgnlRepositoryCatalog(node); 61 String name = getCatalogName(node); 62 if (log.isDebugEnabled()) { 63 log.debug("registering catalog {}", name); 64 } 65 CatalogFactory.getInstance().addCatalog(name, catalog); 66 } 67 68 74 protected String getCatalogName(Content node) { 75 try { 76 if (node.hasNodeData("catalogName")) { 77 return node.getNodeData("catalogName").toString(); 78 } 79 else if (node.getName().equals("commands")) { 80 return node.getParent().getName(); 81 } 82 else { 83 return node.getName(); 84 } 85 } 86 catch (RepositoryException e) { 87 return node.getName(); 88 } 89 } 90 91 94 protected void onClear() { 95 CatalogFactory.clear(); 96 } 97 98 104 public Command getCommand(String catalogName, String commandName) { 105 Catalog catalog = CatalogFactory.getInstance().getCatalog(catalogName); 106 if (catalog != null) { 107 return catalog.getCommand(commandName); 108 } 109 110 return null; 111 } 112 113 118 public Command getCommand(String commandName) { 119 String catalogName = DEFAULT_CATALOG; 120 if (StringUtils.contains(commandName, COMMAND_DELIM)) { 121 catalogName = StringUtils.substringBefore(commandName, COMMAND_DELIM); 122 commandName = StringUtils.substringAfter(commandName, COMMAND_DELIM); 123 } 124 125 Command command = getCommand(catalogName, commandName); 126 if (command == null) { 127 command = getCommand(DEFAULT_CATALOG, commandName); 128 } 129 return command; 130 } 131 132 135 public static CommandsManager getInstance() { 136 return (CommandsManager) FactoryUtil.getSingleton(CommandsManager.class); 137 } 138 139 } 140 | Popular Tags |