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  {
24      private static final Logger log = LoggerFactory.getLogger(OsgiPluginUninstalledHelper.class);
25  
26      private final OsgiContainerManager osgiContainerManager;
27      private final PluginArtifact pluginArtifact;
28  
29      public OsgiPluginUninstalledHelper(final String key, final OsgiContainerManager mgr, final PluginArtifact artifact)
30      {
31          super(key);
32          this.pluginArtifact = checkNotNull(artifact);
33          this.osgiContainerManager = checkNotNull(mgr);
34      }
35  
36      public <T> Class<T> loadClass(final String clazz, final Class<?> callingClass)
37              throws ClassNotFoundException
38      {
39          throw new IllegalPluginStateException(getNotInstalledMessage() + " This is probably because the module " +
40                  "descriptor is trying to load classes in its init() method.  Move all classloading into the " +
41                  "enabled() method, and be sure to properly drop class and instance references in disabled().");
42      }
43  
44      public Bundle install()
45      {
46          final File osgiPlugin = pluginArtifact.toFile();
47          final boolean allowReference = PluginArtifact.AllowsReference.Default.allowsReference(pluginArtifact);
48          log.debug("Installing OSGi plugin '{}'", osgiPlugin);
49          final Bundle bundle = OsgiContainerManager.AllowsReferenceInstall.Default
50                  .installBundle(osgiContainerManager, osgiPlugin, allowReference);
51          final String key = getKey();
52          if (!OsgiHeaderUtil.getPluginKey(bundle).equals(key))
53          {
54              throw new IllegalArgumentException("The plugin key '" + key + "' must either match the OSGi bundle symbolic " +
55                      "name (Bundle-SymbolicName) or be specified in the Atlassian-Plugin-Key manifest header");
56          }
57          return bundle;
58      }
59  
60      protected String getNotInstalledMessage()
61      {
62          return "This operation requires the plugin '" + getKey() + "' to be installed";
63      }
64  
65      public boolean isRemotePlugin()
66      {
67          return hasManifestEntry(getManifest(pluginArtifact.toFile()), OsgiPlugin.REMOTE_PLUGIN_KEY);
68      }
69  }