protected void finishStartup()

in src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java [359:401]


    protected void finishStartup(final Framework framework) {
        Bundle featureBundle = null;
        for(final Bundle bundle : framework.getBundleContext().getBundles()) {
            if ( featureSupplier != null && "org.apache.sling.feature".equals(bundle.getSymbolicName()) ) {
                featureBundle = bundle;
            }
        }
        if ( featureBundle != null ) {
            final Bundle bundle = featureBundle;
            // the feature is registered as a prototype to give each client a copy as feature models are mutable
            final Dictionary<String, Object> properties = new Hashtable<>();
            properties.put("name", "org.apache.sling.feature.launcher");
            featureBundle.getBundleContext().registerService(new String[] {"org.apache.sling.feature.Feature"}, 
                new PrototypeServiceFactory<Object>() {

                    @Override
                    public Object getService(final Bundle client, final ServiceRegistration<Object> registration) {
                        final ClassLoader cl = bundle.adapt(BundleWiring.class).getClassLoader();
                        final ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader();
                        Thread.currentThread().setContextClassLoader(cl);
                        try {
                            final Class<?> readerClass = cl.loadClass("org.apache.sling.feature.io.json.FeatureJSONReader");
                            final Method readMethod = readerClass.getDeclaredMethod("read", java.io.Reader.class, String.class);
                            try( final StringReader reader = new StringReader(featureSupplier.get())) {
                                return readMethod.invoke(null, reader, null);
                            }
                        } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                            // ignore
                        } finally {
                            Thread.currentThread().setContextClassLoader(oldTCCL);
                        }
                        return null;
                    }

                    @Override
                    public void ungetService(final Bundle client, final ServiceRegistration<Object> registration,
                            final Object service) {
                        // nothing to do
                    }
                    
                }, properties);
        }
    }