1 /**
2 /**
3 * This file Copyright (c) 2003-2013 Magnolia International
4 * Ltd. (http://www.magnolia-cms.com). All rights reserved.
5 *
6 *
7 * This file is dual-licensed under both the Magnolia
8 * Network Agreement and the GNU General Public License.
9 * You may elect to use one or the other of these licenses.
10 *
11 * This file is distributed in the hope that it will be
12 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
15 * Redistribution, except as permitted by whichever of the GPL
16 * or MNA you select, is prohibited.
17 *
18 * 1. For the GPL license (GPL), you can redistribute and/or
19 * modify this file under the terms of the GNU General
20 * Public License, Version 3, as published by the Free Software
21 * Foundation. You should have received a copy of the GNU
22 * General Public License, Version 3 along with this program;
23 * if not, write to the Free Software Foundation, Inc., 51
24 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 * 2. For the Magnolia Network Agreement (MNA), this file
27 * and the accompanying materials are made available under the
28 * terms of the MNA which accompanies this distribution, and
29 * is available at http://www.magnolia-cms.com/mna.html
30 *
31 * Any modifications to this file must keep this entire header
32 * intact.
33 *
34 */
35 package info.magnolia.commands;
36
37 import info.magnolia.commands.chain.Command;
38 import info.magnolia.commands.chain.Context;
39
40 import org.apache.commons.beanutils.BeanUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45 /**
46 * To make the coding of commands as easy as possible the default values set in
47 * the config are set and the values of the context are set as properties too if
48 * the naming matches.
49 */
50 public abstract class MgnlCommand implements Command {
51
52 public static Logger log = LoggerFactory.getLogger(MgnlCommand.class);
53
54 private boolean isEnabled = true;
55
56 /**
57 * Make sure that the context is castable to a magnolia context. DO NOT
58 * override this method (with the info.magnolia.commands.chain.Context
59 * parameter type) in the descendant classes - unless you know for 100% what
60 * you are going to do.
61 *
62 * @return true on success, false otherwise
63 */
64 @Override
65 public boolean execute(Context ctx) throws Exception {
66 if (!(ctx instanceof info.magnolia.context.Context)) {
67 throw new IllegalArgumentException("context must be of type " + info.magnolia.context.Context.class);
68 }
69
70 boolean success = executeSynchronized(ctx);
71 // convert the confusing true false behavior to fit the constants defined in the Command interface
72 return !success;
73 }
74
75 private boolean executeSynchronized(Context ctx) throws Exception {
76 boolean success = false; // break execution
77
78 synchronized (this) {
79 BeanUtils.populate(this, ctx);
80 try {
81 success = execute((info.magnolia.context.Context) ctx);
82 } catch (Exception e) {
83 final info.magnolia.context.Context mgnlCtx = (info.magnolia.context.Context) ctx;
84 if (mgnlCtx.getAttribute(info.magnolia.context.Context.ATTRIBUTE_EXCEPTION, info.magnolia.context.Context.LOCAL_SCOPE) == null) {
85 mgnlCtx.setAttribute(info.magnolia.context.Context.ATTRIBUTE_EXCEPTION, e, info.magnolia.context.Context.LOCAL_SCOPE);
86 }
87 throw e;
88
89 } finally {
90 release();
91 }
92 }
93 return success;
94 }
95
96 /**
97 * This is the actual method to be overridden in descendant classes.
98 */
99 public abstract boolean execute(info.magnolia.context.Context context) throws Exception;
100
101 /**
102 * If a clone is passivated we call this method. Please clean up private
103 * properties.
104 */
105 public void release() {
106 }
107
108 public boolean isEnabled() {
109 return this.isEnabled;
110 }
111
112
113 public void setEnabled(boolean isEnabled) {
114 this.isEnabled = isEnabled;
115 }
116
117 @Override
118 public Command clone() throws CloneNotSupportedException {
119 return (Command) super.clone();
120 }
121 }