Package: OsgiFrameworkUtils

OsgiFrameworkUtils

nameinstructionbranchcomplexitylinemethod
OsgiFrameworkUtils()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getScopeName(Bundle)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getService(BundleContext, Class)
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getServices(BundleContext, Class)
M: 60 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
isBundleActive(Bundle)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isScoped(Bundle)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
sameScope(Bundle, Bundle)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.kernel.osgi.framework;
13:
14: import java.util.ArrayList;
15: import java.util.Collections;
16: import java.util.List;
17:
18: import org.osgi.framework.Bundle;
19: import org.osgi.framework.BundleContext;
20: import org.osgi.framework.InvalidSyntaxException;
21: import org.osgi.framework.ServiceReference;
22:
23: /**
24: * Utility methods for working the OSGi framework and {@link Bundle Bundles}.
25: * <p/>
26: *
27: * <strong>Concurrent Semantics</strong><br />
28: *
29: * Threadsafe.
30: *
31: */
32: public final class OsgiFrameworkUtils {
33:
34: private static final String HEADER_MODULE_SCOPE = "Module-Scope";
35:
36: /**
37: * Gets the scope for the supplied {@link Bundle}.
38: *
39: * @param bundle the <code>Bundle</code>.
40: * @return the scope, or <code>null</code> if no scope is specified.
41: */
42: public static String getScopeName(Bundle bundle) {
43: return (String) bundle.getHeaders().get(HEADER_MODULE_SCOPE);
44: }
45:
46: /**
47: * Queries whether the supplied {@link Bundle} is scoped.
48: *
49: * @param bundle the <code>Bundle</code>.
50: * @return <code>true</code> if the <code>Bundle</code> is scoped, otherwise <code>false</code>.
51: */
52: public static boolean isScoped(Bundle bundle) {
53: String scope = getScopeName(bundle);
54:• return scope != null && scope.length() > 0;
55: }
56:
57: /**
58: * Queries whether both supplied {@link Bundle Bundles} are in the same scope.
59: *
60: * @param a the first <code>Bundle</code>.
61: * @param b the second <code>Bundle</code>.
62: * @return <code>true</code> if both <code>Bundles</code> are in the same scope.
63: */
64: public static boolean sameScope(Bundle a, Bundle b) {
65: String scopeA = getScopeName(a);
66:• if (scopeA == null || scopeA.length() == 0) {
67: return false;
68: } else {
69: return scopeA.equals(getScopeName(b));
70: }
71: }
72:
73: /**
74: * Indicates whether or not the supplied {@link Bundle} is active.
75: *
76: * @param bundle The bundle to query
77: * @return <code>true</code> if the supplied bundle is active, otherwise <code>false</code>.
78: * @see Bundle#getState()
79: * @see Bundle#ACTIVE
80: */
81: public static boolean isBundleActive(Bundle bundle) {
82:• return bundle.getState() == Bundle.ACTIVE;
83: }
84:
85: @SuppressWarnings("unchecked")
86: public static <T> OsgiServiceHolder<T> getService(BundleContext bundleContext, Class<T> clazz) {
87: final ServiceReference<T> reference = (ServiceReference<T>) bundleContext.getServiceReference(clazz.getName());
88:• if (reference != null) {
89: final T service = (T) bundleContext.getService(reference);
90: return new StandardOsgiServiceHolder<T>(service, reference);
91: } else {
92: return null;
93: }
94: }
95:
96: /**
97: * Returns an ordered list of {@link OsgiServiceHolder OsgiServiceHolders}, one for each service in the service
98: * registry which was published under the supplied <code>serviceType</code>. The service lookup is performed using
99: * the supplied <code>bundleContext</code>.
100: * <p/>
101: * The ordering of the <code>OsgiServiceHolder</code>s is determined by the
102: * {@link ServiceReference#compareTo(Object) ordering} of the encapsulated {@link ServiceReference
103: * ServiceReferences}.
104: * <p/>
105: * @param bundleContext in which to lookup services
106: * @param serviceType of service to look for
107: * @param <T> of service
108: * @return list of {@link OsgiServiceHolder}s
109: */
110: @SuppressWarnings("unchecked")
111: public static <T> List<OsgiServiceHolder<T>> getServices(BundleContext bundleContext, Class<T> serviceType) {
112: List<OsgiServiceHolder<T>> serviceHolders = new ArrayList<OsgiServiceHolder<T>>();
113: try {
114: ServiceReference<T>[] serviceReferences = (ServiceReference<T>[]) bundleContext.getServiceReferences(serviceType.getName(), null);
115:• if (serviceReferences != null) {
116:• for (ServiceReference<T> serviceReference : serviceReferences) {
117: T service = (T) bundleContext.getService(serviceReference);
118:• if (service != null) {
119: serviceHolders.add(new StandardOsgiServiceHolder<T>(service, serviceReference));
120: }
121: }
122: Collections.sort(serviceHolders);
123: }
124: } catch (InvalidSyntaxException ise) {
125: throw new OsgiFrameworkException(ise);
126: }
127:
128: return serviceHolders;
129: }
130:
131: private static class StandardOsgiServiceHolder<T> implements OsgiServiceHolder<T>, Comparable<OsgiServiceHolder<?>> {
132:
133: private final T service;
134:
135: private final ServiceReference<T> serviceReference;
136:
137: private StandardOsgiServiceHolder(T service, ServiceReference<T> serviceReference) {
138: this.service = service;
139: this.serviceReference = serviceReference;
140: }
141:
142: /**
143: * {@inheritDoc}
144: */
145: public T getService() {
146: return this.service;
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: public ServiceReference<T> getServiceReference() {
153: return this.serviceReference;
154: }
155:
156: /**
157: * {@inheritDoc}
158: */
159: public int compareTo(OsgiServiceHolder<?> o) {
160: return this.serviceReference.compareTo(o.getServiceReference());
161: }
162:
163: /**
164: * {@inheritDoc}
165: */
166: @Override
167: public int hashCode() {
168: final int prime = 31;
169: int result = 1;
170: result = prime * result + (this.serviceReference == null ? 0 : this.serviceReference.hashCode());
171: return result;
172: }
173:
174: /**
175: * {@inheritDoc}
176: */
177: @Override
178: public boolean equals(Object obj) {
179: if (this == obj) {
180: return true;
181: }
182: if (obj == null) {
183: return false;
184: }
185: if (getClass() != obj.getClass()) {
186: return false;
187: }
188: StandardOsgiServiceHolder<?> other = (StandardOsgiServiceHolder<?>) obj;
189: if (this.serviceReference == null) {
190: if (other.serviceReference != null) {
191: return false;
192: }
193: } else if (!this.serviceReference.equals(other.serviceReference)) {
194: return false;
195: }
196: return true;
197: }
198:
199: }
200: }