commandfactory.java
来自「电信的网厅的整站代码」· Java 代码 · 共 106 行
JAVA
106 行
package com.doone.wskfmgr.common.command;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.doone.fj1w.common.PropertiesExtra;
import com.doone.util.FileLogger;
/**
* A singleton class that creates commands using the command mapping property
* file.
*
* @pattern FrontController (role=commandFactoryClass)
*
* @generatedBy CodePro at 07-1-30 H9:22
*
* @author wushaol
*
* @version $Revision$
*/
class CommandFactory {
/**
* The name of the property file mapping command names to classes.
*/
private static final java.lang.String COMMAND_MAPPING_PROPERTY_FILE = "commandMapping.properties";
/**
* Mapping of command name to command class.
*/
private static Map commands;
/**
* Construct an instance.
*/
private CommandFactory() {
}
/**
* Create the map of command names to command classes.
*/
private static void createCommandMap() {
Map commandMap = new HashMap();
try {
PropertiesExtra extra = new PropertiesExtra(COMMAND_MAPPING_PROPERTY_FILE);
Properties properties = extra.getProperties();
for (Enumeration e = properties.keys(); e.hasMoreElements();) {
String action = (String) e.nextElement();
Class newCommandClass = Class.forName(properties
.getProperty(action));
commandMap.put(action, newCommandClass);
}
} catch (Exception e) {
FileLogger.getLogger().warn(
"初始化命今模式失败,命今工厂将无法工作,请查询原因: " + e.getMessage(), e);
}
setCommands(commandMap);
}
/**
* Get the map from command names to command classes.
*
* @return the map from command names to command classes
*/
private synchronized static Map getCommands() {
if (commands == null) {
createCommandMap();
}
return commands;
}
/**
* Set the map from command names to command classes.
*
* @return commandMap the new map from command names to command classes
*/
private static void setCommands(Map commandMap) {
commands = commandMap;
}
/**
* Create a RegistCommandInterface instance for the specified action or
* command.
*
* @param action
* the name of the command
*
* @return RegistCommandInterface or <code>null</code> if the command
* could not be created
*/
public static CommandInterface getCommand(String action) {
Class newCommandClass = (Class) getCommands().get(action);
CommandInterface newCommand = null;
try {
newCommand = (CommandInterface) newCommandClass.newInstance();
} catch (Exception e) {
FileLogger.getLogger().warn(
"生成" + action + "命今操作失败: " + e.getMessage(), e);
}
return newCommand;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?