org/sonews/daemon/CommandSelector.java
changeset 35 ed84c8bdd87b
parent 34 9f0b95aafaa3
child 36 c404a87db5b7
     1.1 --- a/org/sonews/daemon/CommandSelector.java	Sun Aug 29 17:04:25 2010 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,141 +0,0 @@
     1.4 -/*
     1.5 - *   SONEWS News Server
     1.6 - *   see AUTHORS for the list of contributors
     1.7 - *
     1.8 - *   This program is free software: you can redistribute it and/or modify
     1.9 - *   it under the terms of the GNU General Public License as published by
    1.10 - *   the Free Software Foundation, either version 3 of the License, or
    1.11 - *   (at your option) any later version.
    1.12 - *
    1.13 - *   This program is distributed in the hope that it will be useful,
    1.14 - *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 - *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 - *   GNU General Public License for more details.
    1.17 - *
    1.18 - *   You should have received a copy of the GNU General Public License
    1.19 - *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 - */
    1.21 -
    1.22 -package org.sonews.daemon;
    1.23 -
    1.24 -import java.util.HashMap;
    1.25 -import java.util.Map;
    1.26 -import java.util.Set;
    1.27 -import java.util.concurrent.ConcurrentHashMap;
    1.28 -import org.sonews.daemon.command.Command;
    1.29 -import org.sonews.daemon.command.UnsupportedCommand;
    1.30 -import org.sonews.util.Log;
    1.31 -import org.sonews.util.io.Resource;
    1.32 -
    1.33 -/**
    1.34 - * Selects the correct command processing class.
    1.35 - * @author Christian Lins
    1.36 - * @since sonews/1.0
    1.37 - */
    1.38 -public class CommandSelector
    1.39 -{
    1.40 -
    1.41 -  private static Map<Thread, CommandSelector> instances
    1.42 -    = new ConcurrentHashMap<Thread, CommandSelector>();
    1.43 -  private static Map<String, Class<?>> commandClassesMapping
    1.44 -    = new ConcurrentHashMap<String, Class<?>>();
    1.45 -
    1.46 -  static
    1.47 -  {
    1.48 -    String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
    1.49 -    for(String className : classes)
    1.50 -    {
    1.51 -      if(className.charAt(0) == '#')
    1.52 -      {
    1.53 -        // Skip comments
    1.54 -        continue;
    1.55 -      }
    1.56 -
    1.57 -      try
    1.58 -      {
    1.59 -        addCommandHandler(className);
    1.60 -      }
    1.61 -      catch(ClassNotFoundException ex)
    1.62 -      {
    1.63 -        Log.get().warning("Could not load command class: " + ex);
    1.64 -      }
    1.65 -      catch(InstantiationException ex)
    1.66 -      {
    1.67 -        Log.get().severe("Could not instantiate command class: " + ex);
    1.68 -      }
    1.69 -      catch(IllegalAccessException ex)
    1.70 -      {
    1.71 -        Log.get().severe("Could not access command class: " + ex);
    1.72 -      }
    1.73 -    }
    1.74 -  }
    1.75 -
    1.76 -  public static void addCommandHandler(String className)
    1.77 -    throws ClassNotFoundException, InstantiationException, IllegalAccessException
    1.78 -  {
    1.79 -    Class<?> clazz = Class.forName(className);
    1.80 -    Command cmd = (Command)clazz.newInstance();
    1.81 -    String[] cmdStrs = cmd.getSupportedCommandStrings();
    1.82 -    for (String cmdStr : cmdStrs)
    1.83 -    {
    1.84 -      commandClassesMapping.put(cmdStr, clazz);
    1.85 -    }
    1.86 -  }
    1.87 -
    1.88 -  public static Set<String> getCommandNames()
    1.89 -  {
    1.90 -    return commandClassesMapping.keySet();
    1.91 -  }
    1.92 -
    1.93 -  public static CommandSelector getInstance()
    1.94 -  {
    1.95 -    CommandSelector csel = instances.get(Thread.currentThread());
    1.96 -    if(csel == null)
    1.97 -    {
    1.98 -      csel = new CommandSelector();
    1.99 -      instances.put(Thread.currentThread(), csel);
   1.100 -    }
   1.101 -    return csel;
   1.102 -  }
   1.103 -
   1.104 -  private Map<String, Command> commandMapping = new HashMap<String, Command>();
   1.105 -  private Command              unsupportedCmd = new UnsupportedCommand();
   1.106 -
   1.107 -  private CommandSelector()
   1.108 -  {}
   1.109 -
   1.110 -  public Command get(String commandName)
   1.111 -  {
   1.112 -    try
   1.113 -    {
   1.114 -      commandName = commandName.toUpperCase();
   1.115 -      Command cmd = this.commandMapping.get(commandName);
   1.116 -
   1.117 -      if(cmd == null)
   1.118 -      {
   1.119 -        Class<?> clazz = commandClassesMapping.get(commandName);
   1.120 -        if(clazz == null)
   1.121 -        {
   1.122 -          cmd = this.unsupportedCmd;
   1.123 -        }
   1.124 -        else
   1.125 -        {
   1.126 -          cmd = (Command)clazz.newInstance();
   1.127 -          this.commandMapping.put(commandName, cmd);
   1.128 -        }
   1.129 -      }
   1.130 -      else if(cmd.isStateful())
   1.131 -      {
   1.132 -        cmd = cmd.getClass().newInstance();
   1.133 -      }
   1.134 -
   1.135 -      return cmd;
   1.136 -    }
   1.137 -    catch(Exception ex)
   1.138 -    {
   1.139 -      ex.printStackTrace();
   1.140 -      return this.unsupportedCmd;
   1.141 -    }
   1.142 -  }
   1.143 -
   1.144 -}