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.io.InputStream;
24
25 import org.apache.commons.collections.ExtendedProperties;
26
27 import autohit.common.Constants;
28 import autohit.server.ServerException;
29
30 /**
31 * Command registry implementation. This is immutable. Once created it is
32 * set. If you need to catch changes in the registry, you need to create
33 * a new instance. Using the default constructor will cause an exception!
34 * <p>
35 * This version of the registry will implement it as a ExtendedProperties
36 * set.
37 * <p>
38 * The registry has two sections. The first section associates the commands
39 * with numerics. There may ne NO duplicate numerics. (If there are, the system
40 * state is undefined.) This section is constructed as follows:<p>
41 * <code>
42 * command.1=compile
43 * command.2=dump
44 * <br>
45 * ("command.")(numeric)("=")(string name)</code><p>
46 * The second section contains entries for the commands defined in section one.
47 * This section is as follows:<p>
48 * <code>
49 * compile.class=autohit.server.command.CommandCompile
50 * compile.help=This will compile a script found at the target.
51 * <br>
52 * (string name)(".class=")(class name for implementation)
53 * (string name)(".help=")(string giving a hint about the command)</code><p>
54 *
55 * @author Erich P. Gatejen
56 * @version 1.0
57 * <i>Version History</i>
58 * <code>EPG - Initial - 25Jul03</code>
59 *
60 */
61 public class CommandRegistry extends ExtendedProperties {
62
63 final static long serialVersionUID = 1;
64
65 /**
66 * field literals
67 */
68 public final static String FL_COMMAND = "command.";
69 public final static String FL_CLASS = ".class";
70 public final static String FL_HELP = ".help";
71
72 /**
73 * Default Constructor. Don't use!
74 * @throws ServerException
75 */
76 public CommandRegistry() throws ServerException {
77 throw new ServerException(
78 "Programmer used default constructor for CommandRegsitry. this is a bug.",
79 ServerException.CODE_SW_DETECTED_FAULT);
80 }
81
82 /**
83 * Constructor to create and load from an input stream.
84 * @param is input stream from where to read the registry
85 * @throws ServerException
86 */
87 public CommandRegistry(InputStream is) throws ServerException {
88 super();
89 try {
90 load(is);
91 } catch (Exception e) {
92 throw new ServerException(
93 "Failed to load the command registry. message="
94 + e.getMessage(),
95 ServerException.CODE_COMMAND_REGISTRY_FAULT);
96 }
97 }
98
99 /**
100 * Get an instance of the command specified by the numeric
101 * @param numeric numeric for the command
102 * @throws ServerException
103 * @return the object instance.
104 */
105 public Object instance(int numeric) throws ServerException {
106
107 String name = Constants.UNKNOWN;
108 Command target = null;
109 String classname = Constants.UNKNOWN;
110
111 try {
112 // See if the atom points to a valid command
113 String numericS = FL_COMMAND + Integer.toString(numeric);
114 if (this.containsKey(numericS)) {
115
116 // Find the name
117 name = this.getString(numericS);
118 classname = this.getString(name + FL_CLASS);
119
120 // create the instance
121 Class t = Class.forName(classname);
122 target = (Command) t.newInstance();
123
124 } else {
125 throw new ServerException(
126 "Command Registry does not have requested command. Command ignored. Command numeric="
127 + numeric,
128 ServerException.CODE_COMMAND_UNKNOWN);
129 }
130
131 } catch (ClassCastException cee) {
132 throw new ServerException(
133 "Command Registry instance() failed because of malformed '.class' entry for "
134 + name + ". numeric=" + numeric,
135 ServerException.CODE_COMMAND_REGISTRY_FAULT, cee);
136 } catch (ClassNotFoundException cne) {
137 throw new ServerException(
138 "Command Registry instance() failed because of malformed '.class' entry for "
139 + name
140 + ". ClassNotFound for entry=["
141 + classname
142 + "].",
143 ServerException.CODE_COMMAND_REGISTRY_FAULT,
144 cne);
145 } catch (ServerException se) {
146 throw se;
147 } catch (Exception ee) {
148 throw new ServerException(
149 "Command Registry instance() failed to gross exception. message="
150 + ee.getMessage(),
151 ServerException.CODE_COMMAND_REGISTRY_FAULT,
152 ee);
153 }
154 return target;
155 }
156 }
|