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