final User user = runtime.getUserDatabase().findUser(TomcatRuntime.TOMCAT_USER);
final String password = user.getPassword();
NamedResourceAssociation.putResource(contextPath, resource);
try {
DeployTask task = new DeployTask();
task.setWar(contentURL.toExternalForm());
task.setUsername(user.getName());
task.setPassword(password);
task.setPath(contextPath);
task.execute();
} finally {
NamedResourceAssociation.removeResource(contextPath);
if (tempfile != null) {
tempfile.delete();
}
}
// Get the resource as module (may be null)
Runtime runtime = RuntimeLocator.getRequiredRuntime();
Module module = runtime.getModule(identity);
Resource modres = module != null ? module.adapt(Resource.class) : resource;
return new AbstractResourceHandle(modres, module) {
@Override
public void uninstall() {
UndeployTask task = new UndeployTask();
task.setUsername(user.getName());
task.setPassword(password);
task.setPath(contextPath);
task.execute();
}
};
}