これにはフェリックスを使用しています。私のテストでは、単純な HelloWorld を作成しました。
バンドル 1
public interface HelloWorldService {
public void sayHello(String name);
}
--------- 実装 ----------
バンドル 2
バンドルアクティベーター
public class HelloWorldActivator implements BundleActivator {
private static BundleContext bc;
@Override
public void start(BundleContext bundleContext) throws Exception {
bc = bundleContext;
System.out.println(bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME) + " starting...");
HelloWorldService helloWorldService = new HelloWorldImpl();
bc.registerService(HelloWorldService.class.getName(),helloWorldService,null);
System.out.println("registered...");
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
System.out.println(bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME) + " stopping...");
bc = null;
}
}
実装 ---
public class HelloWorldImpl implements HelloWorldService {
@Override
public void sayHello(String name) {
System.out.println("Hello "+name);
}
}
OSGI バンドルでもあるクライアントを使用すると、正常に動作します。OSGI サービスを使用する外部クライアントでは、ClassCastException が発生します。何か案は ?
public class ExternalClient {
private static final String BUNDLE_DIRECTORY = System.getProperty("auto.deploy.dir");
private static final ExternalClient externalClient = new ExternalClient();
private static Framework framework;
private ExternalClient() {
start();
}
public static void main(String[] args) throws Exception{
//For Some reason ServiceTracker was not returning the service, nor was getServiceReference.
ServiceReference[] serviceReferences = framework.getBundleContext().getAllServiceReferences(HelloWorldService.class.getName(),null);
for(ServiceReference serviceReference : serviceReferences) {
System.out.println(framework.getBundleContext().getService(serviceReference));
}
HelloWorldService helloWorldService = (HelloWorldService)framework.getBundleContext().getService(serviceReferences[0]); // Gives CLassCastException saying java.lang.ClassCastException: com.test.helloworld.impl.HelloWorldImpl cannot be cast to com.test.helloworld.HelloWorldService
helloWorldService.sayHello("Blah");
Thread.sleep(Long.MAX_VALUE);
}
public void start() {
try {
ServiceLoader<FrameworkFactory> factoryServiceLoader = ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory frameworkFactory = factoryServiceLoader.iterator().next();
Map config = new HashMap();
config.putAll(System.getProperties());
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN);
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "com.test.helloworld");
framework = frameworkFactory.newFramework(config);
framework.start();
List<Bundle> bundleList = new ArrayList<Bundle>();
BundleContext bundleContext = framework.getBundleContext();
File[] bundleJars = new File(BUNDLE_DIRECTORY).listFiles();
Arrays.sort(bundleJars);
for (File bundleJar : bundleJars) {
if(bundleJar.getName().endsWith(".jar")) {
Bundle b = bundleContext.installBundle(bundleJar.toURI().toString());
bundleList.add(b);
}
}
// Start all installed bundles.
for (Bundle bundle : bundleList) {
if(!isFragment(bundle))
bundle.start();
}
} catch (BundleException bundleException) {
throw new RuntimeException(bundleException);
}
}
private static boolean isFragment(Bundle bundle) {
return bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;
}
}