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