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