Search in sources :

Example 1 with CompiledScriptFactory

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();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) CompiledScriptFactory(com.dexels.navajo.script.api.CompiledScriptFactory) ZipFile(java.util.zip.ZipFile) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BundleException(org.osgi.framework.BundleException)

Example 2 with CompiledScriptFactory

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;
}
Also used : SkipCompilationException(com.dexels.navajo.mapping.compiler.SkipCompilationException) CompilationException(com.dexels.navajo.script.api.CompilationException) ServiceReference(org.osgi.framework.ServiceReference) CompiledScriptFactory(com.dexels.navajo.script.api.CompiledScriptFactory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

CompiledScriptFactory (com.dexels.navajo.script.api.CompiledScriptFactory)2 SkipCompilationException (com.dexels.navajo.mapping.compiler.SkipCompilationException)1 CompilationException (com.dexels.navajo.script.api.CompilationException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipFile (java.util.zip.ZipFile)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1