private void updateFactories()

in src/main/java/org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager.java [422:494]


    private void updateFactories() {
        readWriteLock.writeLock().lock();
        try {
            internalManager = getInternalScriptEngineManager();
            factories.clear();

            long fakeBundleIdCounter = Long.MIN_VALUE;
            // first add the platform factories
            for (final ScriptEngineFactory factory : internalManager.getEngineFactories()) {
                if (isIncluded(factory)) {
                    final SortableScriptEngineFactory sortableScriptEngineFactory =
                            new SortableScriptEngineFactory(factory, fakeBundleIdCounter++, 0, null);
                    factories.add(sortableScriptEngineFactory);
                }
            }

            // then factories from SPI Bundles
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(null);
                for (final Bundle bundle : engineSpiBundles) {
                    try {
                        final ScriptEngineManager manager = new ScriptEngineManager(
                                bundle.adapt(BundleWiring.class).getClassLoader());
                        for (final ScriptEngineFactory factory : manager.getEngineFactories()) {
                            if (isIncluded(factory)) {
                                final SortableScriptEngineFactory sortableScriptEngineFactory =
                                        new SortableScriptEngineFactory(factory, bundle.getBundleId(), 0, null);
                                factories.add(sortableScriptEngineFactory);
                            }
                        }
                    } catch (Exception ex) {
                        logger.error("Unable to process bundle " + bundle.getSymbolicName(), ex);
                    }
                }
            } finally {
                Thread.currentThread().setContextClassLoader(loader);
            }
            // and finally factories registered as OSGi services
            if (bundleContext != null) {
                synchronized (this.serviceReferences) {
                    for (final ServiceReference<ScriptEngineFactory> serviceReference : serviceReferences) {
                        final ScriptEngineFactory scriptEngineFactory = bundleContext.getService(serviceReference);
                        if (isIncluded(scriptEngineFactory)) {
                            final Map<String, Object> factoryProperties =
                                    new HashMap<>(serviceReference.getPropertyKeys().length);
                            for (final String key : serviceReference.getPropertyKeys()) {
                                factoryProperties.put(key, serviceReference.getProperty(key));
                            }
                            final Object prop = serviceReference.getProperty(Constants.SERVICE_RANKING);
                            final int ranking = prop instanceof Integer ? (Integer) prop : 0;
                            final SortableScriptEngineFactory sortableScriptEngineFactory =
                                    new SortableScriptEngineFactory(
                                            scriptEngineFactory,
                                            serviceReference.getBundle().getBundleId(),
                                            ranking,
                                            factoryProperties);
                            factories.add(sortableScriptEngineFactory);
                        }
                    }
                }
            }
            // register the associations at the end, so that the priority sorting is taken into consideration
            for (final ScriptEngineFactory factory : factories) {
                registerAssociations(factory);
            }
            if (eventAdmin != null) {
                eventAdmin.postEvent(new Event(EVENT_TOPIC_SCRIPT_MANAGER_UPDATED, Collections.emptyMap()));
            }
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }