View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.commands.chain.Command;
37  import info.magnolia.commands.chain.Context;
38  
39  import org.apache.commons.beanutils.BeanUtils;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  
44  /**
45   * To make the coding of commands as easy as possible the default values set in
46   * the config are set and the values of the context are set as properties too if
47   * the naming matches.
48   */
49  public abstract class MgnlCommand implements Command {
50  
51      public static Logger log = LoggerFactory.getLogger(MgnlCommand.class);
52  
53      private boolean isEnabled = true;
54  
55      /**
56       * Make sure that the context is castable to a magnolia context. DO NOT
57       * override this method (with the info.magnolia.commands.chain.Context
58       * parameter type) in the descendant classes - unless you know for 100% what
59       * you are going to do.
60       *
61       * @return true on success, false otherwise
62       */
63      @Override
64      public boolean execute(Context ctx) throws Exception {
65          if (!(ctx instanceof info.magnolia.context.Context)) {
66              throw new IllegalArgumentException("context must be of type " + info.magnolia.context.Context.class);
67          }
68  
69          boolean success = executeSynchronized(ctx);
70          // convert the confusing true false behavior to fit the constants defined in the Command interface
71          return !success;
72      }
73  
74      private boolean executeSynchronized(Context ctx) throws Exception {
75          boolean success = false; // break execution
76  
77          synchronized (this) {
78              BeanUtils.populate(this, ctx);
79              try {
80                  success = execute((info.magnolia.context.Context) ctx);
81              } catch (Exception e) {
82                  final info.magnolia.context.Context mgnlCtx = (info.magnolia.context.Context) ctx;
83                  if (mgnlCtx.getAttribute(info.magnolia.context.Context.ATTRIBUTE_EXCEPTION, info.magnolia.context.Context.LOCAL_SCOPE) == null) {
84                      mgnlCtx.setAttribute(info.magnolia.context.Context.ATTRIBUTE_EXCEPTION, e, info.magnolia.context.Context.LOCAL_SCOPE);
85                  }
86                  throw e;
87  
88              } finally {
89                  release();
90              }
91          }
92          return success;
93      }
94  
95      /**
96       * This is the actual method to be overridden in descendant classes.
97       */
98      public abstract boolean execute(info.magnolia.context.Context context) throws Exception;
99  
100     /**
101      * If a clone is passivated we call this method. Please clean up private
102      * properties.
103      */
104     public void release() {
105     }
106 
107     public boolean isEnabled() {
108         return this.isEnabled;
109     }
110 
111 
112     public void setEnabled(boolean isEnabled) {
113         this.isEnabled = isEnabled;
114     }
115 
116     @Override
117     public Command clone() throws CloneNotSupportedException {
118         return (Command) super.clone();
119     }
120 }