Skip to main content
summaryrefslogtreecommitdiffstats
blob: d122fdec0362dad3a34f9c284625b84bd5a26994 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*******************************************************************************
 * Copyright (c) 2017, 2019 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.ds;

import java.util.*;
import java.util.Map.Entry;
import org.apache.felix.scr.*;
import org.osgi.framework.*;
import org.osgi.framework.dto.ServiceReferenceDTO;
import org.osgi.service.component.ComponentConstants;
import org.osgi.service.component.ComponentInstance;
import org.osgi.service.component.runtime.ServiceComponentRuntime;
import org.osgi.service.component.runtime.dto.*;

@Deprecated
public class ScrServiceImpl implements ScrService {
	final ServiceComponentRuntime scr;
	final BundleContext context;

	ScrServiceImpl(ServiceComponentRuntime scr, BundleContext context) {
		this.scr = scr;
		this.context = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
	}

	@Override
	public Component[] getComponents() {
		return toComponents(scr.getComponentDescriptionDTOs());
	}

	private Component[] toComponents(Collection<ComponentDescriptionDTO> componentDescriptionDTOs) {
		Collection<Component> components = new ArrayList<Component>();
		for (ComponentDescriptionDTO dto : componentDescriptionDTOs) {
			Component component = toComponent(dto);
			if (component != null) {
				components.add(component);
			}
		}
		return components.isEmpty() ? null : components.toArray(new Component[0]);
	}

	@Override
	public Component getComponent(long componentId) {
		for (ComponentDescriptionDTO dto : scr.getComponentDescriptionDTOs()) {
			Long id = (Long) dto.properties.get(ComponentConstants.COMPONENT_ID);
			if (componentId == id) {
				return toComponent(dto);
			}
		}
		return null;
	}

	private Component toComponent(ComponentDescriptionDTO dto) {
		Collection<ComponentConfigurationDTO> configs = scr.getComponentConfigurationDTOs(dto);
		if (configs.isEmpty()) {
			return null;
		}

		return new ComponentImpl(configs.iterator().next());
	}

	@Override
	public Component[] getComponents(String componentName) {
		Collection<ComponentDescriptionDTO> dtos = new ArrayList<ComponentDescriptionDTO>();
		for (ComponentDescriptionDTO dto : scr.getComponentDescriptionDTOs()) {
			if (componentName.equals(dto.name)) {
				dtos.add(dto);
			}
		}
		return toComponents(dtos);
	}

	@Override
	public Component[] getComponents(Bundle bundle) {
		return toComponents(scr.getComponentDescriptionDTOs(bundle));
	}

