use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class GenericHandler method doService.
/**
* doService() is called by Dispatcher to perform web service.
*
* @return
* @throws NavajoException
* @throws UserException
* @throws SystemException
* @throws AuthorizationException
*/
@Override
public final Navajo doService(Access a) throws UserException, SystemException, AuthorizationException {
if (a.isBreakWasSet()) {
if (a.getOutputDoc() == null) {
Navajo outDoc = NavajoFactory.getInstance().createNavajo();
a.setOutputDoc(outDoc);
}
return a.getOutputDoc();
}
Navajo outDoc = null;
StringBuilder compilerErrors = new StringBuilder();
outDoc = NavajoFactory.getInstance().createNavajo();
CompiledScriptInterface cso = null;
try {
cso = loadOnDemand(a, a.rpcName);
} catch (Throwable e) {
logger.error("Exception on getting compiledscript", e);
if (e instanceof FileNotFoundException) {
a.setExitCode(Access.EXIT_SCRIPT_NOT_FOUND);
}
throw new SystemException(-1, e.getMessage(), e);
}
try {
if (cso == null) {
if (Version.osgiActive()) {
logger.warn("Script not found from OSGi registry while OSGi is active");
}
logger.error("No compiled script found, proceeding further is useless.");
throw new RuntimeException("Can not resolve script: " + a.rpcName);
}
a.setOutputDoc(outDoc);
a.setCompiledScript(cso);
if (cso.getClassLoader() == null) {
logger.error("No classloader present!");
}
cso.run(a);
return a.getOutputDoc();
} catch (Throwable e) {
if (e instanceof com.dexels.navajo.mapping.BreakEvent) {
// Outdoc might have been changed by running script
outDoc = a.getOutputDoc();
// Create dummy header to set breakwasset attribute.
Header h = NavajoFactory.getInstance().createHeader(outDoc, "", "", "", -1);
outDoc.addHeader(h);
outDoc.getHeader().setHeaderAttribute("breakwasset", "true");
return outDoc;
} else if (e instanceof com.dexels.navajo.server.ConditionErrorException) {
return ((com.dexels.navajo.server.ConditionErrorException) e).getNavajo();
} else if (e instanceof UserException) {
throw (UserException) e;
}
throw new SystemException(-1, e.getMessage(), e);
}
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class GenericHandler method compileScript.
/**
* Non-OSGi only
* @param a
* @param compilerErrors
* @return
* @throws Exception
*/
public CompiledScript compileScript(Access a, StringBuilder compilerErrors) throws Exception {
NavajoConfigInterface properties = DispatcherFactory.getInstance().getNavajoConfig();
List<Dependency> deps = new ArrayList<>();
String scriptPath = properties.getScriptPath();
Object[] all = getScriptPathServiceNameAndScriptFile(a, a.rpcName, a.betaUser);
if (all == null) {
throw new FileNotFoundException("No script found for: " + a.rpcName);
}
String pathPrefix = (String) all[0];
String serviceName = (String) all[1];
File scriptFile = (File) all[2];
// String sourceFileName = (String) all[3];
File sourceFile = (File) all[4];
String className = (String) all[5];
if (properties.isCompileScripts()) {
if (scriptFile != null && scriptFile.exists()) {
if (checkScriptRecompile(scriptFile, sourceFile) || hasDirtyDepedencies(a, className)) {
synchronized (mutex1) {
if (checkScriptRecompile(scriptFile, sourceFile) || hasDirtyDepedencies(a, className)) {
com.dexels.navajo.mapping.compiler.TslCompiler tslCompiler = new com.dexels.navajo.mapping.compiler.TslCompiler(properties.getClassloader());
try {
final String tenant = tenantConfig.getInstanceGroup();
tslCompiler.compileScript(serviceName, scriptPath, properties.getCompiledScriptPath(), pathPrefix, properties.getOutputWriter(properties.getCompiledScriptPath(), pathPrefix, serviceName, ".java"), deps, tenant, tenantConfig.hasTenantScriptFile(serviceName, tenant, null), false);
} catch (SystemException ex) {
Files.deleteIfExists(sourceFile.toPath());
AuditLog.log(AuditLog.AUDIT_MESSAGE_SCRIPTCOMPILER, ex.getMessage(), Level.SEVERE, a.accessID);
throw ex;
}
}
}
}
// end of sync block.
// Java recompile.
// TODO check if this removal is ok
// compilerErrors.append(recompileJava(a, sourceFileName, sourceFile, className, targetFile,serviceName));
} else {
// Maybe the jave file exists in the script path.
if (!sourceFile.exists()) {
// There is no java file present.
AuditLog.log(AuditLog.AUDIT_MESSAGE_SCRIPTCOMPILER, "SCRIPT FILE DOES NOT EXISTS, I WILL TRY TO LOAD THE CLASS FILE ANYWAY....", Level.WARNING, a.accessID);
} else {
// Compile java file using normal java compiler.
if (sourceFile.getName().endsWith("java")) {
logger.error("Separate java scripts not supported!");
}
}
}
}
return getCompiledScript(a, className, sourceFile, serviceName);
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class NavajoConfigEmitter method parseServerXml.
private void parseServerXml() {
try {
String rootPath = this.navajoContext.getInstallationPath();
File rp = new File(rootPath);
File serverXml = new File(rp, CONFIG_SERVER_XML);
URL srv = serverXml.toURI().toURL();
final String absolutePath = properDir(rp.getAbsolutePath());
createNavajoConfigConfiguration(srv, absolutePath);
} catch (MalformedURLException e) {
logger.error("Parse error: ", e);
} catch (IOException e) {
logger.error("IO error: ", e);
} catch (SystemException e) {
logger.error("System error: ", e);
} catch (Throwable e) {
logger.error("Unexpected error activating dispatcher configuration factory: ", e);
}
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class NavajoConfigEmitter method createNavajoConfigConfiguration.
private void createNavajoConfigConfiguration(URL configurationUrl, String rootPath) throws IOException, SystemException {
InputStream is = configurationUrl.openStream();
logger.debug("Setting up configuration for rootpath: {}", rootPath);
logger.debug("Setting up configuration url: {}", configurationUrl);
Dictionary<String, Object> data = new Hashtable<>();
Navajo configuration = NavajoFactory.getInstance().createNavajo(is);
Message body = configuration.getMessage("server-configuration");
if (body == null) {
throw new SystemException(-1, "Could not read configuration file server.xml");
}
// Get the instance name.
data.put("instanceName", getInstanceName(body));
// Get the instance group.
data.put("instanceGroup", getInstanceGroup(body));
String configPath = properDir(rootPath + getMessageValueWithDefault("paths/configuration", "config", body));
String adapterPath = properDir(rootPath + getMessageValueWithDefault("paths/adapters", "adapters", body));
String scriptPath = properDir(rootPath + getMessageValueWithDefault("paths/scripts", "scripts", body));
data.put("configPath", configPath);
data.put("adapterPath", adapterPath);
data.put("scriptPath", scriptPath);
Property resourceProperty = body.getProperty("paths/resource");
if (resourceProperty != null) {
String resourcePath = properDir(rootPath + resourceProperty.getValue());
data.put("resourcePath", resourcePath);
}
String compiledScriptPath = (body.getProperty("paths/compiled-scripts") != null ? properDir(rootPath + body.getProperty("paths/compiled-scripts").getValue()) : "");
data.put("compiledScriptPath", compiledScriptPath);
Property descriptionProviderProperty = body.getProperty("description-provider/class");
String descriptionProviderClass = null;
if (descriptionProviderProperty != null) {
descriptionProviderClass = descriptionProviderProperty.getValue();
if (descriptionProviderClass != null) {
data.put("descriptionProviderClass", descriptionProviderClass);
}
}
Dictionary<String, Object> d = new Hashtable<>();
d.put("enable", true);
injectConfiguration("navajo.server.statistics", d);
Property s = body.getProperty("parameters/async_timeout");
// default 1 hour.
double asyncTimeout = 3600 * 1000;
if (s != null) {
asyncTimeout = Float.parseFloat(s.getValue()) * 1000;
}
boolean enableAsync = (body.getProperty("parameters/enable_async") == null || body.getProperty("parameters/enable_async").getValue().equals("true"));
d = new Hashtable<>();
d.put("enable", enableAsync);
d.put("asyncTimeout", asyncTimeout);
injectConfiguration("navajo.server.async", d);
boolean enableIntegrityWorker = (body.getProperty("parameters/enable_integrity") == null || body.getProperty("parameters/enable_integrity").getValue().equals("true"));
d = new Hashtable<>();
d.put("enable", enableIntegrityWorker);
injectConfiguration("navajo.server.integrity", d);
boolean enableLockManager = (body.getProperty("parameters/enable_locks") == null || body.getProperty("parameters/enable_locks").getValue().equals("true"));
d = new Hashtable<>();
d.put("enable", enableLockManager);
injectConfiguration("navajo.server.lockmanager", d);
int maxAccessSetSize = (body.getProperty("parameters/max_webservices") == null ? MAX_ACCESS_SET_SIZE : Integer.parseInt(body.getProperty("parameters/max_webservices").getValue()));
data.put("maxAccessSetSize", maxAccessSetSize);
data.put("compileScripts", true);
// Get document class implementation.
String documentClass = "com.dexels.navajo.document.base.BaseNavajoFactoryImpl";
data.put("documentClass", documentClass);
if (documentClass != null) {
System.setProperty("com.dexels.navajo.DocumentImplementation", documentClass);
NavajoFactory.resetImplementation();
NavajoFactory.getInstance();
NavajoFactory.getInstance().setExpressionEvaluator(new CachedExpressionEvaluator());
}
addAllProperties(body.getMessage("parameters"), data);
updateIfChanged("navajo.server.config", data);
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class CheckUniqueness method evaluate.
@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
if (getOperands().size() < 2) {
for (int i = 0; i < getOperands().size(); i++) {
Object o = getOperands().get(i);
logger.debug("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
}
throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
}
if (!(operand(0).value instanceof String && operand(1).value instanceof String)) {
throw new TMLExpressionException(this, "Wrong argument types: " + operand(0).value.getClass() + " and " + operand(1).value.getClass());
}
String messageName = getStringOperand(0);
String propertyName = getStringOperand(1);
String filter = null;
if (getOperands().size() > 2) {
filter = getStringOperand(2);
}
Message parent = getCurrentMessage();
Navajo doc = getNavajo();
boolean isUnique = true;
HashSet<Object> values = new HashSet<Object>();
try {
List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
if (arrayMsg == null) {
throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
}
for (int i = 0; i < arrayMsg.size(); i++) {
Message m = arrayMsg.get(i);
Property p = m.getProperty(propertyName);
boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
if (evaluate) {
if (p != null) {
Object o = null;
if (p.getType().equals(Property.SELECTION_PROPERTY)) {
o = p.getSelected().getValue();
} else {
o = p.getTypedValue();
}
if (values.contains(o)) {
return Boolean.FALSE;
} else {
values.add(o);
}
}
}
}
} catch (NavajoException ne) {
throw new TMLExpressionException(this, ne.getMessage());
} catch (SystemException se) {
throw new TMLExpressionException(this, se.getMessage());
}
return (isUnique ? Boolean.TRUE : Boolean.FALSE);
}
Aggregations