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 a plugin container is required, it looks for the container, 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 a plugin container
87 * is available
88 * @param <T> The class type
89 * @return The autowired instance
90 * @throws IllegalPluginStateException If a plugin container is required but not available
91 */
92 <T> T autowire(final Class<T> clazz, final AutowireCapablePlugin.AutowireStrategy autowireStrategy) throws IllegalPluginStateException;
93
94 /**
95 * Autowires a class instance
96 * @param instance The instance to autowire
97 * @param autowireStrategy The autowire strategy to use
98 * @throws IllegalStateException If autowiring is not available
99 */
100 void autowire(final Object instance, final AutowireCapablePlugin.AutowireStrategy autowireStrategy) throws IllegalStateException;
101
102 /**
103 * @return a list of required plugins
104 */
105 Set<String> getRequiredPlugins();
106
107 /**
108 * @param container the plugin container (likely a spring context)
109 */
110 void setPluginContainer(Object container);
111
112 ContainerAccessor getContainerAccessor();
113 }