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 logger.debug("Installing OSGi plugin '{}'", osgiPlugin);
74 final Bundle bundle = osgiContainerManager.installBundle(osgiPlugin);
75 if (!OsgiHeaderUtil.getPluginKey(bundle).equals(key))
76 {
77 throw new IllegalArgumentException("The plugin key '" + key + "' must either match the OSGi bundle symbolic " +
78 "name (Bundle-SymbolicName) or be specified in the Atlassian-Plugin-Key manifest header");
79 }
80 return bundle;
81 }
82
83 public void onEnable(ServiceTracker... serviceTrackers) throws OsgiContainerException
84 {
85 throw new IllegalPluginStateException(getNotInstalledMessage());
86 }
87
88 public void onDisable() throws OsgiContainerException
89 {
90 throw new IllegalPluginStateException(getNotInstalledMessage());
91 }
92
93 public void onUninstall() throws OsgiContainerException
94 {
95 throw new IllegalPluginStateException(getNotInstalledMessage());
96 }
97
98 public Set<String> getRequiredPlugins()
99 {
100 throw new IllegalPluginStateException(getNotInstalledMessage());
101 }
102
103 public void setPluginContainer(Object container)
104 {
105 throw new IllegalPluginStateException(getNotInstalledMessage());
106 }
107
108 public ContainerAccessor getContainerAccessor()
109 {
110 throw new IllegalPluginStateException(getNotInstalledMessage());
111 }
112
113 public ContainerAccessor getRequiredContainerAccessor()
114 {
115 throw new IllegalPluginStateException(getNotInstalledMessage());
116 }
117
118 private String getNotInstalledMessage()
119 {
120 return "This operation requires the plugin '" + key + "' to be installed";
121 }
122
123 @Override
124 public boolean isRemotePlugin()
125 {
126 return hasManifestEntry(getManifest(pluginArtifact.toFile()), OsgiPlugin.REMOTE_PLUGIN_KEY);
127 }
128 }