blob: c451d67fc20d4a614fb3245e1255c38d323af346 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util;
import org.osgi.framework.*;
/**
* Bundle activator that will start the bundle when certain
* service is available.
*
* @author Damian Minkov
*/
public abstract class AbstractServiceDependentActivator
implements BundleActivator
{
/**
* The service we are dependent on.
*/
private Object dependentService = null;
/**
* Starts the bundle.
* @param bundleContext the currently valid <tt>BundleContext</tt>.
*/
@Override
public void start(BundleContext bundleContext)
throws Exception
{
setBundleContext(bundleContext);
if(getDependentService(bundleContext) == null)
{
try
{
bundleContext.addServiceListener(
new DependentServiceListener(bundleContext),
'(' + Constants.OBJECTCLASS + '='
+ getDependentServiceClass().getName() + ')');
}
catch (InvalidSyntaxException ise)
{
// Oh, it should not really happen.
}
return;
}
else
{
start(getDependentService(bundleContext));
}
}
/**
* The dependent service is available and the bundle will start.
* @param dependentService the service this activator is waiting.
*/
public abstract void start(Object dependentService);
/**
* The class of the service which this activator is interested in.
* @return the class name.
*/
public abstract Class<?> getDependentServiceClass();
/**
* Setting context to the activator, as soon as we have one.
*
* @param context the context to set.
*/
public abstract void setBundleContext(BundleContext context);
/**
* Obtain the dependent service. Null if missing.
* @param context the current context to use for obtaining.
* @return the dependent service object or null.
*/
private Object getDependentService(BundleContext context)
{
if(dependentService == null)
{
ServiceReference<?> serviceRef
= context.getServiceReference(
getDependentServiceClass().getName());
if(serviceRef != null)
dependentService = context.getService(serviceRef);
}
return dependentService;
}
/**
* Implements a <tt>ServiceListener</tt> which waits for an
* the dependent service implementation to become available, invokes
* {@link #start(Object)} and un-registers itself.
*/
private class DependentServiceListener
implements ServiceListener
{
private final BundleContext context;
DependentServiceListener(BundleContext context)
{
this.context = context;
}
@Override
public void serviceChanged(ServiceEvent serviceEvent)
{
Object depService = getDependentService(context);
if (depService != null)
{
/*
* This ServiceListener has successfully waited for a Service
* implementation to become available so it no longer need to
* listen.
*/
context.removeServiceListener(this);
start(depService);
}
}
}
}
|