blob: 73a6a4fe355d163034c1bc5a2b6dd3100bf45850 (
plain) (
blame)
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
|
/*******************************************************************************
* Copyright (c) 2010 Markus Alexander Kuppe.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Alexander Kuppe (ecf-dev_eclipse.org <at> lemmster <dot> de) - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.tests.provider.dnssd;
import java.util.Dictionary;
import java.util.Hashtable;
import org.eclipse.ecf.discovery.IDiscoveryLocator;
import org.eclipse.ecf.provider.dnssd.IDnsSdDiscoveryConstants;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
public class Activator implements BundleActivator {
private static Activator instance;
private IDiscoveryLocator discoveryLocator;
private ServiceListener listener;
private final Object lock = new Object();
public Activator() {
instance = this;
}
/* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(final BundleContext context) throws Exception {
Filter filter = null;
final ServiceReference configAdminServiceRef = context
.getServiceReference(ConfigurationAdmin.class.getName());
if (configAdminServiceRef != null) {
ConfigurationAdmin configAdmin = (ConfigurationAdmin) context
.getService(configAdminServiceRef);
Configuration config = configAdmin.createFactoryConfiguration(
DnsSdDiscoveryServiceTest.ECF_DISCOVERY_DNSSD, null);
Dictionary properties = new Hashtable();
properties.put(IDnsSdDiscoveryConstants.CA_SEARCH_PATH, new String[]{DnsSdDiscoveryServiceTest.DOMAIN});
properties.put(IDnsSdDiscoveryConstants.CA_RESOLVER, "8.8.8.8");
config.update(properties);
filter = context.createFilter("(" + Constants.SERVICE_PID + "=" + config.getPid() + ")");
}
// add the service listener
listener = new ServiceListener() {
public void serviceChanged(ServiceEvent event) {
switch (event.getType()) {
case ServiceEvent.REGISTERED:
ServiceReference serviceReference = event.getServiceReference();
discoveryLocator = (IDiscoveryLocator) context.getService(serviceReference);
synchronized (lock) {
lock.notifyAll();
}
}
}
};
context.addServiceListener(listener, filter.toString());
// try to get the service initially
ServiceReference[] references = null;
try {
references = context.getServiceReferences(IDiscoveryLocator.class.getName(), filter.toString());
} catch (InvalidSyntaxException e) {
// may never happen
e.printStackTrace();
}
if(references == null) {
return;
}
for (int i = 0; i < references.length;) {
ServiceReference serviceReference = references[i];
discoveryLocator = (IDiscoveryLocator) context.getService(serviceReference);
synchronized (lock) {
lock.notifyAll();
}
}
}
/* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
if(listener != null) {
context.removeServiceListener(listener);
listener = null;
}
}
public static Activator getDefault() {
return instance;
}
public IDiscoveryLocator getDiscoveryLocator() {
if (discoveryLocator == null) {
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
// may never happen
e.printStackTrace();
return null;
}
}
return discoveryLocator;
}
}
|