]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java
c16545889425e4ffc0b7fd2d5dbbd6e41f4bab44
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / BundleUtils.java
1 package org.simantics.scl.osgi.internal;
2
3 import java.io.File;
4 import java.net.URL;
5
6 import org.eclipse.core.runtime.FileLocator;
7 import org.osgi.framework.Bundle;
8
9 public class BundleUtils {
10         
11         /**
12          * Attempt to find the given entry from the given bundle.
13          * 
14          * This method looks for the entry in all bundles that have the appropriate
15          * symbolic name and returns the first result.
16          * 
17          * @param bundle name of the bundle
18          * @param entry name of the entry
19          * @return URL of the entry or null if not found
20          */
21         public static URL getEntryFromBundle(String bundle, String entry) {
22                 for (Bundle b : Activator.getContext().getBundles()) {
23                         if (b.getSymbolicName().equals(bundle)) {
24                                 URL e = b.getEntry(entry);
25                                 if (e != null)
26                                         return e; 
27                         }
28                 }
29                 return null;
30         }
31         
32         /**
33          * Find the file that corresponds to the given URL.
34          * 
35          * Note that this method does not work if the url points inside a bundle 
36          * which is compiled into a jar file.
37          * 
38          * @param url URL of a file (obtained with getEntryFromBundle or otherwise, 
39          *            must actually be a file)
40          * @return File object that corresponds to the given URL
41          */
42         public static File getFileFromURL(URL url) {
43                 try {
44                         return new File(FileLocator.toFileURL(url).toURI());
45                 }
46                 catch (Exception e) {
47                         throw new RuntimeException(e);
48                 }
49         }
50
51 }