org/sonews/daemon/CommandSelector.java
author cli
Wed, 26 Aug 2009 10:55:59 +0200
changeset 23 e4345a26f81f
parent 21 4b2c8bedb094
child 26 407c428adb5b
permissions -rw-r--r--
Proper reply on XDAEMON GROUPADD if group already existing (#551).
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
cli@21
    32
 * @since sonews/1.0
chris@3
    33
 */
cli@21
    34
public class CommandSelector
chris@3
    35
{
chris@3
    36
chris@3
    37
  private static Map<Thread, CommandSelector> instances
chris@3
    38
    = new ConcurrentHashMap<Thread, CommandSelector>();
cli@21
    39
  private static Map<String, Class<?>> commandClassesMapping
cli@21
    40
    = new ConcurrentHashMap<String, Class<?>>();
chris@3
    41
cli@21
    42
  static
chris@3
    43
  {
chris@3
    44
    String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
chris@3
    45
    for(String className : classes)
chris@3
    46
    {
cli@10
    47
      if(className.charAt(0) == '#')
cli@10
    48
      {
cli@10
    49
        // Skip comments
cli@10
    50
        continue;
cli@10
    51
      }
cli@10
    52
chris@3
    53
      try
chris@3
    54
      {
cli@21
    55
        addCommandHandler(className);
chris@3
    56
      }
chris@3
    57
      catch(ClassNotFoundException ex)
chris@3
    58
      {
cli@15
    59
        Log.get().warning("Could not load command class: " + ex);
chris@3
    60
      }
chris@3
    61
      catch(InstantiationException ex)
chris@3
    62
      {
cli@15
    63
        Log.get().severe("Could not instantiate command class: " + ex);
chris@3
    64
      }
chris@3
    65
      catch(IllegalAccessException ex)
chris@3
    66
      {
cli@15
    67
        Log.get().severe("Could not access command class: " + ex);
chris@3
    68
      }
chris@3
    69
    }
chris@3
    70
  }
chris@3
    71
cli@21
    72
  public static void addCommandHandler(String className)
cli@21
    73
    throws ClassNotFoundException, InstantiationException, IllegalAccessException
cli@21
    74
  {
cli@21
    75
    Class<?> clazz = Class.forName(className);
cli@21
    76
    Command cmd = (Command)clazz.newInstance();
cli@21
    77
    String[] cmdStrs = cmd.getSupportedCommandStrings();
cli@21
    78
    for (String cmdStr : cmdStrs)
cli@21
    79
    {
cli@21
    80
      commandClassesMapping.put(cmdStr, clazz);
cli@21
    81
    }
cli@21
    82
  }
cli@21
    83
cli@21
    84
  public static CommandSelector getInstance()
cli@21
    85
  {
cli@21
    86
    CommandSelector csel = instances.get(Thread.currentThread());
cli@21
    87
    if(csel == null)
cli@21
    88
    {
cli@21
    89
      csel = new CommandSelector();
cli@21
    90
      instances.put(Thread.currentThread(), csel);
cli@21
    91
    }
cli@21
    92
    return csel;
cli@21
    93
  }
cli@21
    94
cli@21
    95
  private Map<String, Command> commandMapping = new HashMap<String, Command>();
cli@21
    96
  private Command              unsupportedCmd = new UnsupportedCommand();
cli@21
    97
cli@21
    98
  private CommandSelector()
cli@21
    99
  {}
cli@21
   100
chris@3
   101
  public Command get(String commandName)
chris@3
   102
  {
chris@3
   103
    try
chris@3
   104
    {
chris@3
   105
      commandName = commandName.toUpperCase();
chris@3
   106
      Command cmd = this.commandMapping.get(commandName);
chris@3
   107
chris@3
   108
      if(cmd == null)
chris@3
   109
      {
cli@21
   110
        Class<?> clazz = commandClassesMapping.get(commandName);
cli@21
   111
        if(clazz == null)
cli@21
   112
        {
cli@21
   113
          cmd = this.unsupportedCmd;
cli@21
   114
        }
cli@21
   115
        else
cli@21
   116
        {
cli@21
   117
          cmd = (Command)clazz.newInstance();
cli@21
   118
          this.commandMapping.put(commandName, cmd);
cli@21
   119
        }
chris@3
   120
      }
chris@3
   121
      else if(cmd.isStateful())
chris@3
   122
      {
cli@21
   123
        cmd = cmd.getClass().newInstance();
chris@3
   124
      }
cli@21
   125
cli@21
   126
      return cmd;
chris@3
   127
    }
chris@3
   128
    catch(Exception ex)
chris@3
   129
    {
chris@3
   130
      ex.printStackTrace();
chris@3
   131
      return this.unsupportedCmd;
chris@3
   132
    }
chris@3
   133
  }
chris@3
   134
chris@3
   135
}