blob: 3e1433ce051fdd3af0234704d76aac6bbb1a86cb (
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
|
/*******************************************************************************
* Copyright (c) 2007, 2010 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.p2.core.helpers;
import java.util.Collection;
import org.osgi.framework.*;
public class ServiceHelper {
/**
* Returns the service described by the given arguments. Note that this is a helper class
* that <b>immediately</b> ungets the service reference. This results in a window where the
* system thinks the service is not in use but indeed the caller is about to use the returned
* service object.
* @param context
* @param clazz the service class
* @return The requested service
*/
public static <T> T getService(BundleContext context, Class<T> clazz) {
if (context == null)
return null;
ServiceReference<T> reference = context.getServiceReference(clazz);
if (reference == null)
return null;
T result = context.getService(reference);
context.ungetService(reference);
return result;
}
public static <T> T getService(BundleContext context, Class<T> clazz, String filter) {
Collection<ServiceReference<T>> references;
try {
references = context.getServiceReferences(clazz, filter);
} catch (InvalidSyntaxException e) {
// TODO Auto-generated catch block
return null;
}
if (references.isEmpty())
return null;
final ServiceReference<T> ref = references.iterator().next();
T result = context.getService(ref);
context.ungetService(ref);
return result;
}
/**
* Returns the service described by the given arguments. Note that this is a helper class
* that <b>immediately</b> ungets the service reference. This results in a window where the
* system thinks the service is not in use but indeed the caller is about to use the returned
* service object.
* @param context
* @param name
* @return The requested service
*/
public static Object getService(BundleContext context, String name) {
if (context == null)
return null;
ServiceReference<?> reference = context.getServiceReference(name);
if (reference == null)
return null;
Object result = context.getService(reference);
context.ungetService(reference);
return result;
}
public static Object getService(BundleContext context, String name, String filter) {
ServiceReference<?>[] references;
try {
references = context.getServiceReferences(name, filter);
} catch (InvalidSyntaxException e) {
// TODO Auto-generated catch block
return null;
}
if (references == null || references.length == 0)
return null;
Object result = context.getService(references[0]);
context.ungetService(references[0]);
return result;
}
}
|