| OSGi Services query fails [message #825802] |
Wed, 21 March 2012 05:19  |
Ludwig Moser Messages: 201 Registered: July 2009 |
Senior Member |
|
|
hi guys!
i wrote a little helper class, which allows me to monitor all my osgi services provided by my application.
so when i launch my Application from within my IDE, everything is working fine and all the services get listed.
but when i export my application and launch this application i run into an nullpointer exception (@see // refs is null here!!)
can someone explain me why this happens?
and/or how to fix it?
thanks in advance
lumo
import java.util.HashMap;
public class ServiceMonitor {
public static final String SERVICERUNNING = "service_running";
public static final String SERVICESTOPPED = "service_stopped";
private static final boolean DEBUG = false;
private static ServiceMonitor instance;
private HashMap<String, Object> services;
private BundleContext context;
private String[] infos;
private ServiceMonitor() {
// construct the singleton instance
}
public static final ServiceMonitor getInstance() {
if (instance == null) {
instance = new ServiceMonitor();
}
return instance;
}
public static void start(final BundleContext context) throws Exception {
System.err.println(ServiceMonitor.class.getName() + " started.");
System.out.println("context = " + context);
if (getInstance().context == null) {
getInstance().context = context;
getInstance().services = new HashMap<String, Object>();
}
if (DEBUG) {
System.out.println(String.format(
"ServiceMonitor is NOW ready @ %s", instance.context));
}
}
public static void stop() {
}
public static String[] getServices() {
final String filter = "(objectclass=lumo.osgi.service.*)";
System.out.println("getInstance().context = " + getInstance().context);
if (getInstance().infos == null) {
try {
final ServiceReference<?>[] refs = getInstance().context
.getAllServiceReferences(null, filter);
// refs is null here!!
System.out.println("refs == null " + (refs == null));
getInstance().infos = new String[refs.length];
for (int i = 0; i < refs.length; i++) {
String name = (String) refs[i]
.getProperty("component.name");
name = name.substring(name.lastIndexOf(".") + 1);
System.err.println(String.format(
"Service %d [%s] of %d Services checked.", i + 1,
name, refs.length));
final String clazz = (String) refs[i]
.getProperty("component.name");
if (!getInstance().services.containsKey(clazz)) {
getInstance().services.put(clazz,
getInstance().context.getService(refs[i]));
}
getInstance().infos[i] = clazz;
}
} catch (final InvalidSyntaxException e) {
e.printStackTrace();
}
}
return getInstance().infos;
}
public static boolean isServiceRunning(final Class<?> clazz) {
final String[] infos = getServices();
for (final String info : infos) {
if (info.equals(clazz.getName())) {
return true;
}
}
return false;
}
/**
* alias for getService
*
* @param clazz
* @return
* @see #getService(Class)
*/
public static Object addService(final Class<?> clazz) {
return getService(clazz);
}
/**
* this function automatically adds a service if its not there...
*
* @param clazz
* @return
*/
public static Object getService(final Class<?> clazz) {
if (clazz != null) {
if (!getInstance().services.containsKey(clazz.getName())) {
final ServiceReference<?> ref = getInstance().context
.getServiceReference(clazz.getName());
if (ref == null) {
return null;
} else {
getInstance().services.put(clazz.getName(),
getInstance().context.getService(ref));
return getInstance().services.get(clazz.getName());
}
}
return getInstance().services.get(clazz.getName());
}
return null;
}
/**
* true, if the service got removed, otherwise false (even if class == null)<br>
* not working yet
*
* @param clazz
* @return
*/
public static Boolean removeService(final Class<?> clazz) {
if (clazz != null) {
boolean result = false;
if (getInstance().services.containsKey(clazz.getName())) {
final ServiceReference<?> ref = getInstance().context
.getServiceReference(clazz.getName());
if (ref == null) {
return Boolean.valueOf(false);
} else {
result = getInstance().context.ungetService(ref);
getInstance().services.remove(clazz.getName());
if (DEBUG) {
System.out.println(String.format("%s got %s removed.",
clazz.getSimpleName(), result ? "successfully"
: "not"));
}
return result;
}
}
if (DEBUG) {
System.out
.println(String.format("%s got %s removed.", clazz
.getSimpleName(), result ? "successfully"
: "not"));
}
return result;
}
return Boolean.valueOf(false);
}
}
UPDATE: i now removed this class from my project and replaced it by normal code (without static references.)
same result! the services are not found when i exported the product, but it's working fine when i launch the product from ide!
UPDATE2: it seems the services are simply not starting in the exported product. although i defined em Quote:enabled="true" immediate="true" in the xml...
[Updated on: Wed, 21 March 2012 09:01] Report message to a moderator
|
|
|
|
| Re: OSGi Services query fails [message #825946 is a reply to message #825932] |
Wed, 21 March 2012 09:01   |
Ludwig Moser Messages: 201 Registered: July 2009 |
Senior Member |
|
|
thanks for your reply Tom,
for your first quote, this code is commented out (forgot to delete it from the quote)
so it should not affect anything (cant remember but somewhere i read that you can also pass the superclass of the bundle, where it stated out that this constatnt.objectclass would return all bundles... - but its not in use anyway)
the // refs is null here!! was meant for the not commented out code 
i have the two plugins
org.eclipse.core.runtime
org.eclipse.equinox.ds
as autostart and starlevel (default) 0
how can i ensure the ds is activated before my code?
UPDATE: i switched eclipse version since my last export (where the osgi services were running fine) and when i compare my config ini's
the old one is including the org.eclipse.equinox.ds as osgi.bundle
whie the new one does NOT, BUT it says: equinox.ds=true
i am currently using eclipse 3.7, last export was with 3.6 and the plugins were created with eclipse 3.4 (but this will not make any difference i guess...)
[Updated on: Wed, 21 March 2012 09:12] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.17215 seconds