View Javadoc
1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.IllegalPluginStateException;
4   import com.atlassian.plugin.PluginArtifact;
5   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
6   import com.atlassian.plugin.osgi.util.OsgiHeaderUtil;
7   import org.osgi.framework.Bundle;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import java.io.File;
12  
13  import static com.atlassian.plugin.osgi.factory.transform.JarUtils.getManifest;
14  import static com.atlassian.plugin.osgi.factory.transform.JarUtils.hasManifestEntry;
15  import static com.google.common.base.Preconditions.checkNotNull;
16  
17  /**
18   * Helper class that implements the methods for an OSGi plugin that has not been installed.
19   *
20   * @since 2.2.0
21   */
22  final class OsgiPluginUninstalledHelper extends OsgiPluginNotInstalledHelperBase {
23      private static final Logger log = LoggerFactory.getLogger(OsgiPluginUninstalledHelper.class);
24  
25      private final OsgiContainerManager osgiContainerManager;
26      private final PluginArtifact pluginArtifact;
27  
28      public OsgiPluginUninstalledHelper(final String key, final OsgiContainerManager mgr, final PluginArtifact artifact) {
29          super(key);
30          this.pluginArtifact = checkNotNull(artifact);
31          this.osgiContainerManager = checkNotNull(mgr);
32      }
33  
34      public <T> Class<T> loadClass(final String clazz, final Class<?> callingClass) {
35          throw new IllegalPluginStateException(getNotInstalledMessage() + " This is probably because the module " +
36                  "descriptor is trying to load classes in its init() method. Move all classloading into the " +
37                  "enabled() method, and be sure to properly drop class and instance references in disabled().");
38      }
39  
40      public Bundle install() {
41          final File osgiPlugin = pluginArtifact.toFile();
42          log.debug("Installing OSGi plugin '{}'", osgiPlugin);
43          final Bundle bundle = osgiContainerManager.installBundle(osgiPlugin, pluginArtifact.getReferenceMode());
44          final String key = getKey();
45          if (!OsgiHeaderUtil.getPluginKey(bundle).equals(key)) {
46              throw new IllegalArgumentException("The plugin key '" + key + "' must either match the OSGi bundle symbolic " +
47                      "name (Bundle-SymbolicName) or be specified in the Atlassian-Plugin-Key manifest header");
48          }
49          return bundle;
50      }
51  
52      protected String getNotInstalledMessage() {
53          return "This operation requires the plugin '" + getKey() + "' to be installed";
54      }
55  
56      public boolean isRemotePlugin() {
57          return hasManifestEntry(getManifest(pluginArtifact.toFile()), OsgiPlugin.REMOTE_PLUGIN_KEY);
58      }
59  }