Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 17 | package org.apache.felix.framework; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.InputStream; |
| 21 | import java.util.Dictionary; |
| 22 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 23 | import org.apache.felix.framework.ext.FelixBundleContext; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 24 | import org.osgi.framework.*; |
| 25 | |
| 26 | class BundleContextImpl implements FelixBundleContext |
| 27 | { |
| 28 | private Felix m_felix = null; |
| 29 | private BundleImpl m_bundle = null; |
| 30 | |
| 31 | protected BundleContextImpl(Felix felix, BundleImpl bundle) |
| 32 | { |
| 33 | m_felix = felix; |
| 34 | m_bundle = bundle; |
| 35 | } |
| 36 | |
| 37 | public void addImportPackage() throws BundleException |
| 38 | { |
| 39 | throw new BundleException("Not implemented yet."); |
| 40 | } |
| 41 | |
| 42 | public void removeImportPackage() throws BundleException |
| 43 | { |
| 44 | throw new BundleException("Not implemented yet."); |
| 45 | } |
| 46 | |
| 47 | public void addExportPackage() throws BundleException |
| 48 | { |
| 49 | throw new BundleException("Not implemented yet."); |
| 50 | } |
| 51 | |
| 52 | public void removeExportPackage() throws BundleException |
| 53 | { |
| 54 | throw new BundleException("Not implemented yet."); |
| 55 | } |
| 56 | |
| 57 | public String getProperty(String name) |
| 58 | { |
| 59 | return m_felix.getProperty(name); |
| 60 | } |
| 61 | |
| 62 | public Bundle getBundle() |
| 63 | { |
| 64 | return m_bundle; |
| 65 | } |
| 66 | |
| 67 | public Filter createFilter(String expr) |
| 68 | throws InvalidSyntaxException |
| 69 | { |
| 70 | return new FilterImpl(m_felix.getLogger(), expr); |
| 71 | } |
| 72 | |
| 73 | public Bundle installBundle(String location) |
| 74 | throws BundleException |
| 75 | { |
| 76 | return installBundle(location, null); |
| 77 | } |
| 78 | |
| 79 | public Bundle installBundle(String location, InputStream is) |
| 80 | throws BundleException |
| 81 | { |
| 82 | return m_felix.installBundle(location, is); |
| 83 | } |
| 84 | |
| 85 | public Bundle getBundle(long id) |
| 86 | { |
| 87 | return m_felix.getBundle(id); |
| 88 | } |
| 89 | |
| 90 | public Bundle[] getBundles() |
| 91 | { |
| 92 | return m_felix.getBundles(); |
| 93 | } |
| 94 | |
| 95 | public void addBundleListener(BundleListener l) |
| 96 | { |
| 97 | m_felix.addBundleListener(m_bundle, l); |
| 98 | } |
| 99 | |
| 100 | public void removeBundleListener(BundleListener l) |
| 101 | { |
| 102 | m_felix.removeBundleListener(l); |
| 103 | } |
| 104 | |
| 105 | public void addServiceListener(ServiceListener l) |
| 106 | { |
| 107 | try |
| 108 | { |
| 109 | addServiceListener(l, null); |
| 110 | } |
| 111 | catch (InvalidSyntaxException ex) |
| 112 | { |
| 113 | // This will not happen since the filter is null. |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public void addServiceListener(ServiceListener l, String s) |
| 118 | throws InvalidSyntaxException |
| 119 | { |
| 120 | m_felix.addServiceListener(m_bundle, l, s); |
| 121 | } |
| 122 | |
| 123 | public void removeServiceListener(ServiceListener l) |
| 124 | { |
| 125 | m_felix.removeServiceListener(l); |
| 126 | } |
| 127 | |
| 128 | public void addFrameworkListener(FrameworkListener l) |
| 129 | { |
| 130 | m_felix.addFrameworkListener(m_bundle, l); |
| 131 | } |
| 132 | |
| 133 | public void removeFrameworkListener(FrameworkListener l) |
| 134 | { |
| 135 | m_felix.removeFrameworkListener(l); |
| 136 | } |
| 137 | |
| 138 | public ServiceRegistration registerService( |
| 139 | String clazz, Object svcObj, Dictionary dict) |
| 140 | { |
| 141 | return registerService(new String[] { clazz }, svcObj, dict); |
| 142 | } |
| 143 | |
| 144 | public ServiceRegistration registerService( |
| 145 | String[] clazzes, Object svcObj, Dictionary dict) |
| 146 | { |
| 147 | return m_felix.registerService(m_bundle, clazzes, svcObj, dict); |
| 148 | } |
| 149 | |
| 150 | public ServiceReference getServiceReference(String clazz) |
| 151 | { |
| 152 | try |
| 153 | { |
| 154 | ServiceReference[] refs = getServiceReferences(clazz, null); |
| 155 | return getBestServiceReference(refs); |
| 156 | } |
| 157 | catch (InvalidSyntaxException ex) |
| 158 | { |
| 159 | m_felix.getLogger().log(LogWrapper.LOG_ERROR, "BundleContextImpl: " + ex); |
| 160 | } |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | private ServiceReference getBestServiceReference(ServiceReference[] refs) |
| 165 | { |
| 166 | if (refs == null) |
| 167 | { |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | if (refs.length == 1) |
| 172 | { |
| 173 | return refs[0]; |
| 174 | } |
| 175 | |
| 176 | // Loop through all service references and return |
| 177 | // the "best" one according to its rank and ID. |
| 178 | ServiceReference bestRef = null; |
| 179 | Integer bestRank = null; |
| 180 | Long bestId = null; |
| 181 | for (int i = 0; i < refs.length; i++) |
| 182 | { |
| 183 | ServiceReference ref = refs[i]; |
| 184 | |
| 185 | // The first time through the loop just |
| 186 | // assume that the first reference is best. |
| 187 | if (bestRef == null) |
| 188 | { |
| 189 | bestRef = ref; |
| 190 | bestRank = (Integer) bestRef.getProperty("service.ranking"); |
| 191 | // The spec says no ranking defaults to zero. |
| 192 | if (bestRank == null) |
| 193 | { |
| 194 | bestRank = new Integer(0); |
| 195 | } |
| 196 | bestId = (Long) bestRef.getProperty("service.id"); |
| 197 | } |
| 198 | |
| 199 | // Compare current and best references to see if |
| 200 | // the current reference is a better choice. |
| 201 | Integer rank = (Integer) ref.getProperty("service.ranking"); |
| 202 | |
| 203 | // The spec says no ranking defaults to zero. |
| 204 | if (rank == null) |
| 205 | { |
| 206 | rank = new Integer(0); |
| 207 | } |
| 208 | |
| 209 | // If the current reference ranking is greater than the |
| 210 | // best ranking, then keep the current reference. |
| 211 | if (bestRank.compareTo(rank) < 0) |
| 212 | { |
| 213 | bestRef = ref; |
| 214 | bestRank = rank; |
| 215 | bestId = (Long) bestRef.getProperty("service.id"); |
| 216 | } |
| 217 | // If rankings are equal, then compare IDs and |
| 218 | // keep the smallest. |
| 219 | else if (bestRank.compareTo(rank) == 0) |
| 220 | { |
| 221 | Long id = (Long) ref.getProperty("service.id"); |
| 222 | // If either reference has a null ID, then keep |
| 223 | // the one with a non-null ID. |
| 224 | if ((bestId == null) || (id == null)) |
| 225 | { |
| 226 | bestRef = (bestId == null) ? ref : bestRef; |
| 227 | // bestRank = bestRank; // No need to update since they are equal. |
| 228 | bestId = (Long) bestRef.getProperty("service.id"); |
| 229 | } |
| 230 | // Otherwise compare IDs. |
| 231 | else |
| 232 | { |
| 233 | // If the current reference ID is less than the |
| 234 | // best ID, then keep the current reference. |
| 235 | if (bestId.compareTo(id) > 0) |
| 236 | { |
| 237 | bestRef = ref; |
| 238 | // bestRank = bestRank; // No need to update since they are equal. |
| 239 | bestId = (Long) bestRef.getProperty("service.id"); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return bestRef; |
| 246 | } |
| 247 | |
| 248 | public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException |
| 249 | { |
| 250 | // TODO: Implement BundleContext.getAllServiceReferences() |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | public ServiceReference[] getServiceReferences(String clazz, String filter) |
| 255 | throws InvalidSyntaxException |
| 256 | { |
| 257 | return m_felix.getServiceReferences(m_bundle, clazz, filter); |
| 258 | } |
| 259 | |
| 260 | public Object getService(ServiceReference ref) |
| 261 | { |
| 262 | if (ref == null) |
| 263 | { |
| 264 | throw new NullPointerException("Specified service reference cannot be null."); |
| 265 | } |
| 266 | return m_felix.getService(m_bundle, ref); |
| 267 | } |
| 268 | |
| 269 | public boolean ungetService(ServiceReference ref) |
| 270 | { |
| 271 | if (ref == null) |
| 272 | { |
| 273 | throw new NullPointerException("Specified service reference cannot be null."); |
| 274 | } |
| 275 | |
| 276 | // Unget the specified service. |
| 277 | return m_felix.ungetService(m_bundle, ref); |
| 278 | } |
| 279 | |
| 280 | public File getDataFile(String s) |
| 281 | { |
| 282 | return m_felix.getDataFile(m_bundle, s); |
| 283 | } |
| 284 | } |