use of com.dexels.navajo.script.api.CompiledScriptFactory in project navajo by Dexels.
the class OSGiFrameworkMock method parseDSFileAndAddServiceReference.
/**
* Helper function to parse the generated DS file and create the necessary ServiceReference object, which is then registered in this class
* Only supports ServiceReference to CompiledScriptFactory
*
* If something goes wrong trying to access the DS file, the service reference is still created but will not contain any properties
*
* @param location The string describing the location of the jar file
* @param bundle The bundle for which to create the service reference
*/
private void parseDSFileAndAddServiceReference(String location, BundleMock bundle) throws BundleException {
ServiceReferenceMock<CompiledScriptFactory> serviceReference = new ServiceReferenceMock<>(CompiledScriptFactory.class, bundle);
serviceReferences.add(serviceReference);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
builderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
// Find the DS file in the jar and parse it for the properties
try (ZipFile jarFile = new ZipFile(Paths.get(URI.create(location)).toFile())) {
InputStream source = jarFile.getInputStream(jarFile.getEntry("OSGI-INF/script.xml"));
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(source);
document.normalize();
NodeList components = document.getElementsByTagName("scr:component");
if (components == null || components.getLength() != 1 || !(components.item(0) instanceof Element)) {
throw new BundleException("Structure of DS file not as expected");
}
Element component = (Element) components.item(0);
String symbolicName = component.getAttribute("name");
if (symbolicName != null) {
String oldSymbolicName = bundle.getSymbolicName();
bundle.setSymbolicName(symbolicName);
installedBundlesBySymbolicName.remove(oldSymbolicName);
installedBundlesBySymbolicName.put(symbolicName, bundle);
}
NodeList nodes = component.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
if (n instanceof Element) {
Element e = (Element) n;
if (e.getTagName().equals("property")) {
serviceReference.addProperty(e.getAttribute("name"), e.getAttribute("value"));
}
}
}
} catch (Exception e) {
// then we don't do properties :(
e.printStackTrace();
}
}
use of com.dexels.navajo.script.api.CompiledScriptFactory in project navajo by Dexels.
the class BundleCreatorComponent method getCompiledScript.
@SuppressWarnings("unchecked")
private CompiledScriptInterface getCompiledScript(String rpcName, String tenant) throws CompilationException {
String scriptName = rpcName.replaceAll("/", ".");
String realTenant = "default";
if (tenant != null) {
realTenant = tenant;
}
if (scriptsMap.containsKey(rpcName)) {
Map<String, CompiledScriptFactory> myScripts = scriptsMap.get(rpcName);
CompiledScriptFactory csf = myScripts.get(realTenant);
if (csf != null) {
try {
return csf.getCompiledScript();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
logger.error("Exception on retrieving cached CompiledScriptFactory for {} {} - going to try non-cached one", rpcName, realTenant);
}
}
}
String filter = "(&(navajo.scriptName=" + scriptName + ") (|(navajo.tenant=" + tenant + ") (navajo.tenant=default)))";
ServiceReference<CompiledScriptFactory>[] servicereferences;
try {
servicereferences = (ServiceReference<CompiledScriptFactory>[]) bundleContext.getServiceReferences(CompiledScriptFactory.class.getName(), filter);
if (servicereferences != null) {
// First try to find one that matches our tenant
for (ServiceReference<CompiledScriptFactory> srinstance : servicereferences) {
if (srinstance.getProperty("navajo.tenant").equals(tenant)) {
CompiledScriptFactory csf = bundleContext.getService(srinstance);
if (csf == null) {
logger.warn("Script with filter: {} found, but could not be resolved.", filter);
return null;
}
updateCachedCompiledScript(rpcName, realTenant, csf);
return csf.getCompiledScript();
}
}
// if that fails, simply return first one (probably "default")
if (servicereferences.length > 0) {
ServiceReference<CompiledScriptFactory> srinstance = servicereferences[0];
CompiledScriptFactory csf = bundleContext.getService(srinstance);
if (csf == null) {
logger.warn("Script with filter: {} found, but could not be resolved.", filter);
return null;
}
updateCachedCompiledScript(rpcName, realTenant, csf);
return csf.getCompiledScript();
}
}
} catch (InvalidSyntaxException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new CompilationException("Error resolving script service for: " + rpcName, e);
}
return null;
}
Aggregations