Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 027b3478cf36a5cc2469eb2a2a1900bfcc79f4aa (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
package org.eclipse.ecf.internal.osgi.services.distribution;

import java.util.Map;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.AbstractTopologyManager;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.EndpointListener;
import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent;

public class BasicTopologyManagerImpl extends AbstractTopologyManager implements
		EndpointListener {

	private static final boolean allowLoopbackReference = new Boolean(
			System.getProperty(
					"org.eclipse.ecf.osgi.services.discovery.allowLoopbackReference", //$NON-NLS-1$
					"false")).booleanValue(); //$NON-NLS-1$

	private static final String defaultScope = System
			.getProperty("org.eclipse.ecf.osgi.services.discovery.endpointListenerScope"); //$NON-NLS-1$

	private String endpointListenerScope;
	private static final String ALL_SCOPE = "(" //$NON-NLS-1$
			+ org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID
			+ "=*)"; //$NON-NLS-1$

	BasicTopologyManagerImpl(BundleContext context) {
		super(context);
		if (defaultScope != null)
			this.endpointListenerScope = defaultScope;
		else if (allowLoopbackReference)
			endpointListenerScope = ALL_SCOPE;
		else {
			StringBuffer elScope = new StringBuffer("("); //$NON-NLS-1$
			// filter so that local framework uuid is not the same as local
			// value
			elScope.append("!("); //$NON-NLS-1$
			elScope.append(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_FRAMEWORK_UUID);
			elScope.append("="); //$NON-NLS-1$
			elScope.append(getFrameworkUUID());
			elScope.append(")"); //$NON-NLS-1$
			elScope.append(")"); //$NON-NLS-1$
			endpointListenerScope = elScope.toString();
		}
	}

	String[] getScope() {
		return (ALL_SCOPE.equals(endpointListenerScope)) ? new String[] { endpointListenerScope }
				: new String[] { endpointListenerScope, ALL_SCOPE };
	}

	protected String getFrameworkUUID() {
		return super.getFrameworkUUID();
	}

	void exportRegisteredServices(String exportRegisteredSvcsClassname,
			String exportRegisteredSvcsFilter) {
		try {
			final ServiceReference[] existingServiceRefs = getContext()
					.getAllServiceReferences(exportRegisteredSvcsClassname,
							exportRegisteredSvcsFilter);
			// Now export as if the service was registering right now...i.e.
			// perform
			// export
			if (existingServiceRefs != null && existingServiceRefs.length > 0) {
				// After having collected all pre-registered services (with
				// marker prop) we are going to asynchronously remote them.
				// Registering potentially is a long-running operation (due to
				// discovery I/O...) and thus should no be carried out in the
				// OSGi FW thread. (https://bugs.eclipse.org/405027)
				new Thread(new Runnable() {
					public void run() {
						for (int i = 0; i < existingServiceRefs.length; i++) {
							// This method will check the service properties for
							// remote service props. If previously registered as
							// a
							// remote service, it will export the remote
							// service if not it will simply return/skip
							handleServiceRegistering(existingServiceRefs[i]);
						}
					}
				}, "BasicTopologyManagerPreRegSrvExporter").start(); //$NON-NLS-1$
			}
		} catch (InvalidSyntaxException e) {
			logError(
					"exportRegisteredServices", //$NON-NLS-1$
					"Could not retrieve existing service references for exportRegisteredSvcsClassname=" //$NON-NLS-1$
							+ exportRegisteredSvcsClassname
							+ " and exportRegisteredSvcsFilter=" //$NON-NLS-1$
							+ exportRegisteredSvcsFilter, e);
		}
	}

	// EndpointListener impl
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.remoteserviceadmin.EndpointListener#endpointAdded(org
	 * .osgi.service.remoteserviceadmin.EndpointDescription, java.lang.String)
	 */
	public void endpointAdded(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpoint,
			String matchedFilter) {
		if (matchedFilter.equals(endpointListenerScope))
			if (endpoint instanceof EndpointDescription)
				handleECFEndpointAdded((EndpointDescription) endpoint);
			else
				handleNonECFEndpointAdded(this, endpoint);
		else if (matchedFilter.equals(ALL_SCOPE))
			if (endpoint instanceof EndpointDescription)
				handleECFEndpointAdded((EndpointDescription) endpoint);
			else
				advertiseEndpointDescription(endpoint);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.remoteserviceadmin.EndpointListener#endpointRemoved(
	 * org.osgi.service.remoteserviceadmin.EndpointDescription,
	 * java.lang.String)
	 */
	public void endpointRemoved(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpoint,
			String matchedFilter) {
		if (matchedFilter.equals(endpointListenerScope))
			if (endpoint instanceof EndpointDescription)
				handleECFEndpointRemoved((EndpointDescription) endpoint);
			else
				handleNonECFEndpointRemoved(this, endpoint);
		else if (matchedFilter.equals(ALL_SCOPE))
			if (endpoint instanceof EndpointDescription)
				handleECFEndpointRemoved((EndpointDescription) endpoint);
			else
				unadvertiseEndpointDescription(endpoint);
	}

	// EventListenerHook impl
	void event(ServiceEvent event, Map listeners) {
		handleEvent(event, listeners);
	}

	// RemoteServiceAdminListener impl
	void handleRemoteAdminEvent(RemoteServiceAdminEvent event) {
		handleRemoteServiceAdminEvent(event);
	}
}

Back to the top