01: package org.osbl.persistence;
02:
03: import java.util.*;
04:
05: public class Persistence<C extends Command> {
06: Map<String, Class<? extends C>> commands = new HashMap<String, Class<? extends C>>();
07:
08: public void registerCommand(String name,
09: Class<? extends C> commandClass) {
10: commands.put(name, commandClass);
11: }
12:
13: public void setCommands(Map<String, Class<? extends C>> commands) {
14: this .commands = commands;
15: }
16:
17: public Set<String> getCommandNames() {
18: return commands.keySet();
19: }
20:
21: public C createCommand(String name) {
22: Class<? extends C> commandClass = commands.get(name);
23: if (commandClass == null)
24: throw new RuntimeException("No Command with name " + name);
25:
26: try {
27: C command = commandClass.newInstance();
28: initializeCommand(command);
29: return command;
30: } catch (Exception e) {
31: throw new RuntimeException(e);
32: }
33: }
34:
35: protected void initializeCommand(C command) {
36: }
37: }
|