You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Tamas_Paulo <pa...@gmail.com> on 2010/07/13 10:57:03 UTC

ConfigurationAdmin/ServiceTracker

Hi all,

I faced a curious problem, when trying to use ConfigurationAdmin using the
two below approaches I get different results, the first one works (sometimes
it tries to get service too early, but otherwise OK). But with the second
one I get NullPointerException on this line (stack trace below):
		Configuration configuration = m_configAdmin.getConfiguration(testPid);
What is the difference between the two, am I doing something wrong?


1.)
		m_configAdmin = (ConfigurationAdmin)
m_bundleContext.getService(m_bundleContext
				.getServiceReference(ConfigurationAdmin.class.getName()));
---------------------------------------------------------------------------------------------
2.)
		ServiceTracker tracker = new ServiceTracker(m_bundleContext, 
				ConfigurationAdmin.class.getName(), null);
		tracker.open();
		m_configAdmin = (ConfigurationAdmin)tracker.waitForService(2000);
		tracker.close();

Relevant part of stack trace:
java.lang.NullPointerException
	at
org.apache.felix.cm.impl.ConfigurationAdminImpl.getConfiguration(ConfigurationAdminImpl.java:93)
        at "my call"

Thanks in advance:
Tamás
-- 
View this message in context: http://old.nabble.com/ConfigurationAdmin-ServiceTracker-tp29148119p29148119.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: ConfigurationAdmin/ServiceTracker

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

On 13.07.2010 10:57, Tamas_Paulo wrote:
> 
> Hi all,
> 
> I faced a curious problem, when trying to use ConfigurationAdmin using the
> two below approaches I get different results, the first one works (sometimes
> it tries to get service too early, but otherwise OK). But with the second
> one I get NullPointerException on this line (stack trace below):
> 		Configuration configuration = m_configAdmin.getConfiguration(testPid);
> What is the difference between the two, am I doing something wrong?
> 
> 
> 1.)
> 		m_configAdmin = (ConfigurationAdmin)
> m_bundleContext.getService(m_bundleContext
> 				.getServiceReference(ConfigurationAdmin.class.getName()));
> ---------------------------------------------------------------------------------------------
> 2.)
> 		ServiceTracker tracker = new ServiceTracker(m_bundleContext, 
> 				ConfigurationAdmin.class.getName(), null);
> 		tracker.open();
> 		m_configAdmin = (ConfigurationAdmin)tracker.waitForService(2000);
> 		tracker.close();
> 
> Relevant part of stack trace:
> java.lang.NullPointerException
> 	at
> org.apache.felix.cm.impl.ConfigurationAdminImpl.getConfiguration(ConfigurationAdminImpl.java:93)
>         at "my call"

I think, the gotcha is the "tracker.close()" call. This closes the
tracker and releases (ungets) all services. In the case of the Felix
ConfigurationAdmin service this causes the ConfigurationAdminImpl object
(assigned to the m_configAdmin field) to be disposed, i.e. unusable.
This causes the NullPointerException.

Thus, the fix here is to not call the ServiceTracker.close() method
before you use the service.

Regards
Felix


> 
> Thanks in advance:
> Tamás


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Gotcha: Don't delete the system directory.

Posted by peter lawrey <pe...@edgeci.com>.
Hello,
  Here is a little gotcha which had me confused for a while...

If you have managed instances of karaf, you will find that an empty "system" directory is created.  While the directory is empty, your instance will not start unless it exists.

This is because in class org.apache.felix.karaf.main.Main line 902, there is the following code.

for(File file: dir.listFiles()) {

This causes a NullPointerException if the directory doesn't exist, this exception is silently discarded by karaf as it doesn't log any errors which occur too early in the start up.

So if your instance doesn't start, check you have a system directory. ;)

Cheers,
  Peter.