Menu

[r45]: / scriptbot / src / java / net / jawe / scriptbot / Main.java  Maximize  Restore  History

Download this file

139 lines (125 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Created on 08.11.2005 by jawe
* $Id$
*/
/*
* This file is part of scriptbot.
*
* scriptbot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* scriptbot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ScriptBot; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.jawe.scriptbot;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.jawe.scriptbot.factory.Factory;
/**
* Startup class for the Scriptbot.<br />
* Maintains a list of bots started throgh this class, that is available to
* every bot instance under the
* {@link net.jawe.scriptbot.Bot#getFromCache(String, String) cache}
* key {@value #BOTS_CACHE_KEY}.
*
* @author jawe
*/
public abstract class Main {
private static final Log LOG = LogFactory.getLog(Main.class);
private static final Map<String, Bot> bots = new HashMap<String, Bot>();
public static final String BOTS_CACHE_KEY = "_bots";
/**
* Starts the bot.
* @param args
*/
public static void main(String[] args) {
try {
if (args.length == 0) {
addBot();
} else {
for (String arg : args) {
if (arg.equals("-h") || arg.equals("--help")) {
printHelp();
break;
} else if (arg.equals("-V") || arg.equals("--version")) {
Bot bot = (Bot) Factory.getInstance().getBean("bot");
System.out.println(bot.getVersion());
break;
} else {
addBot(arg);
}
}
}
startBots();
} catch (Throwable t) {
LOG.fatal("Unexpected error", t);
System.exit(1);
}
}
/**
* Print usage information for the bot's command line invocation.
*/
public static void printUsage() {
System.out.println("Usage: java " + Main.class.getName()
+ " [-h] [-V] [ <init-script> [ <init-script> ...] ]");
}
public static void printHelp() {
printUsage();
System.out.println(" <init-script> (Optional) Path of the initial");
System.out.println(" configuration script. Every script");
System.out.println(" given starts a new bot.");
System.out.println(" Options:");
System.out.println(" -h|--help Print this help text and exit");
System.out.println(" -V|--version Print the program version and exit");
}
/**
* Starts the added bots.
*/
private static void startBots() {
for (final Map.Entry<String, Bot> e : bots.entrySet()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Starting bot " + e.getKey());
}
if (bots.size() > 1) {
Thread t = new Thread(e.getValue(), "Startup thread for bot "
+ e.getKey());
t.setDaemon(false); // give every startup thread a chance
t.start();
} else {
e.getValue().run();
}
}
if (LOG.isDebugEnabled()) {
int n = bots.size();
LOG.debug(n + " bot" + (n != 1 ? "s" : "") + " started");
}
}
/**
* Adds a bot to the list.
*/
private static void addBot() {
addBot(null);
}
/**
* Add a bot to the list.
* @param initScript the initScript to use.
*/
private static void addBot(String initScript) {
Bot bot = (Bot) Factory.getInstance().getBean("bot");
if (initScript != null) {
bot.getConfig().setInitScriptName(initScript);
}
bot.putInCache(null, BOTS_CACHE_KEY, bots);
bots.put(bot.getConfig().getInitScriptName(), bot);
}
}
MongoDB Logo MongoDB