try {
// parse the command line arguments
line = parser.parse(options, args);
} catch (ParseException exp) {
Undeploy.help(options);
throw new SystemExitException(-1);
}
if (line.hasOption("help")) {
Undeploy.help(options);
return;
} else if (line.hasOption("version")) {
OpenEjbVersion.get().print(System.out);
return;
}
if (line.getArgList().size() == 0) {
System.out.println("Must specify an module id.");
help(options);
return;
}
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
String serverUrl = Undeploy.defaultServerUrl;
if (line.hasOption(serverUrl)) {
serverUrl = line.getOptionValue("serverUrl");
}
p.put(Context.PROVIDER_URL, serverUrl);
Deployer deployer = null;
try {
InitialContext ctx = new InitialContext(p);
deployer = (Deployer) ctx.lookup("openejb/DeployerBusinessRemote");
} catch (javax.naming.ServiceUnavailableException e) {
System.out.println(e.getCause().getMessage());
System.out.println(Undeploy.messages.format("cmd.deploy.serverOffline"));
throw new SystemExitException(1);
} catch (javax.naming.NamingException e) {
System.out.println("DeployerEjb does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed.");
throw new SystemExitException(2);
}
int exitCode = 0;
for (Object obj : line.getArgList()) {
String moduleId = (String) obj;
try {
boolean undeployed = false;
// Treat moduleId as a file path, and see if there is a matching app to undeploy
String path = null;
try {
path = new File(moduleId).getCanonicalPath();
} catch (IOException e) {
}
if (path != null) {
try {
deployer.undeploy(path);
undeployed = true;
} catch (NoSuchApplicationException e) {
}
}
// If that didn't work, undeploy using just the moduleId
if (!undeployed) {
deployer.undeploy(moduleId);
}
// TODO make this message
System.out.println(messages.format("cmd.undeploy.successful", moduleId));
} catch (UndeployException e) {
// TODO make this message
// TODO Maybe brush up this excpetion handling
System.out.println(messages.format("cmd.undeploy.failed", moduleId));
e.printStackTrace(System.out);
exitCode += 100;
} catch (NoSuchApplicationException e) {
// TODO make this message
System.out.println(messages.format("cmd.undeploy.noSuchModule", moduleId));
exitCode += 100;
}
}
if (exitCode != 0){
throw new SystemExitException(exitCode);
}
}