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.PluginArtifact;
6   import com.atlassian.plugin.osgi.container.OsgiContainerException;
7   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
8   import com.atlassian.plugin.osgi.util.OsgiHeaderUtil;
9   import org.osgi.framework.Bundle;
10  import org.osgi.util.tracker.ServiceTracker;
11  import org.apache.commons.lang.Validate;
12  
13  import java.io.InputStream;
14  import java.net.URL;
15  import java.util.Set;
16  
17  /**
18   * Helper class that implements the methods assuming the OSGi plugin has not been installed
19   *
20   * @since 2.2.0
21   */
22  class OsgiPluginUninstalledHelper implements OsgiPluginHelper
23  {
24      private final String key;
25      private final OsgiContainerManager osgiContainerManager;
26      private final PluginArtifact pluginArtifact;
27  
28      public OsgiPluginUninstalledHelper(String key, final OsgiContainerManager mgr, final PluginArtifact artifact)
29      {
30          Validate.notNull(key);
31          Validate.notNull(mgr);
32          Validate.notNull(artifact);
33          this.key = key;
34          this.pluginArtifact = artifact;
35          this.osgiContainerManager = mgr;
36      }
37  
38      public Bundle getBundle()
39      {
40          throw new IllegalPluginStateException(getNotInstalledMessage());
41      }
42  
43      public <T> Class<T> loadClass(String clazz, Class<?> callingClass) throws ClassNotFoundException
44      {
45          throw new IllegalPluginStateException(getNotInstalledMessage() + " This is probably because the module " +
46                  "descriptor is trying to load classes in its init() method.  Move all classloading into the " +
47                  "enabled() method, and be sure to properly drop class and instance references in disabled().");
48      }
49  
50      public URL getResource(String name)
51      {
52          throw new IllegalPluginStateException(getNotInstalledMessage());
53      }
54  
55      public InputStream getResourceAsStream(String name)
56      {
57          throw new IllegalPluginStateException(getNotInstalledMessage());
58      }
59  
60      public ClassLoader getClassLoader()
61      {
62          throw new IllegalPluginStateException(getNotInstalledMessage());
63      }
64  
65      public Bundle install()
66      {
67          Bundle bundle = osgiContainerManager.installBundle(pluginArtifact.toFile());
68          if (!OsgiHeaderUtil.getPluginKey(bundle).equals(key))
69          {
70              throw new IllegalArgumentException("The plugin key '" + key + "' must either match the OSGi bundle symbolic " +
71                      "name (Bundle-SymbolicName) or be specified in the Atlassian-Plugin-Key manifest header");
72          }
73          return bundle;
74      }
75  
76      public void onEnable(ServiceTracker... serviceTrackers) throws OsgiContainerException
77      {
78          throw new IllegalPluginStateException(getNotInstalledMessage());
79      }
80  
81      public void onDisable() throws OsgiContainerException
82      {
83          throw new IllegalPluginStateException(getNotInstalledMessage());
84      }
85  
86      public void onUninstall() throws OsgiContainerException
87      {
88          throw new IllegalPluginStateException(getNotInstalledMessage());
89      }
90  
91      public <T> T autowire(Class<T> clazz, AutowireCapablePlugin.AutowireStrategy autowireStrategy) throws IllegalStateException
92      {
93          throw new IllegalPluginStateException(getNotInstalledMessage());
94      }
95  
96      public void autowire(Object instance, AutowireCapablePlugin.AutowireStrategy autowireStrategy) throws IllegalStateException
97      {
98          throw new IllegalPluginStateException(getNotInstalledMessage());
99      }
100 
101     public Set<String> getRequiredPlugins()
102     {
103         throw new IllegalPluginStateException(getNotInstalledMessage());
104     }
105 
106     public void setPluginContainer(Object container)
107     {
108         throw new IllegalPluginStateException(getNotInstalledMessage());
109     }
110 
111     private String getNotInstalledMessage()
112     {
113         return "This operation requires the plugin '" + key + "' to be installed";
114     }
115 }