1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.server.command;
22
23 import java.util.Vector;
24
25 import autohit.common.channels.Receipt;
26 import autohit.server.ServerException;
27 import autohit.server.SystemContext;
28
29 /**
30 * A LOCAL Command server. This one expects to be on the same system as the
31 * issuing agent (though you may be able to cheat this.) It will cache the
32 * command registry on load, so you'll need to dispose it and create a new one,
33 * if you want to capture changes. It will use the root logger as the response
34 * channel.
35 *
36 * @author Erich P. Gatejen
37 * @version 1.0 <i>Version History</i><code>EPG - Initial - 25Jul03 </code>
38 */
39 public class CommandServerLocal implements CommandServer {
40
41 private CommandRegistry commandRegistry;
42 //private Hashtable commandCache; // Command cache not supported now
43 private SystemContext sc = null;
44
45 public final static String RESPOND_FAILED = "FAILED";
46
47 /**
48 * Initialize. You can call this as often as you want, but must be called
49 * at least once.
50 *
51 * @param c
52 * the SystemContext
53 * @throws ServerException
54 */
55 public void init(SystemContext c) throws ServerException {
56 commandRegistry = c.getCommandRegistry();
57 if (commandRegistry == null)
58 throw new ServerException(
59 "CommandServerLocal unable to get a CommandRegistry. Unable to finish initialization and is invalid.",
60 ServerException.CODE_COMMAND_REGISTRY_FAULT);
61
62 // Always make this last. The sc is how we tell if this server is
63 // valid.
64 sc = c;
65 }
66
67 /**
68 * Execute a command. Use default channel for response. If a response
69 * injector is not specified in the CommandAtom, we'll use the
70 * SystemContext root logger for the response channel.
71 *
72 * @param cmd
73 * is a command atom
74 * @throws ServerException
75 * @return printable string of some form. not defined by the interface.
76 */
77 public String execute(CommandAtom cmd) throws ServerException {
78
79 String result = RESPOND_FAILED;
80 if (sc == null)
81 throw new ServerException(
82 "CommandServerLocal not initialized before execute() called",
83 ServerException.CODE_SW_DETECTED_FAULT);
84
85 // Trap any spurious exceptions
86 try {
87
88 // instantiate it
89 Command co = (Command) commandRegistry.instance(cmd.numeric);
90
91 // Make sure it has a command list
92 Receipt rr;
93 if (cmd.thing instanceof Vector) {
94 rr = co.call(sc, (Vector) cmd.thing);
95
96 } else {
97 throw new ServerException(
98 "CommandServerLocal.execute() failed to start command, since the command did not have a command list (Vector).",
99 ServerException.CODE_COMMAND_ERROR);
100 }
101
102 // if the result is acceptable, return it
103 if (rr != null) {
104 result = rr.toString();
105 }
106
107 } catch (ServerException se) {
108 throw se;
109 } catch (Exception ee) {
110 throw new ServerException(
111 "CommandServerLocal.execute() failed to gross exception. message=" + ee.getMessage(),
112 ServerException.CODE_COMMAND_FAULT);
113 }
114 return result;
115 }
116
117 }
|