/*
* This program 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. You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.aitools.programd.interfaces.shell;
import java.io.FileNotFoundException;
import org.aitools.programd.Bot;
import org.aitools.programd.graph.Graphmapper;
import org.aitools.util.resource.URLTools;
import org.apache.log4j.Logger;
/**
* @author <a href="mailto:noel@aitools.org">Noel Bush</a>
*
*/
public class UnloadCommand extends ShellCommand {
/** Shell command string. */
private static final String COMMAND_STRING = "/unload";
/** Argument template. */
public static final String ARGUMENT_TEMPLATE = "filename";
/** Shell help line. */
private static final String HELP_LINE = "unloads given filename for active bot";
/**
* Creates a new UnloadCommand.
*/
public UnloadCommand() {
super(COMMAND_STRING, ARGUMENT_TEMPLATE, HELP_LINE);
}
/**
* Attempts to unload the file named on the command line from the Graphmapper.
*
* @see org.aitools.programd.interfaces.shell.ShellCommand#handle(java.lang.String,
* org.aitools.programd.interfaces.shell.Shell)
*/
@Override
public void handle(String commandLine, Shell shell) {
// See if there is a filename.
int space = commandLine.indexOf(' ');
if (space == -1) {
shell.showError("You must specify a filename.");
}
else {
Graphmapper graphmapper = shell.getCore().getGraphmapper();
int categories = graphmapper.getCategoryCount();
Bot bot = shell.getBots().get(shell.getCurrentBotID());
String path = commandLine.substring(space + 1);
try {
graphmapper.unload(URLTools.createValidURL(path), bot);
bot.getLoadedFilesMap().remove(path);
Logger.getLogger("programd").info(categories - graphmapper.getCategoryCount() + " categories unloaded.");
}
catch (FileNotFoundException e) {
shell.showError(String.format("Could not find \"%s\".", path));
}
}
}
/**
* @see org.aitools.programd.interfaces.shell.ShellCommand#handles(java.lang.String)
*/
@Override
public boolean handles(String commandLine) {
return commandLine.toLowerCase().startsWith(COMMAND_STRING);
}
}