1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.ModuleDescriptor;
4 import com.atlassian.plugin.PluginException;
5 import com.atlassian.plugin.PluginInformation;
6 import com.atlassian.plugin.PluginState;
7 import com.atlassian.plugin.Resourced;
8 import com.atlassian.plugin.osgi.util.BundleClassLoaderAccessor;
9 import com.atlassian.plugin.elements.ResourceDescriptor;
10 import com.atlassian.plugin.elements.ResourceLocation;
11 import com.atlassian.plugin.event.PluginEventManager;
12 import com.atlassian.plugin.impl.AbstractPlugin;
13 import com.atlassian.plugin.util.resource.AlternativeDirectoryResourceLoader;
14
15 import org.osgi.framework.Bundle;
16 import org.osgi.framework.BundleException;
17 import org.osgi.framework.Constants;
18
19 import java.io.InputStream;
20 import java.net.URL;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.List;
25
26
27
28
29 public class OsgiBundlePlugin extends AbstractPlugin
30 {
31
32 private final Bundle bundle;
33 private final PluginInformation pluginInformation;
34 private final Date dateLoaded;
35 private final String key;
36 private final ClassLoader bundleClassLoader;
37
38 public OsgiBundlePlugin(final Bundle bundle, final String key, final PluginEventManager pluginEventManager)
39 {
40 bundleClassLoader = BundleClassLoaderAccessor.getClassLoader(bundle, new AlternativeDirectoryResourceLoader());
41 this.bundle = bundle;
42 pluginInformation = new PluginInformation();
43 pluginInformation.setDescription((String) bundle.getHeaders().get(Constants.BUNDLE_DESCRIPTION));
44 pluginInformation.setVersion((String) bundle.getHeaders().get(Constants.BUNDLE_VERSION));
45 pluginInformation.setVendorName((String) bundle.getHeaders().get(Constants.BUNDLE_VENDOR));
46
47 this.key = key;
48 dateLoaded = new Date();
49 }
50
51
52 @Override
53 public int getPluginsVersion()
54 {
55 return 2;
56 }
57
58 @Override
59 public void setPluginsVersion(final int version)
60 {
61 throw new UnsupportedOperationException("Not available");
62 }
63
64 @Override
65 public String getName()
66 {
67 return (String) bundle.getHeaders().get(Constants.BUNDLE_NAME);
68 }
69
70 @Override
71 public void setName(final String name)
72 {
73 throw new UnsupportedOperationException("Not available");
74 }
75
76 @Override
77 public void setI18nNameKey(final String i18nNameKey)
78 {
79 throw new UnsupportedOperationException("Not available");
80 }
81
82 @Override
83 public String getKey()
84 {
85 return key;
86 }
87
88 @Override
89 public void setKey(final String aPackage)
90 {
91 throw new UnsupportedOperationException("Not available");
92 }
93
94 @Override
95 public void addModuleDescriptor(final ModuleDescriptor<?> moduleDescriptor)
96 {
97 throw new UnsupportedOperationException("Not available");
98 }
99
100 @Override
101 public Collection<ModuleDescriptor<?>> getModuleDescriptors()
102 {
103 return Collections.emptyList();
104 }
105
106 @Override
107 public ModuleDescriptor<?> getModuleDescriptor(final String key)
108 {
109 return null;
110 }
111
112 @Override
113 public <M> List<ModuleDescriptor<M>> getModuleDescriptorsByModuleClass(final Class<M> aClass)
114 {
115 return Collections.emptyList();
116 }
117
118 @Override
119 public boolean isEnabledByDefault()
120 {
121 return true;
122 }
123
124 @Override
125 public void setEnabledByDefault(final boolean enabledByDefault)
126 {
127 throw new UnsupportedOperationException("Not available");
128 }
129
130 @Override
131 public PluginInformation getPluginInformation()
132 {
133 return pluginInformation;
134 }
135
136 @Override
137 public void setPluginInformation(final PluginInformation pluginInformation)
138 {
139 throw new UnsupportedOperationException("Not available");
140 }
141
142 @Override
143 public void setResources(final Resourced resources)
144 {
145 throw new UnsupportedOperationException("Not available");
146 }
147
148 @Override
149 public boolean isSystemPlugin()
150 {
151 return false;
152 }
153
154 @Override
155 public boolean containsSystemModule()
156 {
157 return false;
158 }
159
160 @Override
161 public void setSystemPlugin(final boolean system)
162 {
163 throw new UnsupportedOperationException("Not available");
164 }
165
166 @Override
167 public Date getDateLoaded()
168 {
169 return dateLoaded;
170 }
171
172 public boolean isUninstallable()
173 {
174 return true;
175 }
176
177 public boolean isDeleteable()
178 {
179 return true;
180 }
181
182 public boolean isDynamicallyLoaded()
183 {
184 return true;
185 }
186
187
188 @Override
189 public List<ResourceDescriptor> getResourceDescriptors()
190 {
191 return Collections.emptyList();
192 }
193
194 @Override
195 public List<ResourceDescriptor> getResourceDescriptors(final String type)
196 {
197 return Collections.emptyList();
198 }
199
200 @Override
201 public ResourceLocation getResourceLocation(final String type, final String name)
202 {
203 return null;
204 }
205
206 @Override
207 public ResourceDescriptor getResourceDescriptor(final String type, final String name)
208 {
209 return null;
210 }
211
212 public <T> Class<T> loadClass(final String clazz, final Class<?> callingClass) throws ClassNotFoundException
213 {
214 return BundleClassLoaderAccessor.loadClass(bundle, clazz);
215 }
216
217 public URL getResource(final String name)
218 {
219 return bundleClassLoader.getResource(name);
220 }
221
222 public InputStream getResourceAsStream(final String name)
223 {
224 return bundleClassLoader.getResourceAsStream(name);
225 }
226
227 @Override
228 protected void uninstallInternal()
229 {
230 try
231 {
232 bundle.uninstall();
233 }
234 catch (final BundleException e)
235 {
236 throw new PluginException(e);
237 }
238 }
239
240 @Override
241 protected PluginState enableInternal()
242 {
243 try
244 {
245 bundle.start();
246 return PluginState.ENABLED;
247 }
248 catch (final BundleException e)
249 {
250 throw new PluginException(e);
251 }
252 }
253
254 @Override
255 protected void disableInternal()
256 {
257 try
258 {
259 if (bundle.getState() == Bundle.ACTIVE)
260 {
261 bundle.stop();
262 }
263 }
264 catch (final BundleException e)
265 {
266 throw new PluginException(e);
267 }
268 }
269
270 public ClassLoader getClassLoader()
271 {
272 return bundleClassLoader;
273 }
274
275 }