View Javadoc

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