KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > commands > CommandsManager


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.commands;
14
15 import java.util.Iterator JavaDoc;
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 /**
31  * Manages the Commands and Catalogs.
32  * @author Philipp Bracher
33  * @version $Revision: 6893 $ ($Author: philipp $)
34  */

35 public class CommandsManager extends ObservedManager {
36
37     public static final String JavaDoc DEFAULT_CATALOG = "default";
38
39     public static final String JavaDoc COMMAND_DELIM = "-";
40
41     /**
42      * Register this catalogue
43      */

44     protected void onRegister(Content node) {
45         // is this a catalog or a collection of catalogs?
46
if(node.getChildren(ItemType.CONTENT).size() == 0){
47             registerCatalog(node);
48         }
49         else{
50             for (Iterator JavaDoc iter = node.getChildren(ItemType.CONTENT).iterator(); iter.hasNext();) {
51                 onRegister((Content) iter.next());
52             }
53         }
54     }
55
56     /**
57      * @param node
58      */

59     protected void registerCatalog(Content node) {
60         Catalog catalog = new MgnlRepositoryCatalog(node);
61         String JavaDoc name = getCatalogName(node);
62         if (log.isDebugEnabled()) {
63             log.debug("registering catalog {}", name);
64         }
65         CatalogFactory.getInstance().addCatalog(name, catalog);
66     }
67
68     /**
69      * Get the name to use for this catalog. Checks first the presence of a ctalogName property. If not found and the
70      * nodes name is 'commands' the parents name is used. Otherwise the catalog name is the name of the node itself.
71      * @param node
72      * @return the name of this catalog
73      */

74     protected String JavaDoc 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     /**
92      * Clear all catalogues
93      */

94     protected void onClear() {
95         CatalogFactory.clear();
96     }
97
98     /**
99      * Get the command
100      * @param catalogName the catalog containing the command
101      * @param commandName the name of the command
102      * @return the command to execute
103      */

104     public Command getCommand(String JavaDoc catalogName, String JavaDoc 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     /**
114      * Use a delimiter to separate the catalog and command name
115      * @param commandName
116      * @return the command
117      */

118     public Command getCommand(String JavaDoc commandName) {
119         String JavaDoc 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     /**
133      * @return Returns the instance.
134      */

135     public static CommandsManager getInstance() {
136         return (CommandsManager) FactoryUtil.getSingleton(CommandsManager.class);
137     }
138
139 }
140
Popular Tags