use of com.servoy.j2db.LocalActiveSolutionHandler in project servoy-client by Servoy.
the class HeadlessClientFactoryInternal method createImportHookClient.
public static ISessionClient createImportHookClient(final Solution importHookModule, final IXMLImportUserChannel channel) throws Exception {
final String[] loadException = new String[1];
// assuming no login and no method args for import hooks
SessionClient sc = new SessionClient(null, null, null, null, null, importHookModule.getName()) {
@Override
protected IActiveSolutionHandler createActiveSolutionHandler() {
IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
return new LocalActiveSolutionHandler(as, this) {
@Override
protected Solution loadSolution(RootObjectMetaData solutionDef) throws RemoteException, RepositoryException {
// grab the latest version (-1) not the active one, because the hook was not yet activated.
return (Solution) ((IDeveloperRepository) getRepository()).getRootObject(solutionDef.getRootObjectId(), -1);
}
};
}
@Override
protected IExecutingEnviroment createScriptEngine() {
return new ScriptEngine(this) {
@Override
public Object executeFunction(Function f, Scriptable scope, Scriptable thisObject, Object[] args, boolean focusEvent, boolean throwException) throws Exception {
// always throw exception
return super.executeFunction(f, scope, thisObject, args, focusEvent, true);
}
};
}
@Override
public void reportError(String msg, Object detail) {
super.reportError(msg, detail);
loadException[0] = msg;
if (detail instanceof JavaScriptException && ((JavaScriptException) detail).getValue() instanceof Scriptable) {
loadException[0] += " " + Utils.getScriptableString((Scriptable) ((JavaScriptException) detail).getValue());
}
if (detail instanceof Exception) {
loadException[0] += " " + ((Exception) detail).getMessage();
}
}
};
sc.setUseLoginSolution(false);
String userName = channel.getImporterUsername();
if (userName != null) {
// let the import hook client run with credentials from the logged in user from the admin page.
sc.getClientInfo().setUserUid(ApplicationServerRegistry.get().getUserManager().getUserUID(sc.getClientID(), userName));
sc.getClientInfo().setUserName(userName);
}
sc.setOutputChannel(channel);
sc.loadSolution(importHookModule.getName());
if (loadException[0] != null) {
sc.shutDown(true);
throw new RepositoryException(loadException[0]);
}
return sc;
}
Aggregations