com.atlassian.plugin.osgi.factory
Class OsgiPlugin

java.lang.Object
  extended by com.atlassian.plugin.impl.AbstractPlugin
      extended by com.atlassian.plugin.osgi.factory.OsgiPlugin
All Implemented Interfaces:
AutowireCapablePlugin, Plugin, Resourced, java.lang.Comparable<Plugin>

public class OsgiPlugin
extends AbstractPlugin
implements AutowireCapablePlugin

Plugin that wraps an OSGi bundle that does contain a plugin descriptor. The actual bundle is not created until the AbstractPlugin.install() method is invoked. Any attempt to access a method that requires a bundle will throw an IllegalPluginStateException. This class uses a OsgiPluginHelper to represent different behaviors of key methods in different states. OsgiPluginUninstalledHelper implements the methods when the plugin hasn't yet been installed into the OSGi container, while OsgiPluginInstalledHelper implements the methods when the bundle is available. This leaves this class to manage the PluginState and interactions with the event system.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.atlassian.plugin.AutowireCapablePlugin
AutowireCapablePlugin.AutowireStrategy
 
Field Summary
static java.lang.String ATLASSIAN_PLUGIN_KEY
           
static java.lang.String SPRING_CONTEXT
           
 
Fields inherited from interface com.atlassian.plugin.Plugin
NAME_COMPARATOR
 
Constructor Summary
OsgiPlugin(java.lang.String key, OsgiContainerManager mgr, PluginArtifact artifact, PluginEventManager pluginEventManager)
           
 
Method Summary
<T> T
autowire(java.lang.Class<T> clazz)
          Creates and autowires the class, using Spring's autodetection algorithm
<T> T
autowire(java.lang.Class<T> clazz, AutowireCapablePlugin.AutowireStrategy autowireStrategy)
          Creates and autowires the class
 void autowire(java.lang.Object instance)
          Autowires the instance using Spring's autodetection algorithm
 void autowire(java.lang.Object instance, AutowireCapablePlugin.AutowireStrategy autowireStrategy)
          Autowires the instance
protected  void disableInternal()
          Disables the plugin by changing the bundle state back to resolved
protected  PluginState enableInternal()
          Enables the plugin by setting the OSGi bundle state to enabled.
 org.osgi.framework.Bundle getBundle()
           
 java.lang.ClassLoader getClassLoader()
          Get the classloader for the plugin.
 java.util.Set<java.lang.String> getRequiredPlugins()
          Determines which plugins are required for this one to operate based on tracing the "wires" or packages that are imported by this plugin.
 java.net.URL getResource(java.lang.String name)
          Retrieve the URL of the resource from the plugin.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Load a given resource from the plugin.
protected  void installInternal()
          Installs the plugin artifact into OSGi
 boolean isDeleteable()
          Should the plugin file be deleted on unistall?
 boolean isDynamicallyLoaded()
          Whether or not this plugin is loaded dynamically at runtime.
 boolean isUninstallable()
          Whether or not this plugin can be 'uninstalled'.
<T> java.lang.Class<T>
loadClass(java.lang.String clazz, java.lang.Class<?> callingClass)
          Get the plugin to load a specific class.
 void onServiceDependencyWaitEnded(PluginServiceDependencyWaitEndedEvent event)
           
 void onServiceDependencyWaitEnded(PluginServiceDependencyWaitTimedOutEvent event)
           
 void onServiceDependencyWaitStarting(PluginServiceDependencyWaitStartingEvent event)
           
 void onSpringContextFailed(PluginContainerFailedEvent event)
          Called when the spring context for the bundle has failed to be created.
 void onSpringContextRefresh(PluginContainerRefreshedEvent event)
          Called when the spring context for the bundle has been created or refreshed.
 java.lang.String toString()
           
protected  void uninstallInternal()
          Uninstalls the bundle from the OSGi container
 
