Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
*/
package org.diirt.util.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A wrapper for ServiceLoader that works in OSGi as well.
*
* OSGi is known to break the ServiceLoader mechanism. Unfortunately, we have
* not been able to make the standard based solution based on Ares SPI-Fly work.
* Therefore we have created this wrapper that detects whether the class has
* been loaded with OSGi. If not, uses the standard ServiceLoader. If so, uses
* introspection to access the OSGi framework and replicate the functionality.
*
* Note that the implementation on OSGi will not give all the nice debugging
* information that the standard ServiceLoader implementation does. It also is
* not optimized: makes heavy use of introspection and does not use lazy
* initialization. It's essentially a hack to make things to work.
*
* @author carcassi
*/
public class ServiceLoaderOSGiWrapper {
public static void load(Class serviceClazz, Logger log, Consumer consumer) {
log.log(Level.CONFIG, "Fetching {0}s", serviceClazz.getSimpleName());
int count = 0;
for (T service : ServiceLoaderOSGiWrapper.load(serviceClazz)) {
log.log(Level.CONFIG, "Found {0} ({1})", new Object[] {serviceClazz.getSimpleName(), service.getClass().getSimpleName()});
consumer.accept(service);
count++;
}
log.log(Level.CONFIG, "Found {0} {1}s", new Object[] {count, serviceClazz.getSimpleName()});
}
public static Iterable load(Class serviceClazz) {
if (isOSGi(serviceClazz)) {
return loadOSGi(serviceClazz);
} else {
return ServiceLoader.load(serviceClazz);
}
}
private static boolean isOSGi(Class> serviceClazz) {
try {
if (osgi.frameworkUtilClass != null && osgi.getBundle(serviceClazz) != null) {
return true;
}
} catch (Exception e) {
}
return false;
}
private static List loadOSGi(Class serviceClazz) {
// TODO: improve logging
Object bundle = osgi.getBundle(serviceClazz);
System.out.println("Found bundle " + bundle);
osgi.start(bundle);
Object bundleContext = osgi.getBundleContext(bundle);
Object[] bundles = osgi.getBundles(bundleContext);
System.out.println("Found bundle " + bundleContext);
System.out.println("Found bundles " + bundles);
Map