1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.AutowireCapablePlugin;
4   
5   import org.osgi.framework.Bundle;
6   import org.osgi.util.tracker.ServiceTracker;
7   
8   import java.io.InputStream;
9   import java.net.URL;
10  import java.util.Set;
11  
12  /**
13   * Helper for the {@link OsgiPlugin} to abstract how key operations are handled in different states, represented
14   * by implementations of this interface.
15   *
16   * @since 2.2.0
17   */
18  interface OsgiPluginHelper
19  {
20  
21      /**
22       * @return the OSGi bundle
23       */
24      Bundle getBundle();
25  
26      /**
27       * Loads a class from the bundle
28       *
29       * @param clazz The class name to load
30       * @param callingClass The calling class
31       * @param <T> The type of class to load
32       * @return An instance of the class
33       * @throws ClassNotFoundException If the class cannot be found
34       */
35      <T> Class<T> loadClass(String clazz, Class<?> callingClass) throws ClassNotFoundException;
36  
37      /**
38       * Gets a resource from the bundle
39       * @param name The resource name
40       * @return The resource
41       */
42      URL getResource(final String name);
43  
44      /**
45       * Gets a resource as a stream from the bundle
46       * @param name The resource name
47       * @return The input stream
48       */
49      InputStream getResourceAsStream(final String name);
50  
51      /**
52       * Gets the classloader for this bundle
53       * @return The class loader instance
54       */
55      ClassLoader getClassLoader();
56  
57      /**
58       * Installs the bundle
59       * @return The created bundle
60       */
61      Bundle install();
62  
63      /**
64       * Notification the bundle has been enabled
65       *
66       * @param serviceTrackers The service trackers to associate with the bundle
67       */
68      void onEnable(ServiceTracker... serviceTrackers);
69  
70      /**
71       * Notification that the plugin has been disabled
72       */
73      void onDisable();
74  
75      /**
76       * Notification the bundle has been uninstalled
77       */
78      void onUninstall();
79  
80      /**
81       * Creates and autowires the class using the bundle's spring context
82       * @param clazz The class to autowire
83       * @param autowireStrategy The autowire strategy to use
84       * @param <T> The type of class
85       * @return The class instance
86       * @throws IllegalStateException If autowiring is not available
87       */
88      <T> T autowire(final Class<T> clazz, final AutowireCapablePlugin.AutowireStrategy autowireStrategy) throws IllegalStateException;
89  
90      /**
91       * Autowires a class instance
92       * @param instance The instance to autowire
93       * @param autowireStrategy The autowire strategy to use
94       * @throws IllegalStateException If autowiring is not available
95       */
96      void autowire(final Object instance, final AutowireCapablePlugin.AutowireStrategy autowireStrategy) throws IllegalStateException;
97  
98      /**
99       * @return a list of required plugins
100      */
101     Set<String> getRequiredPlugins();
102 
103     /**
104      * @param container the plugin container (spring context)
105      */
106     void setPluginContainer(Object container);
107 }