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.module.ContainerAccessor;
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.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import java.io.File;
15 import java.io.InputStream;
16 import java.net.URL;
17 import java.util.Set;
18
19 import static com.atlassian.plugin.osgi.factory.transform.JarUtils.getManifest;
20 import static com.atlassian.plugin.osgi.factory.transform.JarUtils.hasManifestEntry;
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23
24
25
26
27
28 final class OsgiPluginUninstalledHelper implements OsgiPluginHelper
29 {
30 private final Logger logger = LoggerFactory.getLogger(this.getClass());
31
32 private final String key;
33 private final OsgiContainerManager osgiContainerManager;
34 private final PluginArtifact pluginArtifact;
35
36 public OsgiPluginUninstalledHelper(String key, final OsgiContainerManager mgr, final PluginArtifact artifact)
37 {
38 this.key = checkNotNull(key);
39 this.pluginArtifact = checkNotNull(artifact);
40 this.osgiContainerManager = checkNotNull(mgr);
41 }
42
43 public Bundle getBundle()
44 {
45 throw new IllegalPluginStateException(getNotInstalledMessage());
46 }
47
48 public <T> Class<T> loadClass(String clazz, Class<?> callingClass) throws ClassNotFoundException
49 {
50 throw new IllegalPluginStateException(getNotInstalledMessage() + " This is probably because the module " +
51 "descriptor is trying to load classes in its init() method. Move all classloading into the " +
52 "enabled() method, and be sure to properly drop class and instance references in disabled().");
53 }
54
55 public URL getResource(String name)
56 {
57 throw new IllegalPluginStateException(getNotInstalledMessage());
58 }
59
60 public InputStream getResourceAsStream(String name)
61 {
62 throw new IllegalPluginStateException(getNotInstalledMessage());
63 }
64
65 public ClassLoader getClassLoader()
66 {
67 throw new IllegalPluginStateException(getNotInstalledMessage());
68 }
69
70 public Bundle install()
71 {
72 final File osgiPlugin = pluginArtifact.toFile();
73 boolean allowReference = PluginArtifact.AllowsReference.Default.allowsReference(pluginArtifact);
74 logger.debug("Installing OSGi plugin '{}'", osgiPlugin);
75 final Bundle bundle = OsgiContainerManager.AllowsReferenceInstall.Default
76 .installBundle(osgiContainerManager, osgiPlugin, allowReference);
77 if (!OsgiHeaderUtil.getPluginKey(bundle).equals(key))
78 {
79 throw new IllegalArgumentException("The plugin key '" + key + "' must either match the OSGi bundle symbolic " +
80 "name (Bundle-SymbolicName) or be specified in the Atlassian-Plugin-Key manifest header");
81 }
82 return bundle;
83 }
84
85 public void onEnable(ServiceTracker... serviceTrackers) throws OsgiContainerException
86 {
87 throw new IllegalPluginStateException(getNotInstalledMessage());
88 }
89
90 public void onDisable() throws OsgiContainerException
91 {
92 throw new IllegalPluginStateException(getNotInstalledMessage());
93 }
94
95 public void onUninstall() throws OsgiContainerException
96 {
97 throw new IllegalPluginStateException(getNotInstalledMessage());
98 }
99
100 public Set<String> getRequiredPlugins()
101 {
102 throw new IllegalPluginStateException(getNotInstalledMessage());
103 }
104
105 public void setPluginContainer(Object container)
106 {
107 throw new IllegalPluginStateException(getNotInstalledMessage());
108 }
109
110 public ContainerAccessor getContainerAccessor()
111 {
112 throw new IllegalPluginStateException(getNotInstalledMessage());
113 }
114
115 public ContainerAccessor getRequiredContainerAccessor()
116 {
117 throw new IllegalPluginStateException(getNotInstalledMessage());
118 }
119
120 private String getNotInstalledMessage()
121 {
122 return "This operation requires the plugin '" + key + "' to be installed";
123 }
124
125 @Override
126 public boolean isRemotePlugin()
127 {
128 return hasManifestEntry(getManifest(pluginArtifact.toFile()), OsgiPlugin.REMOTE_PLUGIN_KEY);
129 }
130 }