View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.commands;
35  
36  import java.util.Iterator;
37  
38  import info.magnolia.cms.beans.config.ObservedManager;
39  import info.magnolia.cms.core.Content;
40  import info.magnolia.cms.core.ItemType;
41  import info.magnolia.content2bean.Content2BeanException;
42  import info.magnolia.content2bean.Content2BeanTransformer;
43  import info.magnolia.content2bean.Content2BeanUtil;
44  import info.magnolia.objectfactory.Components;
45  
46  import org.apache.commons.chain.Catalog;
47  import org.apache.commons.chain.CatalogFactory;
48  import org.apache.commons.chain.Command;
49  import org.apache.commons.lang.StringUtils;
50  
51  
52  /**
53   * Manages the Commands and Catalogs.
54   *
55   * @author Philipp Bracher
56   * @version $Revision: 32667 $ ($Author: gjoseph $)
57   */
58  public class CommandsManager extends ObservedManager {
59  
60      public static final String DEFAULT_CATALOG = "default";
61  
62      public static final String COMMAND_DELIM = "-";
63  
64      protected static Content2BeanTransformer COMMAND_TRANSFORMER = new CommandTransformer();
65  
66      /**
67       * Register this catalogue
68       */
69      protected void onRegister(Content node) {
70          // is this a catalog or a collection of catalogs?
71          if(node.getChildren(ItemType.CONTENT).size() == 0){
72              registerCatalog(node);
73          }
74          else{
75              for (Iterator iter = node.getChildren(ItemType.CONTENT).iterator(); iter.hasNext();) {
76                  onRegister((Content) iter.next());
77              }
78          }
79      }
80  
81      protected void registerCatalog(Content node) {
82          try {
83              MgnlCatalog catalog = (MgnlCatalog) Content2BeanUtil.toBean(node, true, COMMAND_TRANSFORMER);
84              CatalogFactory.getInstance().addCatalog(catalog.getName(), catalog);
85  
86              log.debug("Catalog {} registered: {}", new Object[]{catalog.getName(), catalog});
87          }
88          catch (Content2BeanException e) {
89              log.error("Can't create catalog [" + node  + "]", e);
90          }
91      }
92  
93      /**
94       * Clear all catalogues
95       */
96      protected void onClear() {
97          CatalogFactory.clear();
98      }
99  
100     /**
101      * Get the command
102      * @param catalogName the catalog containing the command
103      * @param commandName the name of the command
104      * @return the command to execute
105      */
106     public Command getCommand(String catalogName, String commandName) {
107         Catalog catalog = CatalogFactory.getInstance().getCatalog(catalogName);
108         if (catalog != null) {
109             return catalog.getCommand(commandName);
110         }
111 
112         return null;
113     }
114 
115     /**
116      * Use a delimiter to separate the catalog and command name
117      * @param commandName
118      * @return the command
119      */
120     public Command getCommand(String commandName) {
121         String catalogName = DEFAULT_CATALOG;
122         if (StringUtils.contains(commandName, COMMAND_DELIM)) {
123             catalogName = StringUtils.substringBefore(commandName, COMMAND_DELIM);
124             commandName = StringUtils.substringAfter(commandName, COMMAND_DELIM);
125         }
126 
127         Command command = getCommand(catalogName, commandName);
128         if (command == null) {
129             command = getCommand(DEFAULT_CATALOG, commandName);
130         }
131         return command;
132     }
133 
134     /**
135      * @return Returns the instance.
136      */
137     public static CommandsManager getInstance() {
138         return Components.getSingleton(CommandsManager.class);
139     }
140 
141 }