org/sonews/daemon/CommandSelector.java
changeset 7 0b76e099eb96
child 10 cb071b654cf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/CommandSelector.java	Wed Aug 12 13:03:23 2009 +0200
     1.3 @@ -0,0 +1,110 @@
     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.concurrent.ConcurrentHashMap;
    1.27 +import org.sonews.daemon.command.Command;
    1.28 +import org.sonews.daemon.command.UnsupportedCommand;
    1.29 +import org.sonews.util.Log;
    1.30 +import org.sonews.util.io.Resource;
    1.31 +
    1.32 +/**
    1.33 + * Selects the correct command processing class.
    1.34 + * @author Christian Lins
    1.35 + */
    1.36 +class CommandSelector
    1.37 +{
    1.38 +
    1.39 +  private static Map<Thread, CommandSelector> instances
    1.40 +    = new ConcurrentHashMap<Thread, CommandSelector>();
    1.41 +  
    1.42 +  public static CommandSelector getInstance()
    1.43 +  {
    1.44 +    CommandSelector csel = instances.get(Thread.currentThread());
    1.45 +    if(csel == null)
    1.46 +    {
    1.47 +      csel = new CommandSelector();
    1.48 +      instances.put(Thread.currentThread(), csel);
    1.49 +    }
    1.50 +    return csel;
    1.51 +  }
    1.52 +
    1.53 +  private Map<String, Command> commandMapping = new HashMap<String, Command>();
    1.54 +  private Command              unsupportedCmd = new UnsupportedCommand();
    1.55 +
    1.56 +  private CommandSelector()
    1.57 +  {
    1.58 +    String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
    1.59 +    for(String className : classes)
    1.60 +    {
    1.61 +      try
    1.62 +      {
    1.63 +        Class<?> clazz   = Class.forName(className);
    1.64 +        Command  cmd     = (Command)clazz.newInstance();
    1.65 +        String[] cmdStrs = cmd.getSupportedCommandStrings();
    1.66 +        for(String cmdStr : cmdStrs)
    1.67 +        {
    1.68 +          this.commandMapping.put(cmdStr, cmd);
    1.69 +        }
    1.70 +      }
    1.71 +      catch(ClassNotFoundException ex)
    1.72 +      {
    1.73 +        Log.msg("Could not load command class: " + ex, false);
    1.74 +      }
    1.75 +      catch(InstantiationException ex)
    1.76 +      {
    1.77 +        Log.msg("Could not instantiate command class: " + ex, false);
    1.78 +      }
    1.79 +      catch(IllegalAccessException ex)
    1.80 +      {
    1.81 +        Log.msg("Could not access command class: " + ex, false);
    1.82 +      }
    1.83 +    }
    1.84 +  }
    1.85 +
    1.86 +  public Command get(String commandName)
    1.87 +  {
    1.88 +    try
    1.89 +    {
    1.90 +      commandName = commandName.toUpperCase();
    1.91 +      Command cmd = this.commandMapping.get(commandName);
    1.92 +
    1.93 +      if(cmd == null)
    1.94 +      {
    1.95 +        return this.unsupportedCmd;
    1.96 +      }
    1.97 +      else if(cmd.isStateful())
    1.98 +      {
    1.99 +        return cmd.getClass().newInstance();
   1.100 +      }
   1.101 +      else
   1.102 +      {
   1.103 +        return cmd;
   1.104 +      }
   1.105 +    }
   1.106 +    catch(Exception ex)
   1.107 +    {
   1.108 +      ex.printStackTrace();
   1.109 +      return this.unsupportedCmd;
   1.110 +    }
   1.111 +  }
   1.112 +
   1.113 +}