1 11 package org.eclipse.pde.internal.build; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.Map ; 17 import org.eclipse.core.runtime.*; 18 import org.osgi.framework.*; 19 20 public class BundleHelper { 21 private Bundle bundle; 22 private BundleContext context; 23 private static BundleHelper defaultInstance; 24 private boolean debug = false; 25 private ILog log = null; 26 27 public static BundleHelper getDefault() { 28 return defaultInstance; 29 } 30 31 static void close() { 32 if (defaultInstance != null) { 33 defaultInstance.context = null; 34 defaultInstance.bundle = null; 35 defaultInstance = null; 36 } 37 } 38 39 BundleHelper(BundleContext context) throws RuntimeException { 40 if (defaultInstance != null) 41 throw new RuntimeException ("Can not instantiate bundle helper"); this.context = context; 43 defaultInstance = this; 44 bundle = context.getBundle(); 45 debug = "true".equalsIgnoreCase(Platform.getDebugOption(IPDEBuildConstants.PI_PDEBUILD + "/debug")); } 47 48 public final URL find(IPath path) { 49 return FileLocator.find(bundle, path, null); 50 } 51 52 public final URL find(IPath path, Map override) { 53 return FileLocator.find(bundle, path, override); 54 } 55 56 public final ILog getLog() { 57 if (log == null) 58 return Platform.getLog(bundle); 59 return log; 60 } 61 62 public final IPath getStateLocation() throws IllegalStateException { 63 return Platform.getStateLocation(getDefault().bundle); 64 } 65 66 public final InputStream openStream(IPath file) throws IOException { 67 return FileLocator.openStream(bundle, file, false); 68 } 69 70 public final InputStream openStream(IPath file, boolean localized) throws IOException { 71 return FileLocator.openStream(bundle, file, localized); 72 } 73 74 public String toString() { 75 return bundle.getSymbolicName(); 76 } 77 78 public Bundle getBundle() { 79 return bundle; 80 } 81 82 public Object acquireService(String serviceName) { 83 ServiceReference reference = context.getServiceReference(serviceName); 84 if (reference == null) 85 return null; 86 return context.getService(reference); 87 } 88 89 public boolean isDebugging() { 90 return debug; 91 } 92 93 public Filter createFilter(String filter) { 94 try { 95 return context.createFilter(filter); 96 } catch (InvalidSyntaxException e) { 97 return null; 99 } 100 } 101 102 public void setLog(Object antLog) { 103 if (antLog == null) { 104 log = null; 105 return; 106 } 107 108 try { 109 log = new AntLogAdapter(antLog); 110 } catch (NoSuchMethodException e) { 111 log = null; 112 } 113 } 114 } 115 | Popular Tags |