org/sonews/daemon/CommandSelector.java
author cli
Mon Aug 24 13:00:05 2009 +0200 (2009-08-24)
changeset 20 6ae5e4f8329b
parent 10 cb071b654cf7
child 21 4b2c8bedb094
permissions -rw-r--r--
Add stubs for org.sonews.acl and add method "impliedCapability" for org.sonews.command.Command interface.
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
    {
cli@10
    58
      if(className.charAt(0) == '#')
cli@10
    59
      {
cli@10
    60
        // Skip comments
cli@10
    61
        continue;
cli@10
    62
      }
cli@10
    63
chris@3
    64
      try
chris@3
    65
      {
chris@3
    66
        Class<?> clazz   = Class.forName(className);
chris@3
    67
        Command  cmd     = (Command)clazz.newInstance();
chris@3
    68
        String[] cmdStrs = cmd.getSupportedCommandStrings();
chris@3
    69
        for(String cmdStr : cmdStrs)
chris@3
    70
        {
chris@3
    71
          this.commandMapping.put(cmdStr, cmd);
chris@3
    72
        }
chris@3
    73
      }
chris@3
    74
      catch(ClassNotFoundException ex)
chris@3
    75
      {
cli@15
    76
        Log.get().warning("Could not load command class: " + ex);
chris@3
    77
      }
chris@3
    78
      catch(InstantiationException ex)
chris@3
    79
      {
cli@15
    80
        Log.get().severe("Could not instantiate command class: " + ex);
chris@3
    81
      }
chris@3
    82
      catch(IllegalAccessException ex)
chris@3
    83
      {
cli@15
    84
        Log.get().severe("Could not access command class: " + ex);
chris@3
    85
      }
chris@3
    86
    }
chris@3
    87
  }
chris@3
    88
chris@3
    89
  public Command get(String commandName)
chris@3
    90
  {
chris@3
    91
    try
chris@3
    92
    {
chris@3
    93
      commandName = commandName.toUpperCase();
chris@3
    94
      Command cmd = this.commandMapping.get(commandName);
chris@3
    95
chris@3
    96
      if(cmd == null)
chris@3
    97
      {
chris@3
    98
        return this.unsupportedCmd;
chris@3
    99
      }
chris@3
   100
      else if(cmd.isStateful())
chris@3
   101
      {
chris@3
   102
        return cmd.getClass().newInstance();
chris@3
   103
      }
chris@3
   104
      else
chris@3
   105
      {
chris@3
   106
        return cmd;
chris@3
   107
      }
chris@3
   108
    }
chris@3
   109
    catch(Exception ex)
chris@3
   110
    {
chris@3
   111
      ex.printStackTrace();
chris@3
   112
      return this.unsupportedCmd;
chris@3
   113
    }
chris@3
   114
  }
chris@3
   115
chris@3
   116
}