1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.util.Enumeration ; 14 import java.util.Vector ; 15 import org.eclipse.osgi.service.resolver.VersionRange; 16 import org.eclipse.osgi.util.ManifestElement; 17 import org.osgi.framework.*; 18 import org.osgi.framework.Constants; 19 20 96 public class BundleNativeCode { 97 100 private Attribute nativepaths; 101 104 private Attribute processor; 105 108 private Attribute osname; 109 112 private Attribute language; 113 116 private Attribute osversion; 117 120 private String filterString; 121 124 private AbstractBundle bundle; 125 126 129 private static AliasMapper aliasMapper = Framework.aliasMapper; 130 131 136 protected BundleNativeCode(ManifestElement element, AbstractBundle bundle) { 137 this.bundle = bundle; 138 String [] nativePaths = element.getValueComponents(); 139 for (int i = 0; i < nativePaths.length; i++) { 140 addPath(nativePaths[i]); 141 } 142 setAttribute(element, Constants.BUNDLE_NATIVECODE_OSNAME); 143 setAttribute(element, Constants.BUNDLE_NATIVECODE_PROCESSOR); 144 setAttribute(element, Constants.BUNDLE_NATIVECODE_OSVERSION); 145 setAttribute(element, Constants.BUNDLE_NATIVECODE_LANGUAGE); 146 setAttribute(element, Constants.SELECTION_FILTER_ATTRIBUTE); 147 } 148 149 private void setAttribute(ManifestElement element, String attribute) { 150 String [] attrValues = element.getAttributes(attribute); 151 if (attrValues != null) { 152 for (int i = 0; i < attrValues.length; i++) { 153 addAttribute(attribute, attrValues[i]); 154 } 155 } 156 } 157 158 163 public String [] getPaths() { 164 if (nativepaths == null) { 165 return null; 166 } 167 String [] paths = new String [nativepaths.size()]; 168 nativepaths.toArray(paths); 169 return (paths); 170 } 171 172 178 protected void addPath(String nativepath) { 179 if (nativepaths == null) { 180 nativepaths = new Attribute(); 181 } 182 nativepaths.addElement(nativepath); 183 } 184 185 194 protected synchronized void addAttribute(String key, String value) { 195 if (key.equals(Constants.BUNDLE_NATIVECODE_PROCESSOR)) { 196 if (processor == null) { 197 processor = new Attribute(); 198 } 199 processor.addElement(aliasMapper.aliasProcessor(value)); 200 return; 201 } 202 if (key.equals(Constants.BUNDLE_NATIVECODE_OSNAME)) { 203 if (osname == null) { 204 osname = new Attribute(); 205 } 206 osname.addElement(aliasMapper.aliasOSName(value)); 207 return; 208 } 209 if (key.equals(Constants.BUNDLE_NATIVECODE_OSVERSION)) { 210 if (osversion == null) { 211 osversion = new Attribute(); 212 } 213 osversion.addElement(new VersionRange(value)); 214 return; 215 } 216 if (key.equals(Constants.SELECTION_FILTER_ATTRIBUTE)) { 217 if (filterString == null) { 218 filterString = value; 219 } 220 return; 221 } 222 if (key.equals(Constants.BUNDLE_NATIVECODE_LANGUAGE)) { 223 if (language == null) { 224 language = new Attribute(); 225 } 226 language.addElement(value.toLowerCase()); 227 return; 228 } 229 } 230 231 236 public String toString() { 237 int size = nativepaths.size(); 238 StringBuffer sb = new StringBuffer (50 * size); 239 for (int i = 0; i < size; i++) { 240 if (i > 0) { 241 sb.append(';'); 242 } 243 sb.append(nativepaths.elementAt(i).toString()); 244 } 245 if (processor != null) { 246 size = processor.size(); 247 for (int i = 0; i < size; i++) { 248 sb.append(';'); 249 sb.append(Constants.BUNDLE_NATIVECODE_PROCESSOR); 250 sb.append('='); 251 sb.append(processor.elementAt(i).toString()); 252 } 253 } 254 if (osname != null) { 255 size = osname.size(); 256 for (int i = 0; i < size; i++) { 257 sb.append(';'); 258 sb.append(Constants.BUNDLE_NATIVECODE_OSNAME); 259 sb.append('='); 260 sb.append(osname.elementAt(i).toString()); 261 } 262 } 263 if (osversion != null) { 264 size = osversion.size(); 265 for (int i = 0; i < size; i++) { 266 sb.append(';'); 267 sb.append(Constants.BUNDLE_NATIVECODE_OSVERSION); 268 sb.append('='); 269 sb.append(osversion.elementAt(i).toString()); 270 } 271 } 272 if (language != null) { 273 size = language.size(); 274 for (int i = 0; i < size; i++) { 275 sb.append(';'); 276 sb.append(Constants.BUNDLE_NATIVECODE_LANGUAGE); 277 sb.append('='); 278 sb.append(language.elementAt(i).toString()); 279 } 280 } 281 return (sb.toString()); 282 } 283 284 294 public int matchProcessorOSNameFilter(String processor, String osname) { 295 if ((this.processor == null) || (this.osname == null)) { 296 return (0); 297 } 298 String otherProcessor = aliasMapper.aliasProcessor(processor); 299 String otherOSName = (String ) aliasMapper.aliasOSName(osname); 300 if (this.processor.equals(otherProcessor) && this.osname.equals(otherOSName) && matchFilter()) { 301 return (1); 302 } 303 return (0); 304 } 305 306 314 public Version matchOSVersion(Version version) { 315 if (this.osversion == null) 316 return Version.emptyVersion; 317 Version result = null; 318 int size = this.osversion.size(); 319 for (int i = 0; i < size; i++) { 320 VersionRange range = (VersionRange) this.osversion.elementAt(i); 322 if (range.isIncluded(version) && (result == null || (range.getMinimum().compareTo(result) > 0))) 323 result = range.getMinimum(); 324 } 325 return result; 326 } 327 328 336 public int matchLanguage(String language) { 337 if (this.language == null) { 338 return (1); 339 } 340 if (this.language.equals(language.toLowerCase())) { 341 return (2); 342 } 343 return (0); 344 } 345 346 public boolean matchFilter() { 347 if (filterString == null) { 348 return true; 349 } 350 FilterImpl filter; 351 try { 352 filter = new FilterImpl(filterString); 353 } catch (InvalidSyntaxException e) { 354 BundleException be = new BundleException(Msg.BUNDLE_NATIVECODE_INVALID_FILTER, e); 355 bundle.framework.publishFrameworkEvent(FrameworkEvent.ERROR, bundle, be); 356 return false; 357 } 358 try { 359 return filter.match(FrameworkProperties.getProperties()); 360 } catch (IllegalArgumentException e) { 361 return filter.matchCase(FrameworkProperties.getProperties()); 362 } 363 } 364 365 368 static class Attribute extends Vector { 369 private static final long serialVersionUID = 3257005440914174512L; 370 371 375 Attribute() { 376 super(10, 10); 377 } 378 379 386 public synchronized boolean equals(Object obj) { 387 for (int i = 0; i < elementCount; i++) { 388 Object data = elementData[i]; 389 if (data instanceof String ) { 390 if (elementData[i].equals(obj)) { 391 return (true); 392 } 393 } else { 394 Enumeration e = ((Vector ) data).elements(); 395 while (e.hasMoreElements()) { 396 if (((String ) e.nextElement()).equals(obj)) { 397 return true; 398 } 399 } 400 } 401 } 402 return (false); 403 } 404 405 411 public synchronized void addElement(Object obj) { 412 if (!contains(obj)) { 413 super.addElement(obj); 414 } 415 } 416 } 417 } 418 | Popular Tags |