|
Lines 10-30
Link Here
|
| 10 |
******************************************************************************/ |
10 |
******************************************************************************/ |
| 11 |
package org.eclipse.ecf.internal.provider.discovery; |
11 |
package org.eclipse.ecf.internal.provider.discovery; |
| 12 |
|
12 |
|
| 13 |
import java.util.*; |
13 |
import org.osgi.framework.BundleActivator; |
| 14 |
import org.eclipse.ecf.core.ContainerConnectException; |
14 |
import org.osgi.framework.BundleContext; |
| 15 |
import org.eclipse.ecf.core.identity.IDCreateException; |
|
|
| 16 |
import org.eclipse.ecf.core.identity.IDFactory; |
| 17 |
import org.eclipse.ecf.core.util.Trace; |
| 18 |
import org.eclipse.ecf.discovery.service.IDiscoveryService; |
| 19 |
import org.eclipse.ecf.provider.discovery.CompositeDiscoveryContainer; |
| 20 |
import org.osgi.framework.*; |
| 21 |
import org.osgi.util.tracker.ServiceTracker; |
| 22 |
|
15 |
|
| 23 |
public class Activator implements BundleActivator { |
16 |
public class Activator implements BundleActivator { |
| 24 |
// The shared instance |
17 |
// The shared instance |
| 25 |
private static Activator plugin; |
18 |
private static Activator plugin; |
| 26 |
public static final String PLUGIN_ID = "org.eclipse.ecf.provider.discovery"; //$NON-NLS-1$ |
19 |
public static final String PLUGIN_ID = "org.eclipse.ecf.provider.discovery"; //$NON-NLS-1$ |
| 27 |
|
20 |
|
|
|
21 |
private static BundleContext context; |
| 22 |
|
| 28 |
/** |
23 |
/** |
| 29 |
* Returns the shared instance |
24 |
* Returns the shared instance |
| 30 |
* |
25 |
* |
|
Lines 45-133
Link Here
|
| 45 |
* (non-Javadoc) |
40 |
* (non-Javadoc) |
| 46 |
* @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext) |
41 |
* @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext) |
| 47 |
*/ |
42 |
*/ |
| 48 |
public void start(final BundleContext context) throws Exception { |
43 |
public void start(final BundleContext ctxt) throws Exception { |
| 49 |
Properties props = new Properties(); |
44 |
context = ctxt; |
| 50 |
props.put(IDiscoveryService.CONTAINER_ID, IDFactory.getDefault().createStringID("org.eclipse.ecf.provider.discovery.CompositeDiscoveryContainer")); //$NON-NLS-1$ |
|
|
| 51 |
props.put(IDiscoveryService.CONTAINER_NAME, CompositeDiscoveryContainer.NAME); |
| 52 |
props.put(Constants.SERVICE_RANKING, new Integer(1000)); |
| 53 |
context.registerService(IDiscoveryService.class.getName(), new ServiceFactory() { |
| 54 |
|
| 55 |
/* (non-Javadoc) |
| 56 |
* @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration) |
| 57 |
*/ |
| 58 |
public Object getService(Bundle bundle, ServiceRegistration registration) { |
| 59 |
|
| 60 |
// get all previously registered IDS from OSGi (but not this one) |
| 61 |
Filter filter = null; |
| 62 |
try { |
| 63 |
String filter2 = "(&(" + Constants.OBJECTCLASS + "=" + IDiscoveryService.class.getName() + ")(!(" + IDiscoveryService.CONTAINER_NAME + "=" + CompositeDiscoveryContainer.NAME + ")))"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
| 64 |
filter = context.createFilter(filter2); |
| 65 |
} catch (InvalidSyntaxException e2) { |
| 66 |
Trace.catching(Activator.PLUGIN_ID, Activator.PLUGIN_ID + "/debug/methods/catching", this.getClass(), "getService(Bundle, ServiceRegistration)", e2); //$NON-NLS-1$ //$NON-NLS-2$ |
| 67 |
return null; |
| 68 |
} |
| 69 |
ServiceTracker tracker = new ServiceTracker(context, filter, null); |
| 70 |
tracker.open(); |
| 71 |
Object[] services = tracker.getServices(); |
| 72 |
tracker.close(); |
| 73 |
List discoveries = services == null ? new ArrayList() : new ArrayList(Arrays.asList(services)); |
| 74 |
|
| 75 |
// register the composite discovery service) |
| 76 |
final CompositeDiscoveryContainer cdc; |
| 77 |
try { |
| 78 |
cdc = new CompositeDiscoveryContainer(discoveries); |
| 79 |
} catch (IDCreateException e1) { |
| 80 |
Trace.catching(Activator.PLUGIN_ID, Activator.PLUGIN_ID + "/debug/methods/catching", this.getClass(), "getService(Bundle, ServiceRegistration)", e1); //$NON-NLS-1$ //$NON-NLS-2$ |
| 81 |
return null; |
| 82 |
} |
| 83 |
try { |
| 84 |
cdc.connect(null, null); |
| 85 |
} catch (ContainerConnectException e) { |
| 86 |
Trace.catching(Activator.PLUGIN_ID, Activator.PLUGIN_ID + "/debug/methods/catching", this.getClass(), "getService(Bundle, ServiceRegistration)", e); //$NON-NLS-1$ //$NON-NLS-2$ |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
// add a service listener to add/remove IDS dynamically |
| 91 |
try { |
| 92 |
context.addServiceListener(new ServiceListener() { |
| 93 |
/* (non-Javadoc) |
| 94 |
* @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent) |
| 95 |
*/ |
| 96 |
public void serviceChanged(ServiceEvent arg0) { |
| 97 |
IDiscoveryService anIDS = (IDiscoveryService) context.getService(arg0.getServiceReference()); |
| 98 |
switch (arg0.getType()) { |
| 99 |
case ServiceEvent.REGISTERED : |
| 100 |
cdc.addContainer(anIDS); |
| 101 |
break; |
| 102 |
case ServiceEvent.UNREGISTERING : |
| 103 |
cdc.removeContainer(anIDS); |
| 104 |
break; |
| 105 |
default : |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
}, "(" + Constants.OBJECTCLASS + "=" + IDiscoveryService.class.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 111 |
} catch (InvalidSyntaxException e) { |
| 112 |
// nop |
| 113 |
} |
| 114 |
return cdc; |
| 115 |
} |
| 116 |
|
| 117 |
/* (non-Javadoc) |
| 118 |
* @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration, java.lang.Object) |
| 119 |
*/ |
| 120 |
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { |
| 121 |
// nop |
| 122 |
} |
| 123 |
}, props); |
| 124 |
} |
45 |
} |
| 125 |
|
46 |
|
| 126 |
/* |
47 |
/* |
| 127 |
* (non-Javadoc) |
48 |
* (non-Javadoc) |
| 128 |
* @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) |
49 |
* @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) |
| 129 |
*/ |
50 |
*/ |
| 130 |
public void stop(BundleContext context) throws Exception { |
51 |
public void stop(BundleContext ctxt) throws Exception { |
| 131 |
plugin = null; |
52 |
plugin = null; |
|
|
53 |
context = null; |
| 54 |
} |
| 55 |
|
| 56 |
public static BundleContext getContext() { |
| 57 |
return context; |
| 132 |
} |
58 |
} |
| 133 |
} |
59 |
} |