View Javadoc

1   package com.atlassian.plugin.main;
2   
3   import com.atlassian.plugin.DefaultPluginManager;
4   import com.atlassian.plugin.PluginAccessor;
5   import com.atlassian.plugin.PluginController;
6   import com.atlassian.plugin.PluginParseException;
7   import com.atlassian.plugin.event.PluginEventManager;
8   import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
9   import com.atlassian.plugin.loaders.BundledPluginLoader;
10  import com.atlassian.plugin.loaders.DirectoryPluginLoader;
11  import com.atlassian.plugin.loaders.PluginLoader;
12  import com.atlassian.plugin.osgi.container.OsgiContainerManager;
13  import com.atlassian.plugin.osgi.container.felix.FelixOsgiContainerManager;
14  import com.atlassian.plugin.osgi.factory.OsgiBundleFactory;
15  import com.atlassian.plugin.osgi.factory.OsgiPluginFactory;
16  import com.atlassian.plugin.repositories.FilePluginInstaller;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.log4j.Logger;
25  
26  /**
27   * Facade interface to the Atlassian Plugins framework.  See the package Javadocs for usage information.
28   */
29  public class AtlassianPlugins
30  {
31      private OsgiContainerManager osgiContainerManager;
32      private PluginEventManager pluginEventManager;
33      private DefaultPluginManager pluginManager;
34      private PluginsConfiguration pluginsConfiguration;
35      private HotDeployer hotDeployer;
36  
37      private static final Logger log = Logger.getLogger(AtlassianPlugins.class);
38  
39      /** Suffix for temporary directories which will be removed on shutdown */
40      public static final String TEMP_DIRECTORY_SUFFIX = ".tmp";
41  
42      /**
43       * Constructs an instance of the plugin framework with the specified config.  No additional validation is performed
44       * on the configuration, so it is recommended you use the {@link PluginsConfigurationBuilder} class to create
45       * a configuration instance.
46       *
47       * @param config The plugins configuration to use
48       */
49      public AtlassianPlugins(PluginsConfiguration config)
50      {
51          List<PluginLoader> pluginLoaders = new ArrayList<PluginLoader>();
52          pluginEventManager = new DefaultPluginEventManager();
53  
54          osgiContainerManager = new FelixOsgiContainerManager(
55                  config.getFrameworkBundlesDirectory(),
56                  config.getPackageScannerConfiguration(),
57                  config.getHostComponentProvider(),
58                  pluginEventManager,
59                  config.getBundleCacheDirectory());
60  
61          OsgiPluginFactory osgiPluginDeployer = new OsgiPluginFactory(
62                  config.getPluginDescriptorFilename(),
63                  osgiContainerManager);
64          OsgiBundleFactory osgiBundleDeployer = new OsgiBundleFactory(osgiContainerManager);
65  
66          pluginLoaders.add(new DirectoryPluginLoader(
67                  config.getPluginDirectory(),
68                  Arrays.asList(osgiPluginDeployer, osgiBundleDeployer),
69                  pluginEventManager));
70  
71          if (config.getBundledPluginUrl() != null)
72          {
73              pluginLoaders.add(new BundledPluginLoader(
74                      config.getBundledPluginUrl(),
75                      config.getBundledPluginCacheDirectory(),
76                      Arrays.asList(osgiPluginDeployer, osgiBundleDeployer),
77                      pluginEventManager));
78          }
79  
80  
81          pluginManager = new DefaultPluginManager(
82                  config.getPluginStateStore(),
83                  pluginLoaders,
84                  config.getModuleDescriptorFactory(),
85                  pluginEventManager);
86          pluginManager.setPluginInstaller(new FilePluginInstaller(
87                  config.getPluginDirectory()));
88  
89          if (config.getHotDeployPollingPeriod() > 0)
90          {
91              hotDeployer = new HotDeployer(pluginManager, config.getHotDeployPollingPeriod());
92          }
93          this.pluginsConfiguration = config;
94  
95  
96      }
97  
98      /**
99       * Starts the plugins framework.  Will return once the plugins have all been loaded and started.  Should only be
100      * called once.
101      *
102      * @throws PluginParseException If there was any problems parsing any of the plugins
103      */
104     public void start() throws PluginParseException
105     {
106         pluginManager.init();
107         if (hotDeployer != null && !hotDeployer.isRunning())
108         {
109             hotDeployer.start();
110         }
111     }
112 
113     /**
114      * Stops the framework.
115      */
116     public void stop()
117     {
118         if (hotDeployer != null && hotDeployer.isRunning())
119         {
120             hotDeployer.stop();
121         }
122         pluginManager.shutdown();
123         deleteDirIfTmp(pluginsConfiguration.getBundleCacheDirectory());
124         deleteDirIfTmp(pluginsConfiguration.getFrameworkBundlesDirectory());
125     }
126 
127     /**
128      * @return the underlying OSGi container manager
129      */
130     public OsgiContainerManager getOsgiContainerManager()
131     {
132         return osgiContainerManager;
133     }
134 
135     /**
136      * @return the plugin event manager
137      */
138     public PluginEventManager getPluginEventManager()
139     {
140         return pluginEventManager;
141     }
142 
143     /**
144      * @return the plugin controller for manipulating plugins
145      */
146     public PluginController getPluginController()
147     {
148         return pluginManager;
149     }
150 
151     /**
152      * @return the plugin accessor for accessing plugins
153      */
154     public PluginAccessor getPluginAccessor()
155     {
156         return pluginManager;
157     }
158 
159     private static void deleteDirIfTmp(File dir)
160     {
161         if (dir.getName().endsWith(TEMP_DIRECTORY_SUFFIX))
162         {
163             try
164             {
165                 org.apache.commons.io.FileUtils.deleteDirectory(dir);
166             }
167             catch (IOException e)
168             {
169                 log.error("Unable to delete directory: " + dir.getAbsolutePath(), e);
170             }
171         }
172     }
173 }