1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.commands.chain.Catalog;
39 import info.magnolia.commands.chain.Command;
40 import info.magnolia.commands.chain.Context;
41 import info.magnolia.context.SimpleContext;
42 import info.magnolia.jcr.node2bean.Node2BeanException;
43 import info.magnolia.jcr.node2bean.Node2BeanProcessor;
44 import info.magnolia.jcr.util.NodeTypes;
45 import info.magnolia.objectfactory.Components;
46
47 import java.util.HashMap;
48 import java.util.Iterator;
49 import java.util.Map;
50
51 import javax.inject.Inject;
52 import javax.inject.Singleton;
53 import javax.jcr.RepositoryException;
54
55 import org.apache.commons.lang.StringUtils;
56
57
58
59
60
61
62 @Singleton
63 public class CommandsManager extends ObservedManager {
64
65 public static final String DEFAULT_CATALOG = "default";
66
67 public static final String COMMAND_DELIM = "-";
68
69 private final CommandTransformer commandTransformer;
70
71 private final Node2BeanProcessor nodeToBean;
72
73 private Map<String, MgnlCatalog> catalogs;
74
75 @Inject
76 public CommandsManager(Node2BeanProcessor nodeToBean) {
77 this.nodeToBean = nodeToBean;
78 this.commandTransformer = new CommandTransformer();
79 this.catalogs = new HashMap<String, MgnlCatalog>();
80 }
81
82
83
84
85 @Override
86 protected void onRegister(Content node) {
87
88 if (node.getChildren(NodeTypes.Content.NAME).size() == 0) {
89 registerCatalog(node);
90 }
91 else{
92 for (Iterator iter = node.getChildren(NodeTypes.Content.NAME).iterator(); iter.hasNext();) {
93 onRegister((Content) iter.next());
94 }
95 }
96 }
97
98
99 protected void registerCatalog(Content node) {
100 log.debug("Registering commands at {}...", node.getHandle());
101 try {
102 MgnlCatalog catalog = (MgnlCatalog) nodeToBean.toBean(node.getJCRNode(), true, commandTransformer, Components.getComponentProvider());
103 MgnlCatalog current = catalogs.get(catalog.getName());
104 if (current == null) {
105 catalogs.put(catalog.getName(), catalog);
106 log.info("Catalog [{}] registered: {}", catalog.getName(), catalog);
107 } else {
108 for (String commandName : catalog.getNames()) {
109 Command command = current.getCommand(commandName);
110 if (command != null) {
111 log.warn(String.format("Command [%s] found at [%s] already exists in the catalog [%s], skipping...", commandName, node.getHandle(), current.getName()));
112 } else {
113 log.info("Adding new command [{}] to already registered catalog [{}]...", commandName, current.getName());
114 current.addCommand(commandName, catalog.getCommand(commandName));
115 }
116 }
117 }
118 }
119 catch (RepositoryException e) {
120 log.error("Can't read catalog [" + node + "]", e);
121 }
122 catch (Node2BeanException e) {
123 log.error("Can't create catalog [" + node + "]", e);
124 }
125 }
126
127
128
129
130 @Override
131 protected void onClear() {
132 catalogs.clear();
133 }
134
135
136
137
138
139
140
141 public Command getCommand(String catalogName, String commandName) {
142
143 MgnlCatalog catalog = catalogs.get(StringUtils.isNotEmpty(catalogName) ? catalogName : DEFAULT_CATALOG);
144 if (catalog != null) {
145 Command command = catalog.getCommand(commandName);
146 if (command != null) {
147 Command copy = command.copy(command);
148 if (copy != null) {
149 return copy;
150 }
151 log.warn("Cannot create copy of command [" + commandName + "] from catalog [" + catalogName + "].");
152 }
153 }
154
155 return null;
156 }
157
158
159
160
161 public Command getCommand(String commandName) {
162 String catalogName = DEFAULT_CATALOG;
163 if (StringUtils.contains(commandName, COMMAND_DELIM)) {
164 catalogName = StringUtils.substringBefore(commandName, COMMAND_DELIM);
165 commandName = StringUtils.substringAfter(commandName, COMMAND_DELIM);
166 }
167
168 Command command = getCommand(catalogName, commandName);
169 if (command == null) {
170 command = getCommand(DEFAULT_CATALOG, commandName);
171 }
172 return command;
173 }
174
175
176
177
178
179 @Deprecated
180 public static CommandsManager getInstance() {
181 return Components.getComponent(CommandsManager.class);
182 }
183
184
185
186
187
188
189
190
191
192 public boolean executeCommand(final String catalogName, final String commandName, final Map<String, Object> params) throws Exception {
193 final Command command = getCommand(catalogName, commandName);
194 if (command == null) {
195 throw new Exception(String.format("Command [%s] could not be found in catalog [%s]", commandName, catalogName));
196 }
197 log.debug("Executing command [{}] from catalog [{}] and params [{}]...", new Object[] { commandName, catalogName, params });
198 return executeCommand(command, params);
199 }
200
201
202
203
204
205
206
207
208 public boolean executeCommand(final String commandName, final Map<String, Object> params) throws Exception {
209 return executeCommand(DEFAULT_CATALOG, commandName, params);
210 }
211
212
213
214
215
216
217 public boolean executeCommand(final Command command, final Map<String, Object> params) throws Exception {
218 return executeCommand(command, params, new SimpleContext());
219 }
220
221
222
223
224
225
226 public boolean executeCommand(final Command command, final Map<String, Object> params, Context context) throws Exception {
227 if (params != null) {
228 context.putAll(params);
229 }
230 return command.execute(context);
231 }
232
233 Catalog getCatalogByName(String name) {
234 return catalogs.get(name);
235 }
236
237 }