View Javadoc
1   package com.atlassian.plugin.osgi.util;
2   
3   import org.osgi.framework.Bundle;
4   import org.osgi.framework.BundleContext;
5   
6   /**
7    * Utility method for accessing system bundle.
8    *
9    * @since 3.0
10   */
11  public class OsgiSystemBundleUtil {
12      /**
13       * The identifier of the system bundle.
14       *
15       * @since 3.0
16       */
17      public static final int SYSTEM_BUNDLE_ID = 0;
18  
19      /**
20       * Returns the system bundle.
21       *
22       * @param currentBundleContext The bundle context used to get the system bundle.
23       * @return The system bundle.
24       * @since 3.0
25       */
26      public static Bundle getSystemBundle(BundleContext currentBundleContext) {
27          return currentBundleContext.getBundle(SYSTEM_BUNDLE_ID);
28      }
29  
30      /**
31       * Returns the system bundle context.
32       *
33       * @param currentBundle The current bundle used to get the system bundle.
34       * @return The system bundle context.
35       * @throws IllegalStateException if the current bundle is not in the STARTING, ACTIVE or STOPPING states.
36       * @since 3.0
37       */
38      public static BundleContext getSystemBundleContext(Bundle currentBundle) {
39          switch (currentBundle.getState()) {
40              case Bundle.STARTING:
41              case Bundle.ACTIVE:
42              case Bundle.STOPPING:
43                  return getSystemBundleContext(currentBundle.getBundleContext());
44              default:
45                  throw new IllegalStateException("Cannot get system bundle context when bundle is not in the STARTING, ACTIVE or STOPPING states.");
46          }
47      }
48  
49      /**
50       * Returns the system bundle context.
51       *
52       * @param currentBundleContext The bundle context used to get the system bundle.
53       * @return The system bundle context.
54       * @since 3.0
55       */
56      public static BundleContext getSystemBundleContext(BundleContext currentBundleContext) {
57          return getSystemBundle(currentBundleContext).getBundleContext();
58      }
59  }