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