1 package com.coldcore.coloradoftp.command.impl; 2 3 import com.coldcore.coloradoftp.command.Command; 4 import com.coldcore.coloradoftp.command.CommandFactory; 5 import com.coldcore.coloradoftp.factory.ObjectFactory; 6 import com.coldcore.coloradoftp.factory.ObjectName; 7 import org.apache.log4j.Logger; 8 9 import java.util.HashMap ; 10 import java.util.HashSet ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 17 public class GenericCommandFactory implements CommandFactory { 18 19 private static Logger log = Logger.getLogger(GenericCommandFactory.class); 20 protected Map <String ,String > map; 21 22 23 public GenericCommandFactory(Map <String ,String > commandsRef) { 24 map = new HashMap <String ,String >(commandsRef); 25 } 26 27 28 public Set <String > listNames() { 29 return new HashSet <String >(map.keySet()); 30 } 31 32 33 public Command create(String input) { 34 String name; 35 String parameter; 36 try { 37 input = input.trim(); 39 int i = input.indexOf(" "); 40 if (i == -1) i = input.length(); 41 name = input.substring(0,i).trim().toUpperCase(); 42 parameter = input.substring(i).trim(); 43 } catch (Throwable e) { 44 log.warn("Failed to process input: "+input); 45 return (Command) ObjectFactory.getObject(ObjectName.COMMAND_SYNTAX_ERROR); 46 } 47 48 Command command; 49 String ref = map.get(name); 50 if (ref != null) { 51 command = (Command) ObjectFactory.getObject(ref); 52 command.setName(name); 53 command.setParameter(parameter); 54 } else { 55 log.warn("Command "+name+" not implemented"); 56 command = (Command) ObjectFactory.getObject(ObjectName.COMMAND_NOT_IMPLEMENTED); 57 } 58 return command; 59 } 60 } 61 | Popular Tags |