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      /**
14       * The identifier of the system bundle.
15       *
16       * @since 3.0
17       */
18      public static final int SYSTEM_BUNDLE_ID = 0;
19  
20      /**
21       * Returns the system bundle.
22       *
23       * @param currentBundleContext The bundle context used to get the system bundle.
24       * @return The system bundle.
25       *
26       * @since 3.0
27       */
28      public static Bundle getSystemBundle(BundleContext currentBundleContext)
29      {
30          return currentBundleContext.getBundle(SYSTEM_BUNDLE_ID);
31      }
32  
33      /**
34       * Returns the system bundle context.
35       *
36       * @param currentBundle The current bundle used to get the system bundle.
37       * @return The system bundle context.
38       * @throws IllegalStateException if the current bundle is not in the STARTING, ACTIVE or STOPPING states.
39       *
40       * @since 3.0
41       */
42      public static BundleContext getSystemBundleContext(Bundle currentBundle)
43      {
44          switch (currentBundle.getState())
45          {
46              case Bundle.STARTING:
47              case Bundle.ACTIVE:
48              case Bundle.STOPPING:
49                  return getSystemBundleContext(currentBundle.getBundleContext());
50              default:
51                  throw new IllegalStateException("Cannot get system bundle context when bundle is not in the STARTING, ACTIVE or STOPPING states.");
52          }
53      }
54  
55      /**
56       * Returns the system bundle context.
57       *
58       * @param currentBundleContext The bundle context used to get the system bundle.
59       * @return The system bundle context.
60       *
61       * @since 3.0
62       */
63      public static BundleContext getSystemBundleContext(BundleContext currentBundleContext)
64      {
65          return getSystemBundle(currentBundleContext).getBundleContext();
66      }
67  }