1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.tarsis.command.factory;
24
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import org.talika.tarsis.command.Command;
30 import org.talika.tarsis.command.action.ActionException;
31 import org.talika.tarsis.service.AbstractService;
32
33 /**
34 * Base implementation of <code>CommandFactory</code> interface.<br>
35 * <br>
36 * Command factories plug in system like a service extending <code>AbstractService</code>.
37 *
38 * @author Jose M. Palomar
39 * @version $Revision: 269 $
40 */
41 public abstract class CommandFactoryService extends AbstractService implements CommandFactory {
42
43
44 /**
45 * Commands map.
46 */
47 private Map commands = new HashMap();
48
49
50 /**
51 * Creates a new <code>CommandFactoryService</code> object.
52 */
53 public CommandFactoryService() {
54 }
55
56
57 /**
58 * Returns matching name command instance.
59 *
60 * @param name String name of command.
61 * @return Command command instance for given name or <code>null</code> if none
62 * found.
63 * @see org.talika.tarsis.command.factory.CommandFactory#findCommand(String)
64 */
65 public final Command findCommand(String name) {
66 Command cmd = (Command) commands.get(name);
67 if (cmd == null) {
68 loadCommand(name);
69 cmd = (Command) commands.get(name);
70 }
71
72 return cmd;
73 }
74
75 /**
76 * Tries to load command definition from repository for a given name.
77 *
78 * @param commandName String name of command to load.
79 */
80 protected abstract void loadCommand(String commandName);
81
82 /**
83 * Tries to load all command definitions from repository for a given package.
84 *
85 * @param packageName String name for package to load.
86 */
87 protected abstract void loadPackage(String packageName);
88
89 /**
90 * Adds a command definition to factory.
91 *
92 * @param name String name of command.
93 * @param cmd Command instance of command.
94 */
95 protected final void addCommand(String name, Command cmd) {
96
97 try {
98
99 if (getLogger().isDebugEnabled()) {
100 getLogger().logDebug("Command " + cmd.getFullName() + ": init");
101 }
102
103 cmd.getAction().init(getContext());
104 commands.put(name, cmd);
105
106 if (getLogger().isDebugEnabled()) {
107 getLogger().logDebug("Command " + cmd.getFullName() +
108 ": enabled");
109 }
110
111 }
112 catch (ActionException ae) {
113 if (getLogger().isErrorEnabled()) {
114 getLogger().logError("Error initializing command " +
115 cmd.getFullName(), ae);
116 }
117 if (getLogger().isDebugEnabled()) {
118 getLogger().logDebug("Command " + cmd.getFullName() +
119 ": disabled");
120 }
121 }
122
123 }
124
125 /**
126 * Adds a map of command definitions to factory. Map keys are command's names and
127 * values are command's instances.
128 *
129 * @param cmds Map map of command instances.
130 */
131 protected final void addCommands(Map cmds) {
132 Iterator cmdsIterator = cmds.values().iterator();
133 while (cmdsIterator.hasNext()) {
134 Command cmd = (Command) cmdsIterator.next();
135 addCommand(cmd.getFullName(), cmd);
136 }
137 }
138
139 }