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