Methods inherited from class com.atlassian.plugin.impl.AbstractPlugin
addModuleDescriptor, close, compareTo, containsSystemModule, disable, enable, getDateLoaded, getI18nNameKey, getKey, getLog, getModuleDescriptor, getModuleDescriptors, getModuleDescriptorsByModuleClass, getName, getPluginInformation, getPluginState, getPluginsVersion, getResourceDescriptor, getResourceDescriptors, getResourceDescriptors, getResourceLocation, install, isBundledPlugin, isEnabled, isEnabledByDefault, isSystemPlugin, removeModuleDescriptor, setEnabled, setEnabledByDefault, setI18nNameKey, setKey, setName, setPluginInformation, setPluginState, setPluginsVersion, setResources, setSystemPlugin, uninstall
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SPRING_CONTEXT

public static final java.lang.String SPRING_CONTEXT
See Also:
Constant Field Values

ATLASSIAN_PLUGIN_KEY

public static final java.lang.String ATLASSIAN_PLUGIN_KEY
See Also:
Constant Field Values
Constructor Detail

OsgiPlugin

public OsgiPlugin(java.lang.String key,
                  OsgiContainerManager mgr,
                  PluginArtifact artifact,
                  PluginEventManager pluginEventManager)
Method Detail

getBundle

public org.osgi.framework.Bundle getBundle()
                                    throws IllegalPluginStateException
Returns:
The active bundle
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

isUninstallable

public boolean isUninstallable()
Description copied from interface: Plugin
Whether or not this plugin can be 'uninstalled'.

Specified by:
isUninstallable in interface Plugin
Returns:
true

isDynamicallyLoaded

public boolean isDynamicallyLoaded()
Description copied from interface: Plugin
Whether or not this plugin is loaded dynamically at runtime.

Specified by:
isDynamicallyLoaded in interface Plugin
Returns:
true

isDeleteable

public boolean isDeleteable()
Description copied from interface: Plugin
Should the plugin file be deleted on unistall?

Specified by:
isDeleteable in interface Plugin
Returns:
true

loadClass

public <T> java.lang.Class<T> loadClass(java.lang.String clazz,
                                        java.lang.Class<?> callingClass)
                             throws java.lang.ClassNotFoundException,
                                    IllegalPluginStateException
Description copied from interface: Plugin
Get the plugin to load a specific class.

Specified by:
loadClass in interface Plugin
Type Parameters:
T - The class type
Parameters:
clazz - The name of the class to be loaded
callingClass - The class calling the loading (used to help find a classloader)
Returns:
The class instance, loaded from the OSGi bundle
Throws:
java.lang.ClassNotFoundException - If the class cannot be found
IllegalPluginStateException - if the bundle hasn't been created yet

getResource

public java.net.URL getResource(java.lang.String name)
                         throws IllegalPluginStateException
Description copied from interface: Plugin
Retrieve the URL of the resource from the plugin.

Specified by:
getResource in interface Plugin
Parameters:
name - The resource name
Returns:
The resource URL, null if not found
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
                                        throws IllegalPluginStateException
Description copied from interface: Plugin
Load a given resource from the plugin. Plugins that are loaded dynamically will need to implement this in a way that loads the resource from the same context as the plugin. Static plugins can just pull them from their own classloader.

Specified by:
getResourceAsStream in interface Plugin
Parameters:
name - The name of the resource to be loaded.
Returns:
Null if not found
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

getClassLoader

public java.lang.ClassLoader getClassLoader()
                                     throws IllegalPluginStateException
Description copied from interface: Plugin
Get the classloader for the plugin.

Specified by:
getClassLoader in interface Plugin
Returns:
The classloader to load classes and resources from the bundle
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

onSpringContextFailed

public void onSpringContextFailed(PluginContainerFailedEvent event)
                           throws IllegalPluginStateException
Called when the spring context for the bundle has failed to be created. This means the bundle is still active, but the Spring context is not available, so for our purposes, the plugin shouldn't be enabled.

Parameters:
event - The plugin container failed event
Throws:
IllegalPluginStateException - If the plugin key hasn't been set yet

onServiceDependencyWaitStarting

public void onServiceDependencyWaitStarting(PluginServiceDependencyWaitStartingEvent event)

onServiceDependencyWaitEnded

public void onServiceDependencyWaitEnded(PluginServiceDependencyWaitEndedEvent event)

