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