View Javadoc

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.apache.commons.io.IOUtils;
16  import org.osgi.framework.Constants;
17  import org.osgi.framework.Bundle;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.jar.Manifest;
23  
24  /**
25   * Plugin deployer that deploys OSGi bundles that don't contain XML descriptor files
26   */
27  public class OsgiBundleFactory implements PluginFactory
28  {
29      private static final Log log = LogFactory.getLog(OsgiBundleFactory.class);
30  
31      private final OsgiContainerManager osgi;
32  
33      public OsgiBundleFactory(OsgiContainerManager osgi)
34      {
35          Validate.notNull(osgi, "The osgi container is required");
36          this.osgi = osgi;
37      }
38  
39      public String canCreate(PluginArtifact pluginArtifact) throws PluginParseException {
40          Validate.notNull(pluginArtifact, "The plugin artifact is required");
41          String pluginKey = null;
42          InputStream manifestStream = pluginArtifact.getResourceAsStream("META-INF/MANIFEST.MF");
43  
44          try
45          {
46              if (manifestStream != null)
47              {
48                  Manifest mf;
49                  try {
50                      mf = new Manifest(manifestStream);
51                  } catch (IOException e) {
52                      throw new PluginParseException("Unable to parse manifest", e);
53                  }
54                  pluginKey = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
55              }
56              return pluginKey;
57          }
58          finally
59          {
60              IOUtils.closeQuietly(manifestStream);
61          }
62      }
63  
64      public Plugin create(DeploymentUnit deploymentUnit, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException {
65          Validate.notNull(deploymentUnit, "The plugin deployment unit is required");
66          Validate.notNull(moduleDescriptorFactory, "The module descriptor factory is required");
67          
68          Bundle bundle;
69          try
70          {
71              bundle = osgi.installBundle(deploymentUnit.getPath());
72          } catch (OsgiContainerException ex)
73          {
74              return reportUnloadablePlugin(deploymentUnit.getPath(), ex);
75          }
76          return new OsgiBundlePlugin(bundle);
77      }
78  
79      private Plugin reportUnloadablePlugin(File file, Exception e)
80      {
81          log.error("Unable to load plugin: "+file, e);
82  
83          UnloadablePlugin plugin = new UnloadablePlugin();
84          plugin.setErrorText("Unable to load plugin: "+e.getMessage());
85          return plugin;
86      }
87  }