	ServiceReference<?> getServiceReference(long id) {
		try {
			ServiceReference<?>[] refs = context.getServiceReferences((String) null, "(" + Constants.SERVICE_ID + "=" + id + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			if (refs != null && refs.length > 0) {
				return refs[0];
			}
		} catch (InvalidSyntaxException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	class ComponentImpl implements Component {
		private final ComponentConfigurationDTO config;

		ComponentImpl(ComponentConfigurationDTO config) {
			this.config = config;
		}

		@Override
		public long getId() {
			return config.id;
		}

		@Override
		public String getName() {
			return config.description.name;
		}

		@Override
		public int getState() {
			switch (config.state) {
				case ComponentConfigurationDTO.ACTIVE :
					return Component.STATE_ACTIVE;
				case ComponentConfigurationDTO.SATISFIED :
					return Component.STATE_REGISTERED;
				case ComponentConfigurationDTO.UNSATISFIED_CONFIGURATION :
					return Component.STATE_UNSATISFIED;
				case ComponentConfigurationDTO.UNSATISFIED_REFERENCE :
					return Component.STATE_UNSATISFIED;
				default :
					break;
			}
			return Component.STATE_DISABLED;
		}

		@Override
		public Bundle getBundle() {
			return context.getBundle(config.description.bundle.id);
		}

		@Override
		public String getFactory() {
			return config.description.factory;
		}

		@Override
		public boolean isServiceFactory() {
			return Constants.SCOPE_BUNDLE.equals(config.description.scope);
		}

		@Override
		public String getClassName() {
			return config.description.implementationClass;
		}

		@Override
		public boolean isDefaultEnabled() {
			return config.description.defaultEnabled;
		}

		@Override
		public boolean isImmediate() {
			return config.description.immediate;
		}

		@Override
		public String[] getServices() {
			return config.description.serviceInterfaces;
		}

		@SuppressWarnings({"rawtypes", "unchecked"})
		@Override
		public Dictionary getProperties() {
			return new Hashtable(config.description.properties);
		}

		@Override
		public Reference[] getReferences() {
			if (config.description.references.length == 0) {
				return null;
			}
			Map<String, ReferenceDTO> referenceDTOs = new HashMap<String, ReferenceDTO>();
			for (ReferenceDTO reference : config.description.references) {
				referenceDTOs.put(reference.name, reference);
			}
			Map<String, SatisfiedReferenceDTO> satisfiedDTOs = new HashMap<String, SatisfiedReferenceDTO>();
			for (SatisfiedReferenceDTO satisfied : config.satisfiedReferences) {
				satisfiedDTOs.put(satisfied.name, satisfied);
			}
			Map<String, UnsatisfiedReferenceDTO> unsatisfiedDTOs = new HashMap<String, UnsatisfiedReferenceDTO>();
			for (UnsatisfiedReferenceDTO unsatisfied : config.unsatisfiedReferences) {
				unsatisfiedDTOs.put(unsatisfied.name, unsatisfied);
			}
			return toReferences(referenceDTOs, satisfiedDTOs, unsatisfiedDTOs);
		}

		private Reference[] toReferences(Map<String, ReferenceDTO> referenceDTOs, Map<String, SatisfiedReferenceDTO> satisfiedDTOs, Map<String, UnsatisfiedReferenceDTO> unsatisfiedDTOs) {
			Collection<Reference> references = new ArrayList<Reference>();
			for (Entry<String, SatisfiedReferenceDTO> satisfied : satisfiedDTOs.entrySet()) {
				references.add(new SatsifiedReference(satisfied.getValue(), referenceDTOs.get(satisfied.getValue().name)));
			}
			for (Entry<String, UnsatisfiedReferenceDTO> unsatisfied : unsatisfiedDTOs.entrySet()) {
				references.add(new UnsatsifiedReference(unsatisfied.getValue(), referenceDTOs.get(unsatisfied.getValue().name)));
			}
			return references.toArray(new Reference[0]);
		}

		@Override
		public ComponentInstance<Object> getComponentInstance() {
			throw new UnsupportedOperationException("Not supported."); //$NON-NLS-1$
		}

		@Override
		public String getActivate() {
			return config.description.activate;
		}

		@Override
		public boolean isActivateDeclared() {
			return config.description.activate != null;
		}

		@Override
		public String getDeactivate() {
			return config.description.deactivate;
		}

		@Override
		public boolean isDeactivateDeclared() {
			return config.description.deactivate != null;
		}

		@Override
		public String getModified() {
			return config.description.modified;
		}

		@Override
		public String getConfigurationPolicy() {
			return config.description.configurationPolicy;
		}

		@Override
		public void enable() {
			scr.disableComponent(config.description);
		}

		@Override
		public void disable() {
			scr.disableComponent(config.description);
		}
	}

	abstract class ReferenceBase implements Reference {
		protected final ReferenceDTO reference;

		ReferenceBase(ReferenceDTO reference) {
			this.reference = reference;
		}

		@Override
		public String getName() {
			return reference.name;
		}

		@Override
		public String getServiceName() {
			return reference.interfaceName;
		}

		@Override
		public boolean isOptional() {
			return reference.cardinality.startsWith("0"); //$NON-NLS-1$
		}

		@Override
		public boolean isMultiple() {
			return reference.cardinality.startsWith("1"); //$NON-NLS-1$
		}

		@Override
		public boolean isStatic() {
			return "static".equals(reference.policy); //$NON-NLS-1$
		}

		@Override
		public String getTarget() {
			return reference.target;
		}

		@Override
		public String getBindMethodName() {
			return reference.bind;
		}

		@Override
		public String getUnbindMethodName() {
			return reference.unbind;
		}

		@Override
		public String getUpdatedMethodName() {
			return reference.updated;
		}
	}

	class SatsifiedReference extends ReferenceBase {
		private final SatisfiedReferenceDTO satisfied;

		SatsifiedReference(SatisfiedReferenceDTO satisifed, ReferenceDTO reference) {
			super(reference);
			this.satisfied = satisifed;
		}

		@SuppressWarnings("rawtypes")
		@Override
		public ServiceReference[] getServiceReferences() {
			Collection<ServiceReference<?>> serviceReferences = new ArrayList<ServiceReference<?>>();
			for (ServiceReferenceDTO serviceRefDTO : satisfied.boundServices) {
				ServiceReference<?> ref = getServiceReference(serviceRefDTO.id);
				if (ref != null) {
					serviceReferences.add(ref);
				}
			}
			return serviceReferences.isEmpty() ? null : serviceReferences.toArray(new ServiceReference[0]);
		}

		@Override
		public boolean isSatisfied() {
			return true;
		}
	}

	class UnsatsifiedReference extends ReferenceBase {
		private final UnsatisfiedReferenceDTO unsatisfied;

		UnsatsifiedReference(UnsatisfiedReferenceDTO unsatisifed, ReferenceDTO reference) {
			super(reference);
			this.unsatisfied = unsatisifed;
		}

		@Override
		@SuppressWarnings("rawtypes")
		public ServiceReference[] getServiceReferences() {
			Collection<ServiceReference<?>> serviceReferences = new ArrayList<ServiceReference<?>>();
			for (ServiceReferenceDTO serviceRefDTO : unsatisfied.targetServices) {
				ServiceReference<?> ref = getServiceReference(serviceRefDTO.id);
				if (ref != null) {
					serviceReferences.add(ref);
				}
			}
			return serviceReferences.isEmpty() ? null : serviceReferences.toArray(new ServiceReference[0]);
		}

		@Override
		public boolean isSatisfied() {
			return false;
		}
	}

}

Back to the top