View Javadoc

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