]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java
SCL-compiler should activate installed bundles
[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     public static String resolveBundleState(Bundle bundle) {
52         switch (bundle.getState()) {
53         case Bundle.UNINSTALLED:
54             return "UNINSTALLED";
55         case Bundle.INSTALLED:
56             return "INSTALLED";
57         case Bundle.RESOLVED:
58             return "RESOLVED";
59         case Bundle.STARTING:
60             return "STARTING";
61         case Bundle.STOPPING:
62             return "STOPPING";
63         case Bundle.ACTIVE:
64             return "ACTIVE";
65         default:
66             return "UNKNOWN";
67         }
68     }
69
70 }