onServiceDependencyWaitEnded

public void onServiceDependencyWaitEnded(PluginServiceDependencyWaitTimedOutEvent event)

onSpringContextRefresh

public void onSpringContextRefresh(PluginContainerRefreshedEvent event)
                            throws IllegalPluginStateException
Called when the spring context for the bundle has been created or refreshed. If this is the first time the context has been refreshed, then it is a new context. Otherwise, this means that the bundle has been reloaded, usually due to a dependency upgrade.

Parameters:
event - The event
Throws:
IllegalPluginStateException - If the plugin key hasn't been set yet

autowire

public <T> T autowire(java.lang.Class<T> clazz)
           throws IllegalPluginStateException
Creates and autowires the class, using Spring's autodetection algorithm

Specified by:
autowire in interface AutowireCapablePlugin
Parameters:
clazz - The class to create
Returns:
The created and wired bean
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

autowire

public <T> T autowire(java.lang.Class<T> clazz,
                      AutowireCapablePlugin.AutowireStrategy autowireStrategy)
           throws IllegalPluginStateException
Creates and autowires the class

Specified by:
autowire in interface AutowireCapablePlugin
Parameters:
clazz - The class to create
autowireStrategy - The autowire strategy
Returns:
The created and wired bean
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

autowire

public void autowire(java.lang.Object instance)
              throws java.lang.IllegalStateException
Autowires the instance using Spring's autodetection algorithm

Specified by:
autowire in interface AutowireCapablePlugin
Parameters:
instance - The object to inject
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet
java.lang.IllegalStateException

autowire

public void autowire(java.lang.Object instance,
                     AutowireCapablePlugin.AutowireStrategy autowireStrategy)
              throws IllegalPluginStateException
Autowires the instance

Specified by:
autowire in interface AutowireCapablePlugin
Parameters:
instance - The object to autowire
autowireStrategy - The autowire strategy, must not be constructor
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

getRequiredPlugins

public java.util.Set<java.lang.String> getRequiredPlugins()
                                                   throws IllegalPluginStateException
Determines which plugins are required for this one to operate based on tracing the "wires" or packages that are imported by this plugin. Bundles that provide those packages are determined to be required plugins.

Specified by:
getRequiredPlugins in interface Plugin
Overrides:
getRequiredPlugins in class AbstractPlugin
Returns:
A set of bundle symbolic names, or plugin keys. Empty set if none.
Throws:
IllegalPluginStateException
Since:
2.2.0

toString

public java.lang.String toString()
Overrides:
toString in class AbstractPlugin

installInternal

protected void installInternal()
                        throws IllegalPluginStateException
Installs the plugin artifact into OSGi

Overrides:
installInternal in class AbstractPlugin
Throws:
IllegalPluginStateException - if the bundle hasn't been created yet

enableInternal

protected PluginState enableInternal()
                              throws OsgiContainerException,
                                     IllegalPluginStateException
Enables the plugin by setting the OSGi bundle state to enabled.

Overrides:
enableInternal in class AbstractPlugin
Returns:
PluginState.ENABLEDif spring isn't necessory or PluginState.ENABLING if we are waiting on a spring context
Throws:
OsgiContainerException - If the underlying OSGi system threw an exception or we tried to enable the bundle when it was in an invalid state
IllegalPluginStateException - if the bundle hasn't been created yet

disableInternal

protected void disableInternal()
                        throws OsgiContainerException,
                               IllegalPluginStateException
Disables the plugin by changing the bundle state back to resolved

Overrides:
disableInternal in class AbstractPlugin
Throws:
OsgiContainerException - If the OSGi system threw an exception
IllegalPluginStateException - if the bundle hasn't been created yet

uninstallInternal

protected void uninstallInternal()
                          throws OsgiContainerException,
                                 IllegalPluginStateException
Uninstalls the bundle from the OSGi container

Overrides:
uninstallInternal in class AbstractPlugin
Throws:
OsgiContainerException - If the underlying OSGi system threw an exception
IllegalPluginStateException - if the bundle hasn't been created yet


Copyright © 2009 Atlassian Pty Ltd. All Rights Reserved.