001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.net.URL;
015: import java.util.Map;
016: import org.eclipse.core.runtime.*;
017: import org.osgi.framework.*;
018:
019: public class BundleHelper {
020: private Bundle bundle;
021: private BundleContext context;
022: private static BundleHelper defaultInstance;
023: private boolean debug = false;
024: private ILog log = null;
025:
026: public static BundleHelper getDefault() {
027: return defaultInstance;
028: }
029:
030: static void close() {
031: if (defaultInstance != null) {
032: defaultInstance.context = null;
033: defaultInstance.bundle = null;
034: defaultInstance = null;
035: }
036: }
037:
038: BundleHelper(BundleContext context) throws RuntimeException {
039: if (defaultInstance != null)
040: throw new RuntimeException(
041: "Can not instantiate bundle helper"); //$NON-NLS-1$
042: this .context = context;
043: defaultInstance = this ;
044: bundle = context.getBundle();
045: debug = "true".equalsIgnoreCase(Platform.getDebugOption(IPDEBuildConstants.PI_PDEBUILD + "/debug")); //$NON-NLS-1$ //$NON-NLS-2$
046: }
047:
048: public final URL find(IPath path) {
049: return FileLocator.find(bundle, path, null);
050: }
051:
052: public final URL find(IPath path, Map override) {
053: return FileLocator.find(bundle, path, override);
054: }
055:
056: public final ILog getLog() {
057: if (log == null)
058: return Platform.getLog(bundle);
059: return log;
060: }
061:
062: public final IPath getStateLocation() throws IllegalStateException {
063: return Platform.getStateLocation(getDefault().bundle);
064: }
065:
066: public final InputStream openStream(IPath file) throws IOException {
067: return FileLocator.openStream(bundle, file, false);
068: }
069:
070: public final InputStream openStream(IPath file, boolean localized)
071: throws IOException {
072: return FileLocator.openStream(bundle, file, localized);
073: }
074:
075: public String toString() {
076: return bundle.getSymbolicName();
077: }
078:
079: public Bundle getBundle() {
080: return bundle;
081: }
082:
083: public Object acquireService(String serviceName) {
084: ServiceReference reference = context
085: .getServiceReference(serviceName);
086: if (reference == null)
087: return null;
088: return context.getService(reference);
089: }
090:
091: public boolean isDebugging() {
092: return debug;
093: }
094:
095: public Filter createFilter(String filter) {
096: try {
097: return context.createFilter(filter);
098: } catch (InvalidSyntaxException e) {
099: //Ignore, this has been caught when resolving the state.
100: return null;
101: }
102: }
103:
104: public void setLog(Object antLog) {
105: if (antLog == null) {
106: log = null;
107: return;
108: }
109:
110: try {
111: log = new AntLogAdapter(antLog);
112: } catch (NoSuchMethodException e) {
113: log = null;
114: }
115: }
116: }
|