<dependencies> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.3</version> <scope>runtime</scope> </dependency> ・・・
<?xml version="1.0" encoding="UTF-8"?>
<msg-app>
<!-- application scope variables -->
<context-param>
<user-name>root</user-name>
<nick-name>root</nick-name>
<group>Msglet</group>
<debug>true</debug>
</context-param>
<!-- msglet settings -->
<msglet>
<msglet-name>Sanmoku</msglet-name>
<msglet-class>com.snail.msglet.sample.SampleMsglet</msglet-class>
<init-param>
<param-name>width</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>3</param-value>
</init-param>
</msglet>
<msglet>
<msglet-name>Gomoku</msglet-name>
<msglet-class>com.snail.msglet.sample.SampleMsglet</msglet-class>
<init-param>
<param-name>width</param-name>
<param-value>5</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>5</param-value>
</init-param>
</msglet>
<!-- startup msglet -->
<welcome-msglet>Sanmoku</welcome-msglet>
</msg-app>
public final class Startup {
private static final String USER_NAME = "context-param.user-name";
private static final String NICK_NAME = "context-param.nick-name";
private static final String GROUP = "context-param.group";
private static final String DEBUG = "context-param.debug";
private static final String WELCOME_MSGLET = "welcome-msglet";
public static void main(final String[] args) {
MsgletContainer container = null;
try {
XMLConfiguration config = new XMLConfiguration("msg.xml");
String userName = config.getString(USER_NAME, "Msglet");
String nickName = config.getString(NICK_NAME, "Msglet");
String group = config.getString(GROUP, "Msglet");
boolean debug = config.getBoolean(DEBUG, false);
String welcome = config.getString(WELCOME_MSGLET);
container = new MsgletContainer(
userName,
nickName,
group,
debug,
welcome);
container.setMsgletMap(MsgletFactory.createMsglets(config));
container.login();
container.start();
waitTerminate();
} catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
} finally {
if (container != null) {
try {
container.logout();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 空文字か、"shutodwn"が入力されるまで入力待ちをします .
*/
private static void waitTerminate() {
BufferedReader breader =
new BufferedReader(new InputStreamReader(System.in));
try {
String line = null;
while ((line = breader.readLine()) != null) {
if ("shutdown".equals(line)) {
break;
}
}
} catch (IOException ignoreEx) {
ignoreEx.printStackTrace();
}
}
}
・・・ <msglet> <msglet-name>a-msglet</msglet-name> <msglet-class>com.snail.AMsglet</msglet-class> </msglet> <msglet> <msglet-name>b-msglet</msglet-name> <msglet-class>com.snail.BMsglet</msglet-class> </msglet> ・・・
name[0] = config.getString("msglet.msglet-name"); class[0] = config.getString("msglet.msglet-class");
for(int n = 0 ; n < repetition ; n++){ name[n] = config.getString("msglet(n).msglet-name"); class[n] = config.getString("msglet(n).msglet-class"); }
public final class MsgletFactory {
private static final String MSGLET = "msglet";
private static final String MSGLET_NAME = "msglet-name";
private static final String MSGLET_CLASS = "msglet-class";
private static final String INIT_PARAM = "init-param";
private static final String PARAM_NAME = "param-name";
private static final String PARAM_VALUE = "param-value";
public static Map<String, Msglet> createMsglets(
final XMLConfiguration config)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, ConfigurationException {
Map<String, Msglet> retMap = new HashMap<String, Msglet>();
// necessary tag
String pibot = MSGLET + "." + MSGLET_NAME;
int repetition = getRepetition(config, pibot);
if (repetition == 1) {
String name = config.getString(MSGLET + "." + MSGLET_NAME);
Msglet msglet =
createMsglet(config.getString(MSGLET + "." + MSGLET_CLASS));
msglet.setInitParamMap(
createInitParams(MSGLET + "." + INIT_PARAM, config));
retMap.put(name, msglet);
} else if (repetition > 1) {
for (int cnt = 0; cnt < repetition; cnt++) {
String name = config.getString(MSGLET + "(" + cnt + ")." + MSGLET_NAME);
Msglet msglet =
createMsglet(
config.getString(MSGLET + "(" + cnt + ")." + MSGLET_CLASS));
msglet.setInitParamMap(
createInitParams(MSGLET + "(" + cnt + ")." + INIT_PARAM, config));
retMap.put(name, msglet);
}
}
return retMap;
}
・・・
private static int getRepetition(
final XMLConfiguration config,
final String path) {
Object obj = config.getProperty(path);
if (obj == null) {
return 0;
} else if (obj instanceof Collection) {
return ((Collection) obj).size();
}
return 1;
}
}