in winegrower-extension/winegrower-agent/src/main/java/org/apache/winegrower/extension/agent/WinegrowerAgent.java [114:152]
private static void doStart(final String agentArgs, final Instrumentation instrumentation) throws Throwable {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final Class<?> bundleClass = loader.loadClass("org.osgi.framework.Bundle");
final Class<?> ripenerImplClass = loader.loadClass("org.apache.winegrower.Ripener$Impl");
final Class<?> ripenerConfigurationClass = loader.loadClass("org.apache.winegrower.Ripener$Configuration");
final Object ripener = ripenerImplClass.getConstructor(ripenerConfigurationClass)
.newInstance(createConfiguration(ripenerConfigurationClass, agentArgs));
// before start - avoids reload/refresh
final Object services = ripenerImplClass.getMethod("getServices").invoke(ripener);
final Object bundleRegistry = ripenerImplClass.getMethod("getRegistry").invoke(ripener);
final Object bundleLifecycle0 = Map.class.cast(bundleRegistry.getClass().getMethod("getBundles").invoke(bundleRegistry)).get(0L);
final Object bundle0 = bundleLifecycle0.getClass().getMethod("getBundle").invoke(bundleLifecycle0);
services.getClass().getMethod("registerService", String[].class, Object.class, Dictionary.class, bundleClass)
.invoke(services, new String[]{Instrumentation.class.getName()}, instrumentation, new Hashtable<>(), bundle0);
doCall(ripener, "start", new Class<?>[0], new Object[0]);
System.setProperty("winegrower.agent.started", "true");
final Thread stopThread = new Thread(() -> {
try {
doCall(ripener, "stop", new Class<?>[0], new Object[0]);
} catch (final Throwable throwable) {
if (DEBUG) {
throwable.printStackTrace();
} // else: not that important
} finally {
if (WinegrowerAgentClassLoader.class.isInstance(loader)) {
try {
WinegrowerAgentClassLoader.class.cast(loader).close();
} catch (final IOException e) {
// no-op
}
}
}
}, WinegrowerAgent.class.getName() + "-shutdown");
stopThread.setContextClassLoader(loader);
Runtime.getRuntime().addShutdownHook(stopThread);
}