Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b60c52fbb686513fcabd85b1022e4ad8a3c860ba (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Copyright (c) 2009 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.provider.dnssd;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.discovery.IDiscoveryAdvertiser;
import org.eclipse.ecf.discovery.IDiscoveryLocator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;

public class Activator implements BundleActivator {

	public static final String PLUGIN_ID = "org.eclipse.ecf.provider.dnssd"; //$NON-NLS-1$
	public static final String DISCOVERY_CONTAINER_NAME_VALUE = "ecf.discovery.dnssd"; //$NON-NLS-1$
	public static final String LOCATOR = ".locator"; //$NON-NLS-1$
	public static final String ADVERTISER = ".advertiser"; //$NON-NLS-1$

	private static final String DISCOVERY_CONTAINER_NAME_KEY = "org.eclipse.ecf.discovery.containerName"; //$NON-NLS-1$
	
	private final Map serviceRegistrations = new HashMap();
	private volatile BundleContext context;

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		this.context = context;
		
		// register a managed factory for the locator service
		final Properties locCmProps = new Properties();
		locCmProps.put(Constants.SERVICE_PID, DISCOVERY_CONTAINER_NAME_VALUE + LOCATOR);
		context.registerService(ManagedServiceFactory.class.getName(), new DnsSdManagedServiceFactory(DnsSdDiscoveryLocator.class), locCmProps);
		
		// register the locator service
		final Properties locProps = new Properties();
		locProps.put(DISCOVERY_CONTAINER_NAME_KEY, DISCOVERY_CONTAINER_NAME_VALUE + LOCATOR);
		locProps.put(Constants.SERVICE_RANKING, new Integer(750));
		serviceRegistrations.put(null, context.registerService(IDiscoveryLocator.class.getName(), new DnsSdServiceFactory(DnsSdDiscoveryLocator.class), locProps));

		// register a managed factory for the advertiser service
		final Properties advCmProps = new Properties();
		advCmProps.put(Constants.SERVICE_PID, DISCOVERY_CONTAINER_NAME_VALUE + ADVERTISER);
		context.registerService(ManagedServiceFactory.class.getName(), new DnsSdManagedServiceFactory(DnsSdDiscoveryAdvertiser.class), advCmProps);
		
		// register the advertiser service
		final Properties advProps = new Properties();
		advProps.put(DISCOVERY_CONTAINER_NAME_KEY, DISCOVERY_CONTAINER_NAME_VALUE + ADVERTISER);
		advProps.put(Constants.SERVICE_RANKING, new Integer(750));
		serviceRegistrations.put(null, context.registerService(IDiscoveryAdvertiser.class.getName(), new DnsSdServiceFactory(DnsSdDiscoveryAdvertiser.class), advProps));
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		if (serviceRegistrations != null) {
			for (final Iterator itr = serviceRegistrations.values().iterator(); itr.hasNext();) {
				final ServiceRegistration serviceRegistration = (ServiceRegistration) itr.next();
				disposeServiceRegistration(serviceRegistration);
			}
		}
		this.context = null;
	}
	
	/**
	 * @param serviceRegistration disconnects the underlying IContainer and unregisters the service
	 */
	private void disposeServiceRegistration(ServiceRegistration serviceRegistration) {
		final ServiceReference reference = serviceRegistration.getReference();
		final IContainer aContainer = (DnsSdDiscoveryContainerAdapter) context.getService(reference);
		
		serviceRegistration.unregister();
		final IContainer container = (IContainer) aContainer.getAdapter(IContainer.class);
		container.dispose();
		container.disconnect();
	}
	
	/**
	 * A ManagedServiceFactory capable to handle DnsSdDiscoveryContainerAdapters
	 */
	private class DnsSdManagedServiceFactory implements ManagedServiceFactory {
		private final Class containerClass;

		public DnsSdManagedServiceFactory(Class aContainerClass) {
			containerClass = aContainerClass;
		}

		/* (non-Javadoc)
		 * @see org.osgi.service.cm.ManagedServiceFactory#getName()
		 */
		public String getName() {
			return this.getClass().getName();
		}

		/* (non-Javadoc)
		 * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String, java.util.Dictionary)
		 */
		public void updated(String pid, Dictionary properties)
				throws ConfigurationException {
			if(properties != null) {
				DnsSdDiscoveryContainerAdapter adapter = null;
				DnsSdServiceTypeID targetID = null;
				try {
					// get existing or create new discoverycontainer
					final ServiceRegistration serviceRegistration = (ServiceRegistration) serviceRegistrations.get(pid);
					if(serviceRegistration != null) {
						adapter = (DnsSdDiscoveryContainerAdapter) context.getService(serviceRegistration.getReference());
						targetID = (DnsSdServiceTypeID) adapter.getConnectedID();
					} else {
						adapter = (DnsSdDiscoveryContainerAdapter) containerClass.newInstance();
						targetID = new DnsSdServiceTypeID();
					}

					// apply configuration
					final String[] searchPaths = (String[]) properties.get(IDnsSdDiscoveryConstants.CA_SEARCH_PATH);
					if(searchPaths != null) {
						targetID.setSearchPath(searchPaths);
					}

					final String resolver = (String) properties.get(IDnsSdDiscoveryConstants.CA_RESOLVER);
					if(resolver != null) {
						adapter.setResolver(resolver);
					}
					
					final String tsigKey = (String) properties.get(IDnsSdDiscoveryConstants.CA_TSIG_KEY);
					if(tsigKey != null) {
						final String tsigKeyName = (String) properties.get(IDnsSdDiscoveryConstants.CA_TSIG_KEY_NAME);
						adapter.setTsigKey(tsigKeyName, tsigKey);
					}
					
					// finally connect container and keep ser reg for later updates/deletes
					if(serviceRegistration == null) {
						final Properties props = new Properties();
						props.put(Constants.SERVICE_PID, pid);
						adapter.connect(targetID, null);
						serviceRegistrations.put(pid, context.registerService(IDiscoveryLocator.class.getName(), adapter, props));
					}
				} catch (ContainerConnectException e) {
					throw new ConfigurationException("IDnsSdDiscoveryConstants properties", e.getLocalizedMessage(), e); //$NON-NLS-1$
				} catch (ClassCastException cce) {
					throw new ConfigurationException("IDnsSdDiscoveryConstants properties", cce.getLocalizedMessage(), cce); //$NON-NLS-1$
				} catch (InstantiationException e) {
					// may never happen
					throw new ConfigurationException("InstantiationException", e.getLocalizedMessage(), e); //$NON-NLS-1$
				} catch (IllegalAccessException e) {
					// may never happen
					throw new ConfigurationException("IllegalAccessException", e.getLocalizedMessage(), e); //$NON-NLS-1$
				}
			}
		}

		/* (non-Javadoc)
		 * @see org.osgi.service.cm.ManagedServiceFactory#deleted(java.lang.String)
		 */
		public void deleted(String pid) {
			final ServiceRegistration serviceRegistration = (ServiceRegistration) serviceRegistrations.get(pid);
			disposeServiceRegistration(serviceRegistration);
		}
	}
	
	/**
	 * A ServiceFactory capable to handle DnsSdDiscoveryContainerAdapters
	 */
	public class DnsSdServiceFactory implements ServiceFactory {
		private volatile DnsSdDiscoveryContainerAdapter container;
		private final Class containerClass;

		public DnsSdServiceFactory(Class aDiscoveryContainerClass) {
			containerClass = aDiscoveryContainerClass;
		}

		/* (non-Javadoc)
		 * @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration)
		 */
		public Object getService(Bundle bundle, ServiceRegistration registration) {
			if (container == null) {
				try {
					container = (DnsSdDiscoveryContainerAdapter) containerClass.newInstance();
					container.connect(null, null);
				} catch (final ContainerConnectException e) {
					// may never happen
					e.printStackTrace();
					container = null;
				} catch (InstantiationException e) {
					// may never happen
					e.printStackTrace();
					container = null;
				} catch (IllegalAccessException e) {
					// may never happen
					e.printStackTrace();
					container = null;
				}
			}
			return container;
		}

		/* (non-Javadoc)
		 * @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration, java.lang.Object)
		 */
		public void ungetService(Bundle bundle,
				ServiceRegistration registration, Object service) {
			// nop
		}
	}
}

Back to the top