2008-06-16 12:31:14 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
2009-06-15 15:32:27 +00:00
|
|
|
import java.io.File;
|
2010-02-11 19:18:26 +00:00
|
|
|
import java.io.IOException;
|
2008-06-16 12:31:14 +00:00
|
|
|
import java.util.HashMap;
|
2008-06-17 13:48:41 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2008-06-16 12:31:14 +00:00
|
|
|
import java.util.Map;
|
2008-06-17 13:48:41 +00:00
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import net.i2p.router.startup.ClientAppConfig;
|
2008-06-20 20:20:50 +00:00
|
|
|
import net.i2p.router.startup.LoadClientAppsJob;
|
2008-06-16 12:31:14 +00:00
|
|
|
import net.i2p.util.Log;
|
|
|
|
|
2008-06-20 20:20:50 +00:00
|
|
|
import org.mortbay.jetty.Server;
|
|
|
|
|
2008-06-16 12:31:14 +00:00
|
|
|
/**
|
2008-06-17 13:48:41 +00:00
|
|
|
* Saves changes to clients.config or webapps.config
|
2008-06-16 12:31:14 +00:00
|
|
|
*/
|
|
|
|
public class ConfigClientsHandler extends FormHandler {
|
2009-08-15 16:08:33 +00:00
|
|
|
private Log configClient_log;
|
2008-06-16 12:31:14 +00:00
|
|
|
private Map _settings;
|
|
|
|
|
2008-06-20 20:20:50 +00:00
|
|
|
public ConfigClientsHandler() {
|
2009-08-15 16:08:33 +00:00
|
|
|
configClient_log = ContextHelper.getContext(null).logManager().getLog(ConfigClientsHandler.class);
|
2008-06-20 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 16:08:33 +00:00
|
|
|
@Override
|
2008-06-16 12:31:14 +00:00
|
|
|
protected void processForm() {
|
2009-10-26 10:53:53 +00:00
|
|
|
if (_action.equals(_("Save Client Configuration"))) {
|
2008-06-17 13:48:41 +00:00
|
|
|
saveClientChanges();
|
2009-10-28 18:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_action.equals(_("Save WebApp Configuration"))) {
|
2008-06-17 13:48:41 +00:00
|
|
|
saveWebAppChanges();
|
2009-10-28 18:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
if (_action.equals(_("Save Plugin Configuration"))) {
|
|
|
|
savePluginChanges();
|
|
|
|
return;
|
|
|
|
}
|
2010-02-06 20:25:13 +00:00
|
|
|
if (_action.equals(_("Install Plugin"))) {
|
|
|
|
installPlugin();
|
|
|
|
return;
|
|
|
|
}
|
2009-10-28 18:26:50 +00:00
|
|
|
// value
|
|
|
|
if (_action.startsWith("Start ")) {
|
2008-06-20 20:20:50 +00:00
|
|
|
String app = _action.substring(6);
|
|
|
|
int appnum = -1;
|
|
|
|
try {
|
|
|
|
appnum = Integer.parseInt(app);
|
|
|
|
} catch (NumberFormatException nfe) {}
|
2010-02-17 18:12:46 +00:00
|
|
|
if (appnum >= 0) {
|
2008-06-20 20:20:50 +00:00
|
|
|
startClient(appnum);
|
2010-02-17 18:12:46 +00:00
|
|
|
} else {
|
|
|
|
List<String> plugins = PluginStarter.getPlugins();
|
|
|
|
if (plugins.contains(app))
|
|
|
|
startPlugin(app);
|
|
|
|
else
|
|
|
|
startWebApp(app);
|
|
|
|
}
|
2009-10-28 18:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-06 21:24:08 +00:00
|
|
|
|
|
|
|
// value
|
|
|
|
if (_action.startsWith("Delete ")) {
|
|
|
|
String app = _action.substring(7);
|
|
|
|
int appnum = -1;
|
|
|
|
try {
|
|
|
|
appnum = Integer.parseInt(app);
|
|
|
|
} catch (NumberFormatException nfe) {}
|
2010-02-10 19:09:35 +00:00
|
|
|
if (appnum >= 0) {
|
2010-01-06 21:24:08 +00:00
|
|
|
deleteClient(appnum);
|
2010-02-10 19:09:35 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
PluginStarter.stopPlugin(_context, app);
|
2010-02-11 19:18:26 +00:00
|
|
|
PluginStarter.deletePlugin(_context, app);
|
|
|
|
addFormNotice(_("Deleted plugin {0}", app));
|
2010-02-11 21:41:54 +00:00
|
|
|
} catch (Throwable e) {
|
2010-02-11 19:18:26 +00:00
|
|
|
addFormError(_("Error deleting plugin {0}", app) + ": " + e);
|
2010-02-11 21:41:54 +00:00
|
|
|
_log.error("Error deleting plugin " + app, e);
|
2010-02-11 19:18:26 +00:00
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
}
|
2010-01-06 21:24:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
|
|
|
|
// value
|
|
|
|
if (_action.startsWith("Stop ")) {
|
|
|
|
String app = _action.substring(5);
|
|
|
|
try {
|
|
|
|
PluginStarter.stopPlugin(_context, app);
|
2010-02-11 21:41:54 +00:00
|
|
|
addFormNotice(_("Stopped plugin {0}", app));
|
|
|
|
} catch (Throwable e) {
|
|
|
|
addFormError(_("Error stopping plugin {0}", app) + ": " + e);
|
|
|
|
_log.error("Error stopping plugin " + app, e);
|
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// value
|
|
|
|
if (_action.startsWith("Update ")) {
|
|
|
|
String app = _action.substring(7);
|
|
|
|
updatePlugin(app);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// value
|
|
|
|
if (_action.startsWith("Check ")) {
|
|
|
|
String app = _action.substring(6);
|
|
|
|
checkPlugin(app);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-28 18:26:50 +00:00
|
|
|
// label (IE)
|
|
|
|
String xStart = _("Start");
|
|
|
|
if (_action.toLowerCase().startsWith(xStart + "<span class=hide> ") &&
|
2009-04-03 21:33:35 +00:00
|
|
|
_action.toLowerCase().endsWith("</span>")) {
|
|
|
|
// IE sucks
|
2009-10-28 18:26:50 +00:00
|
|
|
String app = _action.substring(xStart.length() + 18, _action.length() - 7);
|
2009-04-03 21:33:35 +00:00
|
|
|
int appnum = -1;
|
|
|
|
try {
|
|
|
|
appnum = Integer.parseInt(app);
|
|
|
|
} catch (NumberFormatException nfe) {}
|
2010-02-17 18:12:46 +00:00
|
|
|
if (appnum >= 0) {
|
2009-04-03 21:33:35 +00:00
|
|
|
startClient(appnum);
|
2010-02-17 18:12:46 +00:00
|
|
|
} else {
|
|
|
|
List<String> plugins = PluginStarter.getPlugins();
|
|
|
|
if (plugins.contains(app))
|
|
|
|
startPlugin(app);
|
|
|
|
else
|
|
|
|
startWebApp(app);
|
|
|
|
}
|
2008-06-16 12:31:14 +00:00
|
|
|
} else {
|
2009-10-28 18:26:50 +00:00
|
|
|
addFormError(_("Unsupported") + ' ' + _action + '.');
|
2008-06-16 12:31:14 +00:00
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
|
2008-06-16 12:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setSettings(Map settings) { _settings = new HashMap(settings); }
|
|
|
|
|
2008-06-17 13:48:41 +00:00
|
|
|
private void saveClientChanges() {
|
2010-01-06 21:24:08 +00:00
|
|
|
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
|
2008-06-17 13:48:41 +00:00
|
|
|
for (int cur = 0; cur < clients.size(); cur++) {
|
2010-01-06 21:24:08 +00:00
|
|
|
ClientAppConfig ca = clients.get(cur);
|
2008-06-17 13:48:41 +00:00
|
|
|
Object val = _settings.get(cur + ".enabled");
|
2009-10-28 18:26:50 +00:00
|
|
|
if (! ("webConsole".equals(ca.clientName) || "Web console".equals(ca.clientName)))
|
2008-06-17 13:48:41 +00:00
|
|
|
ca.disabled = val == null;
|
2010-01-06 21:24:08 +00:00
|
|
|
// edit of an existing entry
|
2010-03-05 14:44:50 +00:00
|
|
|
String desc = getJettyString("desc" + cur);
|
2010-01-06 21:24:08 +00:00
|
|
|
if (desc != null) {
|
|
|
|
int spc = desc.indexOf(" ");
|
|
|
|
String clss = desc;
|
|
|
|
String args = null;
|
|
|
|
if (spc >= 0) {
|
|
|
|
clss = desc.substring(0, spc);
|
|
|
|
args = desc.substring(spc + 1);
|
|
|
|
}
|
|
|
|
ca.className = clss;
|
|
|
|
ca.args = args;
|
2010-03-05 14:44:50 +00:00
|
|
|
ca.clientName = getJettyString("name" + cur);
|
2010-01-06 21:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int newClient = clients.size();
|
2010-03-05 14:44:50 +00:00
|
|
|
String newDesc = getJettyString("desc" + newClient);
|
2010-01-18 14:51:39 +00:00
|
|
|
if (newDesc != null && newDesc.trim().length() > 0) {
|
2010-01-06 21:24:08 +00:00
|
|
|
// new entry
|
|
|
|
int spc = newDesc.indexOf(" ");
|
|
|
|
String clss = newDesc;
|
|
|
|
String args = null;
|
|
|
|
if (spc >= 0) {
|
|
|
|
clss = newDesc.substring(0, spc);
|
|
|
|
args = newDesc.substring(spc + 1);
|
|
|
|
}
|
2010-03-05 14:44:50 +00:00
|
|
|
String name = getJettyString("name" + newClient);
|
2010-01-18 14:51:39 +00:00
|
|
|
if (name == null || name.trim().length() <= 0) name = "new client";
|
2010-01-06 21:24:08 +00:00
|
|
|
ClientAppConfig ca = new ClientAppConfig(clss, name, args, 2*60*1000,
|
|
|
|
_settings.get(newClient + ".enabled") != null);
|
|
|
|
clients.add(ca);
|
|
|
|
addFormNotice(_("New client added") + ": " + name + " (" + clss + ").");
|
2008-06-17 13:48:41 +00:00
|
|
|
}
|
2010-01-06 21:24:08 +00:00
|
|
|
|
2008-06-17 13:48:41 +00:00
|
|
|
ClientAppConfig.writeClientAppConfig(_context, clients);
|
2009-10-26 10:53:53 +00:00
|
|
|
addFormNotice(_("Client configuration saved successfully - restart required to take effect."));
|
2008-06-17 13:48:41 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 21:24:08 +00:00
|
|
|
/** curses Jetty for returning arrays */
|
2010-03-05 14:44:50 +00:00
|
|
|
private String getJettyString(String key) {
|
2010-01-06 21:24:08 +00:00
|
|
|
String[] arr = (String[]) _settings.get(key);
|
|
|
|
if (arr == null)
|
|
|
|
return null;
|
|
|
|
return arr[0];
|
|
|
|
}
|
|
|
|
|
2008-06-20 20:20:50 +00:00
|
|
|
private void startClient(int i) {
|
2010-01-06 21:24:08 +00:00
|
|
|
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
|
2008-06-20 20:20:50 +00:00
|
|
|
if (i >= clients.size()) {
|
2009-10-26 10:53:53 +00:00
|
|
|
addFormError(_("Bad client index."));
|
2008-06-20 20:20:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-06 21:24:08 +00:00
|
|
|
ClientAppConfig ca = clients.get(i);
|
2009-08-15 16:08:33 +00:00
|
|
|
LoadClientAppsJob.runClient(ca.className, ca.clientName, LoadClientAppsJob.parseArgs(ca.args), configClient_log);
|
2009-10-28 18:26:50 +00:00
|
|
|
addFormNotice(_("Client") + ' ' + _(ca.clientName) + ' ' + _("started") + '.');
|
2008-06-20 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 21:24:08 +00:00
|
|
|
private void deleteClient(int i) {
|
|
|
|
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
|
|
|
|
if (i < 0 || i >= clients.size()) {
|
|
|
|
addFormError(_("Bad client index."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ClientAppConfig ca = clients.remove(i);
|
|
|
|
ClientAppConfig.writeClientAppConfig(_context, clients);
|
|
|
|
addFormNotice(_("Client") + ' ' + _(ca.clientName) + ' ' + _("deleted") + '.');
|
|
|
|
}
|
|
|
|
|
2008-06-17 13:48:41 +00:00
|
|
|
private void saveWebAppChanges() {
|
|
|
|
Properties props = RouterConsoleRunner.webAppProperties();
|
|
|
|
Set keys = props.keySet();
|
|
|
|
int cur = 0;
|
|
|
|
for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
|
|
|
|
String name = (String)iter.next();
|
|
|
|
if (! (name.startsWith(RouterConsoleRunner.PREFIX) && name.endsWith(RouterConsoleRunner.ENABLED)))
|
|
|
|
continue;
|
|
|
|
String app = name.substring(RouterConsoleRunner.PREFIX.length(), name.lastIndexOf(RouterConsoleRunner.ENABLED));
|
|
|
|
Object val = _settings.get(app + ".enabled");
|
|
|
|
if (! RouterConsoleRunner.ROUTERCONSOLE.equals(app))
|
|
|
|
props.setProperty(name, "" + (val != null));
|
2008-06-16 12:31:14 +00:00
|
|
|
}
|
2008-06-17 13:48:41 +00:00
|
|
|
RouterConsoleRunner.storeWebAppProperties(props);
|
2010-02-10 19:09:35 +00:00
|
|
|
addFormNotice(_("WebApp configuration saved."));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void savePluginChanges() {
|
|
|
|
Properties props = PluginStarter.pluginProperties();
|
|
|
|
Set keys = props.keySet();
|
|
|
|
int cur = 0;
|
|
|
|
for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
|
|
|
|
String name = (String)iter.next();
|
|
|
|
if (! (name.startsWith(PluginStarter.PREFIX) && name.endsWith(PluginStarter.ENABLED)))
|
|
|
|
continue;
|
|
|
|
String app = name.substring(PluginStarter.PREFIX.length(), name.lastIndexOf(PluginStarter.ENABLED));
|
|
|
|
Object val = _settings.get(app + ".enabled");
|
|
|
|
props.setProperty(name, "" + (val != null));
|
|
|
|
}
|
|
|
|
PluginStarter.storePluginProperties(props);
|
|
|
|
addFormNotice(_("Plugin configuration saved."));
|
2008-06-16 12:31:14 +00:00
|
|
|
}
|
2008-06-20 20:20:50 +00:00
|
|
|
|
2010-02-07 13:32:49 +00:00
|
|
|
/**
|
|
|
|
* Big hack for the moment, not using properties for directory and port
|
|
|
|
* Go through all the Jetty servers, find the one serving port 7657,
|
|
|
|
* requested and add the .war to that one
|
|
|
|
*/
|
2008-06-20 20:20:50 +00:00
|
|
|
private void startWebApp(String app) {
|
2010-02-18 16:33:47 +00:00
|
|
|
Server s = WebAppStarter.getConsoleServer();
|
2010-02-07 13:32:49 +00:00
|
|
|
if (s != null) {
|
2008-06-20 20:20:50 +00:00
|
|
|
try {
|
2009-06-15 15:32:27 +00:00
|
|
|
File path = new File(_context.getBaseDir(), "webapps");
|
|
|
|
path = new File(path, app + ".war");
|
2010-02-06 20:25:13 +00:00
|
|
|
WebAppStarter.startWebApp(_context, s, app, path.getAbsolutePath());
|
2009-10-28 18:26:50 +00:00
|
|
|
addFormNotice(_("WebApp") + " <a href=\"/" + app + "/\">" + _(app) + "</a> " + _("started") + '.');
|
2010-02-11 21:41:54 +00:00
|
|
|
} catch (Throwable e) {
|
|
|
|
addFormError(_("Failed to start") + ' ' + _(app) + " " + e + '.');
|
|
|
|
_log.error("Failed to start webapp " + app, e);
|
2008-06-20 20:20:50 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-10-26 10:53:53 +00:00
|
|
|
addFormError(_("Failed to find server."));
|
2008-06-20 20:20:50 +00:00
|
|
|
}
|
2010-02-06 20:25:13 +00:00
|
|
|
|
|
|
|
private void installPlugin() {
|
2010-03-05 14:44:50 +00:00
|
|
|
String url = getJettyString("pluginURL");
|
2010-02-06 20:25:13 +00:00
|
|
|
if (url == null || url.length() <= 0) {
|
|
|
|
addFormError(_("No plugin URL specified."));
|
|
|
|
return;
|
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
installPlugin(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePlugin(String app) {
|
|
|
|
Properties props = PluginStarter.pluginProperties(_context, app);
|
|
|
|
String url = props.getProperty("updateURL");
|
|
|
|
if (url == null) {
|
|
|
|
addFormError(_("No update URL specified for {0}",app));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
installPlugin(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void installPlugin(String url) {
|
2010-02-06 20:25:13 +00:00
|
|
|
if ("true".equals(System.getProperty(UpdateHandler.PROP_UPDATE_IN_PROGRESS))) {
|
|
|
|
addFormError(_("Plugin or update download already in progress."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PluginUpdateHandler puh = PluginUpdateHandler.getInstance(_context);
|
|
|
|
if (puh.isRunning()) {
|
|
|
|
addFormError(_("Plugin or update download already in progress."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
puh.update(url);
|
|
|
|
addFormNotice(_("Downloading plugin from {0}", url));
|
|
|
|
// So that update() will post a status to the summary bar before we reload
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException ie) {}
|
|
|
|
}
|
2010-02-10 19:09:35 +00:00
|
|
|
|
|
|
|
private void checkPlugin(String app) {
|
|
|
|
if ("true".equals(System.getProperty(UpdateHandler.PROP_UPDATE_IN_PROGRESS))) {
|
|
|
|
addFormError(_("Plugin or update download already in progress."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PluginUpdateChecker puc = PluginUpdateChecker.getInstance(_context);
|
|
|
|
if (puc.isRunning()) {
|
|
|
|
addFormError(_("Plugin or update download already in progress."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
puc.update(app);
|
|
|
|
addFormNotice(_("Checking plugin {0} for updates", app));
|
|
|
|
// So that update() will post a status to the summary bar before we reload
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
|
2010-02-17 18:12:46 +00:00
|
|
|
private void startPlugin(String app) {
|
|
|
|
try {
|
|
|
|
PluginStarter.startPlugin(_context, app);
|
|
|
|
addFormNotice(_("Started plugin {0}", app));
|
|
|
|
} catch (Throwable e) {
|
|
|
|
addFormError(_("Error starting plugin {0}", app) + ": " + e);
|
|
|
|
_log.error("Error starting plugin " + app, e);
|
|
|
|
}
|
|
|
|
}
|
2008-06-16 12:31:14 +00:00
|
|
|
}
|