blob: 8ae49de3c6669904d684700b2efcfeeac6713ac3 [file] [log] [blame]
Marcel Offermans7f687482010-05-11 09:12:34 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Marcel Offermans8b93efa2010-07-02 18:27:21 +000019package org.apache.felix.dm;
Marcel Offermans7f687482010-05-11 09:12:34 +000020
Marcel Offermans63d9aee2010-08-20 12:21:17 +000021import org.osgi.framework.Bundle;
Marcel Offermans7f687482010-05-11 09:12:34 +000022import org.osgi.framework.Constants;
23import org.osgi.framework.ServiceReference;
24
25/**
26 * OSGi service utilities.
27 */
28public class ServiceUtil {
29 /**
30 * Returns the service ranking of a service, based on its service reference. If
31 * the service has a property specifying its ranking, that will be returned. If
32 * not, the default ranking of zero will be returned.
33 *
34 * @param ref the service reference to determine the ranking for
35 * @return the ranking
36 */
37 public static int getRanking(ServiceReference ref) {
38 Integer rank = (Integer) ref.getProperty(Constants.SERVICE_RANKING);
39 if (rank != null) {
40 return rank.intValue();
41 }
42 return 0;
43 }
44
45 /**
46 * Returns the service ID of a service, based on its service reference. This
47 * method is aware of service aspects as defined by the dependency manager and
48 * will return the ID of the orginal service if you give it an aspect.
49 *
50 * @param ref the service reference to determine the service ID of
51 * @return the service ID
52 */
53 public static long getServiceId(ServiceReference ref) {
54 Long sid = (Long) ref.getProperty(Constants.SERVICE_ID);
55 Long aid = (Long) ref.getProperty(DependencyManager.ASPECT);
56 if (aid != null) {
57 return aid.longValue();
58 }
59 if (sid != null) {
60 return sid.longValue();
61 }
62 throw new IllegalArgumentException("Invalid service reference, no service ID found");
63 }
64
65 /**
66 * Determines if the service is an aspect as defined by the dependency manager.
67 * Aspects are defined by a property and this method will check for its presence.
68 *
69 * @param ref the service reference
70 * @return <code>true</code> if it's an aspect, <code>false</code> otherwise
71 */
72 public static boolean isAspect(ServiceReference ref) {
73 Long aid = (Long) ref.getProperty(DependencyManager.ASPECT);
74 return (aid != null);
75 }
76
77 /**
78 * Converts a service reference to a string, listing both the bundle it was
79 * registered from and all properties.
80 *
81 * @param ref the service reference
82 * @return a string representation of the service
83 */
84 public static String toString(ServiceReference ref) {
85 if (ref == null) {
Marcel Offermans63d9aee2010-08-20 12:21:17 +000086 return "ServiceReference[null]";
Marcel Offermans7f687482010-05-11 09:12:34 +000087 }
Marcel Offermans63d9aee2010-08-20 12:21:17 +000088 else {
89 StringBuffer buf = new StringBuffer();
90 Bundle bundle = ref.getBundle();
91 if (bundle != null) {
92 buf.append("ServiceReference[");
93 buf.append(bundle.getBundleId());
94 buf.append("]{");
Marcel Offermans7f687482010-05-11 09:12:34 +000095 }
Marcel Offermans63d9aee2010-08-20 12:21:17 +000096 else {
97 buf.append("ServiceReference[unregistered]{");
98 }
99 String[] keys = ref.getPropertyKeys();
100 for (int i = 0; i < keys.length; i++) {
101 if (i > 0) {
102 buf.append(',');
103 }
104 buf.append(keys[i]);
105 buf.append('=');
Marcel Offermans4f50b0f2010-10-07 11:21:10 +0000106 Object val = ref.getProperty(keys[i]);
107 if (val instanceof String[]) {
108 String[] valArray = (String[]) val;
109 StringBuffer valBuf = new StringBuffer();
110 valBuf.append('{');
111 for (int j = 0; j < valArray.length; j++) {
112 if (valBuf.length() > 1) {
113 valBuf.append(',');
114 }
115 valBuf.append(valArray[j].toString());
116 }
117 valBuf.append('}');
118 buf.append(valBuf);
119 }
120 else {
121 buf.append(val.toString());
122 }
Marcel Offermans63d9aee2010-08-20 12:21:17 +0000123 }
124 buf.append("}");
125 return buf.toString();
Marcel Offermans7f687482010-05-11 09:12:34 +0000126 }
Marcel Offermans7f687482010-05-11 09:12:34 +0000127 }
128}