1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.ModuleDescriptorFactory;
4 import com.atlassian.plugin.Plugin;
5 import com.atlassian.plugin.PluginArtifact;
6 import com.atlassian.plugin.PluginParseException;
7 import com.atlassian.plugin.impl.UnloadablePlugin;
8 import com.atlassian.plugin.factories.PluginFactory;
9 import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
10 import com.atlassian.plugin.osgi.container.OsgiContainerException;
11 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.commons.lang.Validate;
15 import org.osgi.framework.Constants;
16 import org.osgi.framework.Bundle;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.jar.Manifest;
22
23
24
25
26 public class OsgiBundleFactory implements PluginFactory
27 {
28 private static final Log log = LogFactory.getLog(OsgiBundleFactory.class);
29
30 private final OsgiContainerManager osgi;
31
32 public OsgiBundleFactory(OsgiContainerManager osgi)
33 {
34 Validate.notNull(osgi, "The osgi container is required");
35 this.osgi = osgi;
36 }
37
38 public String canCreate(PluginArtifact pluginArtifact) throws PluginParseException {
39 Validate.notNull(pluginArtifact, "The plugin artifact is required");
40 String pluginKey = null;
41 InputStream manifestStream = pluginArtifact.getResourceAsStream("META-INF/MANIFEST.MF");
42 if (manifestStream != null)
43 {
44 Manifest mf;
45 try {
46 mf = new Manifest(manifestStream);
47 } catch (IOException e) {
48 throw new PluginParseException("Unable to parse manifest", e);
49 }
50 pluginKey = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
51 }
52 return pluginKey;
53 }
54
55 public Plugin create(DeploymentUnit deploymentUnit, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException {
56 Validate.notNull(deploymentUnit, "The plugin deployment unit is required");
57 Validate.notNull(moduleDescriptorFactory, "The module descriptor factory is required");
58
59 Bundle bundle;
60 try
61 {
62 bundle = osgi.installBundle(deploymentUnit.getPath());
63 } catch (OsgiContainerException ex)
64 {
65 return reportUnloadablePlugin(deploymentUnit.getPath(), ex);
66 }
67 return new OsgiBundlePlugin(bundle);
68 }
69
70 private Plugin reportUnloadablePlugin(File file, Exception e)
71 {
72 log.error("Unable to load plugin: "+file, e);
73
74 UnloadablePlugin plugin = new UnloadablePlugin();
75 plugin.setErrorText("Unable to load plugin: "+e.getMessage());
76 return plugin;
77 }
78 }