org/sonews/daemon/CommandSelector.java
author chris <chris@marvin>
Wed Jul 22 14:04:05 2009 +0200 (2009-07-22)
changeset 3 2fdc9cc89502
child 10 cb071b654cf7
permissions -rw-r--r--
sonews/1.0.0
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.daemon;
chris@3
    20
chris@3
    21
import java.util.HashMap;
chris@3
    22
import java.util.Map;
chris@3
    23
import java.util.concurrent.ConcurrentHashMap;
chris@3
    24
import org.sonews.daemon.command.Command;
chris@3
    25
import org.sonews.daemon.command.UnsupportedCommand;
chris@3
    26
import org.sonews.util.Log;
chris@3
    27
import org.sonews.util.io.Resource;
chris@3
    28
chris@3
    29
/**
chris@3
    30
 * Selects the correct command processing class.
chris@3
    31
 * @author Christian Lins
chris@3
    32
 */
chris@3
    33
class CommandSelector
chris@3
    34
{
chris@3
    35
chris@3
    36
  private static Map<Thread, CommandSelector> instances
chris@3
    37
    = new ConcurrentHashMap<Thread, CommandSelector>();
chris@3
    38
  
chris@3
    39
  public static CommandSelector getInstance()
chris@3
    40
  {
chris@3
    41
    CommandSelector csel = instances.get(Thread.currentThread());
chris@3
    42
    if(csel == null)
chris@3
    43
    {
chris@3
    44
      csel = new CommandSelector();
chris@3
    45
      instances.put(Thread.currentThread(), csel);
chris@3
    46
    }
chris@3
    47
    return csel;
chris@3
    48
  }
chris@3
    49
chris@3
    50
  private Map<String, Command> commandMapping = new HashMap<String, Command>();
chris@3
    51
  private Command              unsupportedCmd = new UnsupportedCommand();
chris@3
    52
chris@3
    53
  private CommandSelector()
chris@3
    54
  {
chris@3
    55
    String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
chris@3
    56
    for(String className : classes)
chris@3
    57
    {
chris@3
    58
      try
chris@3
    59
      {
chris@3
    60
        Class<?> clazz   = Class.forName(className);
chris@3
    61
        Command  cmd     = (Command)clazz.newInstance();
chris@3
    62
        String[] cmdStrs = cmd.getSupportedCommandStrings();
chris@3
    63
        for(String cmdStr : cmdStrs)
chris@3
    64
        {
chris@3
    65
          this.commandMapping.put(cmdStr, cmd);
chris@3
    66
        }
chris@3
    67
      }
chris@3
    68
      catch(ClassNotFoundException ex)
chris@3
    69
      {
chris@3
    70
        Log.msg("Could not load command class: " + ex, false);
chris@3
    71
      }
chris@3
    72
      catch(InstantiationException ex)
chris@3
    73
      {
chris@3
    74
        Log.msg("Could not instantiate command class: " + ex, false);
chris@3
    75
      }
chris@3
    76
      catch(IllegalAccessException ex)
chris@3
    77
      {
chris@3
    78
        Log.msg("Could not access command class: " + ex, false);
chris@3
    79
      }
chris@3
    80
    }
chris@3
    81
  }
chris@3
    82
chris@3
    83
  public Command get(String commandName)
chris@3
    84
  {
chris@3
    85
    try
chris@3
    86
    {
chris@3
    87
      commandName = commandName.toUpperCase();
chris@3
    88
      Command cmd = this.commandMapping.get(commandName);
chris@3
    89
chris@3
    90
      if(cmd == null)
chris@3
    91
      {
chris@3
    92
        return this.unsupportedCmd;
chris@3
    93
      }
chris@3
    94
      else if(cmd.isStateful())
chris@3
    95
      {
chris@3
    96
        return cmd.getClass().newInstance();
chris@3
    97
      }
chris@3
    98
      else
chris@3
    99
      {
chris@3
   100
        return cmd;
chris@3
   101
      }
chris@3
   102
    }
chris@3
   103
    catch(Exception ex)
chris@3
   104
    {
chris@3
   105
      ex.printStackTrace();
chris@3
   106
      return this.unsupportedCmd;
chris@3
   107
    }
chris@3
   108
  }
chris@3
   109
chris@3
   110
}