public RenderUnit getRenderUnit()

in src/main/java/org/apache/sling/scripting/sightly/impl/engine/bundled/BundledUnitManagerImpl.java [110:186]


    public RenderUnit getRenderUnit(@NotNull Bindings bindings, @NotNull String identifier) {
        BundledRenderUnit bundledRenderUnit = getBundledRenderUnit(bindings);
        Resource currentResource = BindingsUtils.getResource(bindings);
        Set<String> defaultLocations = new LinkedHashSet<>();
        for (String searchPath : resourceResolverFactory.getSearchPath()) {
            if (identifier.startsWith("/")) {
                defaultLocations.add(identifier);
                if (identifier.startsWith(searchPath)) {
                    defaultLocations.add(identifier.substring(searchPath.length()));
                }
            } else {
                String path = ResourceUtil.normalize(searchPath + "/" + identifier);
                if (path != null) {
                    defaultLocations.add(path);
                }
            }
        }
        if (currentResource != null && bundledRenderUnit != null) {
            for (TypeProvider provider : bundledRenderUnit.getTypeProviders()) {
                Set<String> locations = new LinkedHashSet<>();
                if (!identifier.startsWith("/")) {
                    for (ResourceType type :
                            provider.getBundledRenderUnitCapability().getResourceTypes()) {
                        locations.add(getResourceTypeQualifiedPath(identifier, type));
                    }
                }
                locations.addAll(defaultLocations);
                for (String renderUnitIdentifier : locations) {
                    String renderUnitBundledPath = renderUnitIdentifier;
                    if (renderUnitBundledPath.startsWith("/")) {
                        renderUnitBundledPath = renderUnitBundledPath.substring(1);
                    }
                    String className = JavaEscapeHelper.makeJavaPackage(renderUnitBundledPath);
                    try {
                        Class<?> clazz = provider.getBundle().loadClass(className);
                        if (clazz.getSuperclass() == RenderUnit.class) {
                            return (RenderUnit) clazz.getDeclaredConstructor().newInstance();
                        }
                    } catch (ClassNotFoundException e) {
                        URL bundledScriptURL =
                                provider.getBundle().getEntry("javax.script" + "/" + renderUnitBundledPath);
                        if (bundledScriptURL != null) {
                            try {
                                SightlyScriptEngine sightlyScriptEngine =
                                        (SightlyScriptEngine) sightlyScriptEngineFactory.getScriptEngine();
                                if (sightlyScriptEngine != null) {
                                    CachedScript cachedScript =
                                            scriptCache.getScript(bundledScriptURL.toExternalForm());
                                    if (cachedScript != null) {
                                        return ((SightlyCompiledScript) cachedScript.getCompiledScript())
                                                .getRenderUnit();
                                    } else {
                                        try (ScriptNameAwareReader reader = new ScriptNameAwareReader(
                                                new InputStreamReader(
                                                        bundledScriptURL.openStream(), StandardCharsets.UTF_8),
                                                renderUnitIdentifier)) {
                                            SightlyCompiledScript compiledScript =
                                                    (SightlyCompiledScript) sightlyScriptEngine.compile(reader);
                                            return compiledScript.getRenderUnit();
                                        }
                                    }
                                }
                            } catch (IOException | ScriptException compileException) {
                                throw new IllegalStateException(compileException);
                            }
                        }
                    } catch (NoSuchMethodException
                            | InstantiationException
                            | IllegalAccessException
                            | InvocationTargetException e) {
                        throw new IllegalArgumentException(e);
                    }
                }
            }
        }
        return null;
    }