1 36 package org.ungoverned.oscar; 37 38 import java.io.File ; 39 import java.io.InputStream ; 40 import java.util.Dictionary ; 41 42 import org.osgi.framework.*; 43 44 class BundleContextImpl implements BundleContext 45 { 46 private Oscar m_oscar = null; 47 private BundleImpl m_bundle = null; 48 49 protected BundleContextImpl(Oscar oscar, BundleImpl bundle) 50 { 51 m_oscar = oscar; 52 m_bundle = bundle; 53 } 54 55 public String getProperty(String name) 56 { 57 return m_oscar.getProperty(name); 58 } 59 60 public Bundle getBundle() 61 { 62 return m_bundle; 63 } 64 65 public Filter createFilter(String expr) 66 throws InvalidSyntaxException 67 { 68 return new FilterImpl(expr); 69 } 70 71 public Bundle installBundle(String location) 72 throws BundleException 73 { 74 return installBundle(location, null); 75 } 76 77 public Bundle installBundle(String location, InputStream is) 78 throws BundleException 79 { 80 return m_oscar.installBundle(location, is); 81 } 82 83 public Bundle getBundle(long id) 84 { 85 return m_oscar.getBundle(id); 86 } 87 88 public Bundle[] getBundles() 89 { 90 return m_oscar.getBundles(); 91 } 92 93 public void addBundleListener(BundleListener l) 94 { 95 m_oscar.addBundleListener(m_bundle, l); 96 } 97 98 public void removeBundleListener(BundleListener l) 99 { 100 m_oscar.removeBundleListener(l); 101 } 102 103 public void addServiceListener(ServiceListener l) 104 { 105 try { 106 addServiceListener(l, null); 107 } catch (InvalidSyntaxException ex) { 108 } 110 } 111 112 public void addServiceListener(ServiceListener l, String s) 113 throws InvalidSyntaxException 114 { 115 Oscar.debug("BundleContext.addServiceListener(l, \"" + s + "\")"); 116 m_oscar.addServiceListener(m_bundle, l, s); 117 } 118 119 public void removeServiceListener(ServiceListener l) 120 { 121 m_oscar.removeServiceListener(l); 122 } 123 124 public void addFrameworkListener(FrameworkListener l) 125 { 126 m_oscar.addFrameworkListener(m_bundle, l); 127 } 128 129 public void removeFrameworkListener(FrameworkListener l) 130 { 131 m_oscar.removeFrameworkListener(l); 132 } 133 134 public ServiceRegistration registerService( 135 String clazz, Object svcObj, Dictionary dict) 136 { 137 return registerService(new String [] { clazz }, svcObj, dict); 138 } 139 140 public ServiceRegistration registerService( 141 String [] clazzes, Object svcObj, Dictionary dict) 142 { 143 return m_oscar.registerService(m_bundle, clazzes, svcObj, dict); 144 } 145 146 public ServiceReference getServiceReference(String clazz) 147 { 148 Oscar.debug("BundleContext.getServiceReference()"); 149 try { 150 ServiceReference[] refs = getServiceReferences(clazz, null); 151 return getBestServiceReference(refs); 152 } catch (InvalidSyntaxException ex) { 153 Oscar.error("BundleContextImpl: " + ex); 154 } 155 return null; 156 } 157 158 private ServiceReference getBestServiceReference(ServiceReference[] refs) 159 { 160 if (refs == null) 161 { 162 return null; 163 } 164 165 if (refs.length == 1) 166 { 167 return refs[0]; 168 } 169 170 ServiceReference bestRef = null; 173 Integer bestRank = null; 174 Long bestId = null; 175 for (int i = 0; i < refs.length; i++) 176 { 177 ServiceReference ref = refs[i]; 178 179 if (bestRef == null) 182 { 183 bestRef = ref; 184 bestRank = (Integer ) bestRef.getProperty("service.ranking"); 185 if (bestRank == null) 187 { 188 bestRank = new Integer (0); 189 } 190 bestId = (Long ) bestRef.getProperty("service.id"); 191 } 192 193 Integer rank = (Integer ) ref.getProperty("service.ranking"); 196 197 if (rank == null) 199 { 200 rank = new Integer (0); 201 } 202 203 if (bestRank.compareTo(rank) < 0) 206 { 207 bestRef = ref; 208 bestRank = rank; 209 bestId = (Long ) bestRef.getProperty("service.id"); 210 } 211 else if (bestRank.compareTo(rank) == 0) 214 { 215 Long id = (Long ) ref.getProperty("service.id"); 216 if ((bestId == null) || (id == null)) 219 { 220 bestRef = (bestId == null) ? ref : bestRef; 221 bestId = (Long ) bestRef.getProperty("service.id"); 223 } 224 else 226 { 227 if (bestId.compareTo(id) > 0) 230 { 231 bestRef = ref; 232 bestId = (Long ) bestRef.getProperty("service.id"); 234 } 235 } 236 } 237 } 238 239 return bestRef; 240 } 241 242 public ServiceReference[] getServiceReferences(String clazz, String filter) 243 throws InvalidSyntaxException 244 { 245 Oscar.debug("BundleContext.getServiceReferences()"); 246 return m_oscar.getServiceReferences(clazz, filter); 247 } 248 249 public Object getService(ServiceReference ref) 250 { 251 if (ref == null) 252 { 253 throw new NullPointerException ("Specified service reference cannot be null."); 254 } 255 return m_oscar.getService(m_bundle, ref); 256 } 257 258 public boolean ungetService(ServiceReference ref) 259 { 260 if (ref == null) 261 { 262 throw new NullPointerException ("Specified service reference cannot be null."); 263 } 264 265 Object svcObj = m_oscar.ungetService(m_bundle, ref); 267 268 return (svcObj != null); 270 } 271 272 public File getDataFile(String s) 273 { 274 Oscar.debug("BundleContext.getDataFile()"); 275 return m_oscar.getBundleDataFile(m_bundle, s); 276 } 277 } | Popular Tags |