KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > BundleHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Map JavaDoc;
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 JavaDoc {
40         if (defaultInstance != null)
41             throw new RuntimeException JavaDoc("Can not instantiate bundle helper"); //$NON-NLS-1$
42
this.context = context;
43         defaultInstance = this;
44         bundle = context.getBundle();
45         debug = "true".equalsIgnoreCase(Platform.getDebugOption(IPDEBuildConstants.PI_PDEBUILD + "/debug")); //$NON-NLS-1$ //$NON-NLS-2$
46
}
47
48     public final URL JavaDoc find(IPath path) {
49         return FileLocator.find(bundle, path, null);
50     }
51
52     public final URL JavaDoc find(IPath path, Map JavaDoc 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 JavaDoc {
63         return Platform.getStateLocation(getDefault().bundle);
64     }
65
66     public final InputStream JavaDoc openStream(IPath file) throws IOException JavaDoc {
67         return FileLocator.openStream(bundle, file, false);
68     }
69
70     public final InputStream JavaDoc openStream(IPath file, boolean localized) throws IOException JavaDoc {
71         return FileLocator.openStream(bundle, file, localized);
72     }
73
74     public String JavaDoc toString() {
75         return bundle.getSymbolicName();
76     }
77
78     public Bundle getBundle() {
79         return bundle;
80     }
81
82     public Object JavaDoc acquireService(String JavaDoc 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 JavaDoc filter) {
94         try {
95             return context.createFilter(filter);
96         } catch (InvalidSyntaxException e) {
97             //Ignore, this has been caught when resolving the state.
98
return null;
99         }
100     }
101     
102     public void setLog(Object JavaDoc antLog) {
103         if (antLog == null) {
104             log = null;
105             return;
106         }
107         
108         try {
109             log = new AntLogAdapter(antLog);
110         } catch (NoSuchMethodException JavaDoc e) {
111             log = null;
112         }
113     }
114 }
115
Popular Tags