src/org/sonews/daemon/CommandSelector.java
changeset 75 41d6c0cac8b3
parent 35 ed84c8bdd87b
     1.1 --- a/src/org/sonews/daemon/CommandSelector.java	Sun Aug 29 17:28:58 2010 +0200
     1.2 +++ b/src/org/sonews/daemon/CommandSelector.java	Thu Oct 13 03:09:22 2011 +0200
     1.3 @@ -35,107 +35,84 @@
     1.4  public class CommandSelector
     1.5  {
     1.6  
     1.7 -  private static Map<Thread, CommandSelector> instances
     1.8 -    = new ConcurrentHashMap<Thread, CommandSelector>();
     1.9 -  private static Map<String, Class<?>> commandClassesMapping
    1.10 -    = new ConcurrentHashMap<String, Class<?>>();
    1.11 +	private static Map<Thread, CommandSelector> instances = new ConcurrentHashMap<Thread, CommandSelector>();
    1.12 +	private static Map<String, Class<?>> commandClassesMapping = new ConcurrentHashMap<String, Class<?>>();
    1.13  
    1.14 -  static
    1.15 -  {
    1.16 -    String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
    1.17 -    for(String className : classes)
    1.18 -    {
    1.19 -      if(className.charAt(0) == '#')
    1.20 -      {
    1.21 -        // Skip comments
    1.22 -        continue;
    1.23 -      }
    1.24 +	static {
    1.25 +		String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
    1.26 +		for (String className : classes) {
    1.27 +			if (className.charAt(0) == '#') {
    1.28 +				// Skip comments
    1.29 +				continue;
    1.30 +			}
    1.31  
    1.32 -      try
    1.33 -      {
    1.34 -        addCommandHandler(className);
    1.35 -      }
    1.36 -      catch(ClassNotFoundException ex)
    1.37 -      {
    1.38 -        Log.get().warning("Could not load command class: " + ex);
    1.39 -      }
    1.40 -      catch(InstantiationException ex)
    1.41 -      {
    1.42 -        Log.get().severe("Could not instantiate command class: " + ex);
    1.43 -      }
    1.44 -      catch(IllegalAccessException ex)
    1.45 -      {
    1.46 -        Log.get().severe("Could not access command class: " + ex);
    1.47 -      }
    1.48 -    }
    1.49 -  }
    1.50 +			try {
    1.51 +				addCommandHandler(className);
    1.52 +			} catch (ClassNotFoundException ex) {
    1.53 +				Log.get().warning("Could not load command class: " + ex);
    1.54 +			} catch (InstantiationException ex) {
    1.55 +				Log.get().severe("Could not instantiate command class: " + ex);
    1.56 +			} catch (IllegalAccessException ex) {
    1.57 +				Log.get().severe("Could not access command class: " + ex);
    1.58 +			}
    1.59 +		}
    1.60 +	}
    1.61  
    1.62 -  public static void addCommandHandler(String className)
    1.63 -    throws ClassNotFoundException, InstantiationException, IllegalAccessException
    1.64 -  {
    1.65 -    Class<?> clazz = Class.forName(className);
    1.66 -    Command cmd = (Command)clazz.newInstance();
    1.67 -    String[] cmdStrs = cmd.getSupportedCommandStrings();
    1.68 -    for (String cmdStr : cmdStrs)
    1.69 -    {
    1.70 -      commandClassesMapping.put(cmdStr, clazz);
    1.71 -    }
    1.72 -  }
    1.73 +	public static void addCommandHandler(String className)
    1.74 +		throws ClassNotFoundException, InstantiationException,
    1.75 +		IllegalAccessException
    1.76 +	{
    1.77 +		Class<?> clazz = Class.forName(className);
    1.78 +		Command cmd = (Command) clazz.newInstance();
    1.79 +		String[] cmdStrs = cmd.getSupportedCommandStrings();
    1.80 +		for (String cmdStr : cmdStrs) {
    1.81 +			commandClassesMapping.put(cmdStr, clazz);
    1.82 +		}
    1.83 +	}
    1.84  
    1.85 -  public static Set<String> getCommandNames()
    1.86 -  {
    1.87 -    return commandClassesMapping.keySet();
    1.88 -  }
    1.89 +	public static Set<String> getCommandNames()
    1.90 +	{
    1.91 +		return commandClassesMapping.keySet();
    1.92 +	}
    1.93  
    1.94 -  public static CommandSelector getInstance()
    1.95 -  {
    1.96 -    CommandSelector csel = instances.get(Thread.currentThread());
    1.97 -    if(csel == null)
    1.98 -    {
    1.99 -      csel = new CommandSelector();
   1.100 -      instances.put(Thread.currentThread(), csel);
   1.101 -    }
   1.102 -    return csel;
   1.103 -  }
   1.104 +	public static CommandSelector getInstance()
   1.105 +	{
   1.106 +		CommandSelector csel = instances.get(Thread.currentThread());
   1.107 +		if (csel == null) {
   1.108 +			csel = new CommandSelector();
   1.109 +			instances.put(Thread.currentThread(), csel);
   1.110 +		}
   1.111 +		return csel;
   1.112 +	}
   1.113 +	private Map<String, Command> commandMapping = new HashMap<String, Command>();
   1.114 +	private Command unsupportedCmd = new UnsupportedCommand();
   1.115  
   1.116 -  private Map<String, Command> commandMapping = new HashMap<String, Command>();
   1.117 -  private Command              unsupportedCmd = new UnsupportedCommand();
   1.118 +	private CommandSelector()
   1.119 +	{
   1.120 +	}
   1.121  
   1.122 -  private CommandSelector()
   1.123 -  {}
   1.124 +	public Command get(String commandName)
   1.125 +	{
   1.126 +		try {
   1.127 +			commandName = commandName.toUpperCase();
   1.128 +			Command cmd = this.commandMapping.get(commandName);
   1.129  
   1.130 -  public Command get(String commandName)
   1.131 -  {
   1.132 -    try
   1.133 -    {
   1.134 -      commandName = commandName.toUpperCase();
   1.135 -      Command cmd = this.commandMapping.get(commandName);
   1.136 +			if (cmd == null) {
   1.137 +				Class<?> clazz = commandClassesMapping.get(commandName);
   1.138 +				if (clazz == null) {
   1.139 +					cmd = this.unsupportedCmd;
   1.140 +				} else {
   1.141 +					cmd = (Command) clazz.newInstance();
   1.142 +					this.commandMapping.put(commandName, cmd);
   1.143 +				}
   1.144 +			} else if (cmd.isStateful()) {
   1.145 +				cmd = cmd.getClass().newInstance();
   1.146 +			}
   1.147  
   1.148 -      if(cmd == null)
   1.149 -      {
   1.150 -        Class<?> clazz = commandClassesMapping.get(commandName);
   1.151 -        if(clazz == null)
   1.152 -        {
   1.153 -          cmd = this.unsupportedCmd;
   1.154 -        }
   1.155 -        else
   1.156 -        {
   1.157 -          cmd = (Command)clazz.newInstance();
   1.158 -          this.commandMapping.put(commandName, cmd);
   1.159 -        }
   1.160 -      }
   1.161 -      else if(cmd.isStateful())
   1.162 -      {
   1.163 -        cmd = cmd.getClass().newInstance();
   1.164 -      }
   1.165 -
   1.166 -      return cmd;
   1.167 -    }
   1.168 -    catch(Exception ex)
   1.169 -    {
   1.170 -      ex.printStackTrace();
   1.171 -      return this.unsupportedCmd;
   1.172 -    }
   1.173 -  }
   1.174 -
   1.175 +			return cmd;
   1.176 +		} catch (Exception ex) {
   1.177 +			ex.printStackTrace();
   1.178 +			return this.unsupportedCmd;
   1.179 +		}
   1.180 +	}
   1.181  }