org.java.plugin.extension
Class InstanceFactory

java.lang.Object
  extended by org.java.plugin.extension.InstanceFactory

public class InstanceFactory
extends java.lang.Object

This class creates instances from Extensions. It is based on Guice to allow dependecy injection of Objects and parameters.

Author:
Thasso Griebel (Thasso.Griebel@gmail.com)

Constructor Summary
InstanceFactory(com.google.inject.Injector injector, PluginController controller, ConverterGraph converterGraph)
          Creates a new InstanceFactory where the Injector is used as the main injector to create extensions (instances from extensions that provide a "class" parameter) defined in the given PluginController
 
Method Summary
<T> T
convert(java.lang.Object value, java.lang.reflect.Type source, java.lang.reflect.Type target, java.lang.ClassLoader loader)
          Helper method to access the converter facility and convert from source to target type
 java.lang.Object createFromExtension(Extension extension)
          Use this to craete an instance from the given extension.
<T> T
get(java.lang.Class<? extends T> clazz)
          Creates an instance of the given class using the current injector.
static InstanceFactory getInstance()
          Static access to the instance factory.
 java.lang.ClassLoader getPluginClassLoader(Extension extension)
          Method to resolved plugin's classloader from a given extension
 void initiazlieFromController()
          Get converter extension and add them to the converter graph
 boolean isAutoActivate()
           
 java.lang.Class<?> loadClass(Extension extension)
          Looks for a parameter named class and uses the plugins classloader to load that class.
 void registerTypeConverter(java.lang.reflect.Type targetType, Converter<java.lang.String,?> converter)
          Registers a new Type converter.
 void setAutoActivate(boolean autoActivate)
          Use this to enable/disable automatic plugin activation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InstanceFactory

@Inject
public InstanceFactory(com.google.inject.Injector injector,
                              PluginController controller,
                              ConverterGraph converterGraph)
Creates a new InstanceFactory where the Injector is used as the main injector to create extensions (instances from extensions that provide a "class" parameter) defined in the given PluginController

Parameters:
injector - the main Injector
controller - the PluginManager
See Also:
PluginController, Injector, Extension.createInstance()
Method Detail

initiazlieFromController

public void initiazlieFromController()
Get converter extension and add them to the converter graph


registerTypeConverter

public void registerTypeConverter(java.lang.reflect.Type targetType,
                                  Converter<java.lang.String,?> converter)
Registers a new Type converter. This has to be called befor any opbject creation, otherwise the type converter will not be added to the extension modules. Add you custom converters directly after initializing the PluginManager.

You can also use this to overwrite already registered converter but overriding the Guice default (such as String) does not work.

Parameters:
targetType - the target type
converter - the converter class

get

public <T> T get(java.lang.Class<? extends T> clazz)
Creates an instance of the given class using the current injector.

Type Parameters:
T - the class' type
Parameters:
clazz - to create
Returns:
instance of given class

createFromExtension

public java.lang.Object createFromExtension(Extension extension)
                                     throws JpfException
Use this to craete an instance from the given extension. The extension has to provide a parameter called "class" and the class has to be loadable by the declaring plugins ClassLoader. If autoActivate is enabled, this will make sure that the decalaring plugin of the given Extension is activated before access to its plugin classloader.

Parameters:
extension - the
Returns:
object instance created from extension's class paramter
Throws:
java.lang.ClassNotFoundException - if the extensions class could not be found
JpfException

loadClass

public java.lang.Class<?> loadClass(Extension extension)
                             throws java.lang.ClassNotFoundException
Looks for a parameter named class and uses the plugins classloader to load that class. If autoActivate is enabled, this will make sure that the decalaring plugin of the given Extension is activated before access to its plugin classloader.

Parameters:
extension - the extension
Returns:
class loaded from parameter class
Throws:
java.lang.ClassNotFoundException - if the specified exception class could not be found

getPluginClassLoader

public java.lang.ClassLoader getPluginClassLoader(Extension extension)
Method to resolved plugin's classloader from a given extension

Parameters:
extension - the extension
Returns:
plugin classloader

getInstance

public static InstanceFactory getInstance()
Static access to the instance factory. WARNING Do not use this unless you initialized framework properly. This class depends on Guice DI and its Injector!

Returns:
InstanceFactory

isAutoActivate

public boolean isAutoActivate()
Returns:
true if auto activation is enabled

setAutoActivate

public void setAutoActivate(boolean autoActivate)
Use this to enable/disable automatic plugin activation. If this is true, plugins will be automatically activated before the InstanceFactory acesses an extension or an extension class.

Parameters:
autoActivate - enabled
See Also:
loadClass(org.java.plugin.registry.Extension), createFromExtension(org.java.plugin.registry.Extension)

convert

public <T> T convert(java.lang.Object value,
                     java.lang.reflect.Type source,
                     java.lang.reflect.Type target,
                     java.lang.ClassLoader loader)
Helper method to access the converter facility and convert from source to target type

Type Parameters:
T - target type
Parameters:
value - the value
source - the source type
target - the target type
loader - the loader
Returns:
converted value or null


Copyright © 2010. All Rights Reserved.