View Javadoc

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