View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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 javax.inject.Singleton;
39  
40  import info.magnolia.cms.beans.config.ObservedManager;
41  import info.magnolia.cms.core.Content;
42  import info.magnolia.cms.core.ItemType;
43  import info.magnolia.content2bean.Content2BeanException;
44  import info.magnolia.content2bean.Content2BeanUtil;
45  import info.magnolia.objectfactory.Components;
46  
47  import org.apache.commons.chain.Catalog;
48  import org.apache.commons.chain.CatalogFactory;
49  import org.apache.commons.chain.Command;
50  import org.apache.commons.lang.StringUtils;
51  
52  
53  /**
54   * Manages the Commands and Catalogs.
55   *
56   * @author Philipp Bracher
57   * @version $Revision$ ($Author$)
58   */
59  @Singleton
60  public class CommandsManager extends ObservedManager {
61  
62      public static final String DEFAULT_CATALOG = "default";
63  
64      public static final String COMMAND_DELIM = "-";
65  
66      private final CommandTransformer commandTransformer;
67  
68      public CommandsManager() {
69          this.commandTransformer = new CommandTransformer();
70      }
71  
72      /**
73       * Register observation for command catalogs.
74       */
75      @Override
76      protected void onRegister(Content node) {
77          // is this a catalog or a collection of catalogs?
78          if(node.getChildren(ItemType.CONTENT).size() == 0){
79              registerCatalog(node);
80          }
81          else{
82              for (Iterator iter = node.getChildren(ItemType.CONTENT).iterator(); iter.hasNext();) {
83                  onRegister((Content) iter.next());
84              }
85          }
86      }
87  
88      protected void registerCatalog(Content node) {
89          try {
90              MgnlCatalog catalog = (MgnlCatalog) Content2BeanUtil.toBean(node, true, commandTransformer);
91              CatalogFactory factory = CatalogFactory.getInstance();
92              if (factory.getCatalog(catalog.getName()) == null) {
93                  factory.addCatalog(catalog.getName(), catalog);
94              } else {
95                  // runtime because this code is called by observation and there's no place to catch it anyway
96                  throw new RuntimeException("Catalog [" + catalog.getName() + "] is already registered. Please run: select * from nt:base where jcr:path like '/modules/%/commands/"+ catalog.getName()+"' on config repository to find out the duplicate.");
97              }
98  
99              log.debug("Catalog {} registered: {}", new Object[]{catalog.getName(), catalog});
100         }
101         catch (Content2BeanException e) {
102             log.error("Can't create catalog [" + node  + "]", e);
103         }
104     }
105 
106     /**
107      * Clear all catalogs.
108      */
109     @Override
110     protected void onClear() {
111         CatalogFactory.clear();
112     }
113 
114     /**
115      * Get the command.
116      * @param catalogName the catalog containing the command
117      * @param commandName the name of the command
118      * @return the command to execute
119      */
120     public Command getCommand(String catalogName, String commandName) {
121         Catalog catalog = CatalogFactory.getInstance().getCatalog(catalogName);
122         if (catalog != null) {
123             return catalog.getCommand(commandName);
124         }
125 
126         return null;
127     }
128 
129     /**
130      * Use a delimiter to separate the catalog and command name.
131      * @param commandName
132      * @return the command
133      */
134     public Command getCommand(String commandName) {
135         String catalogName = DEFAULT_CATALOG;
136         if (StringUtils.contains(commandName, COMMAND_DELIM)) {
137             catalogName = StringUtils.substringBefore(commandName, COMMAND_DELIM);
138             commandName = StringUtils.substringAfter(commandName, COMMAND_DELIM);
139         }
140 
141         Command command = getCommand(catalogName, commandName);
142         if (command == null) {
143             command = getCommand(DEFAULT_CATALOG, commandName);
144         }
145         return command;
146     }
147 
148     /**
149      * @return Returns the instance.
150      * @deprecated since 4.5, use IoC !
151      */
152     public static CommandsManager getInstance() {
153         return Components.getSingleton(CommandsManager.class);
154     }
155 
156 }