org.pentaho.platform.plugin.services.pluginmgr
Class PluginResourceLoader

java.lang.Object
  extended by org.pentaho.platform.plugin.services.pluginmgr.PluginResourceLoader
All Implemented Interfaces:
IPluginResourceLoader

public class PluginResourceLoader
extends Object
implements IPluginResourceLoader

The default implementation of the IPluginResourceLoader. Obtains resources by searching the root directory of a PluginClassLoader.

Resource discovery

PluginResourceLoader will search the following places for plugin classes: PluginResourceLoader will search for non-class resources in several locations:

resourcePath

This class requires resource paths to be the relative paths to plugin resources, relative the root directory of the plugin. A resource path can be specified either using '/' or '.' (or both) in the path, depending on the particular method you are using. It is usually best to specify the path using '/' since both the filesystem and the classloader can handle this delimiter, whereas '.' will not be handled correctly if you are trying to load a resource from the filesystem.

Plugin Settings

This class backs the plugin settings APIs with the PentahoSystem settings service. See PentahoSystem.getSystemSetting(String, String) and ISystemSettings. System settings are expected in a file named settings.xml in the root of the plugin directory.

Author:
aphillips

Constructor Summary
PluginResourceLoader()
           
 
Method Summary
 List<URL> findResources(Class<?> clazz, String namePattern)
          A searching method, yielding a list of plugin-related resources as URLs.
 List<URL> findResources(ClassLoader classLoader, String namePattern)
           
 String getPluginSetting(Class<?> pluginClass, String key)
          Searches for the plugin setting with the specified key.
 String getPluginSetting(Class<?> pluginClass, String key, String defaultVal)
          Searches for the plugin setting with the specified key.
 String getPluginSetting(ClassLoader classLoader, String key, String defaultVal)
          Searches for the plugin setting with the specified key.
 byte[] getResourceAsBytes(Class<? extends Object> clazz, String resourcePath)
          Gets a plugin-related resource in the form of an array of bytes.
 InputStream getResourceAsStream(Class<?> clazz, String resourcePath)
          Gets a plugin-related resource in the form of an InputStream.
 InputStream getResourceAsStream(ClassLoader classLoader, String resourcePath)
          Gets a plugin-related resource in the form of an InputStream.
 String getResourceAsString(Class<? extends Object> clazz, String resourcePath)
          Gets a plugin-related resource in the form of a String.
 String getResourceAsString(Class<? extends Object> clazz, String resourcePath, String charsetName)
          Gets a plugin-related resource in the form of a String.
 ResourceBundle getResourceBundle(Class<?> clazz, String resourcePath)
          Retrieves a localized resource bundle for the plugin represented by pluginClass.
 String getSystemRelativePluginPath(ClassLoader classLoader)
           
 void setOverrideClassloader(PluginClassLoader pluginClassloader)
          Deprecated. 
 void setRootDir(File rootDir)
          Deprecated. instead of setting the root dir, have your application use a subclass of PluginResourceLoader that returns an appropriately pathed PluginClassLoader from an overridden getClassLoader(Class).
 void setSettingsPath(String settingsPath)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PluginResourceLoader

public PluginResourceLoader()
Method Detail

setSettingsPath

public void setSettingsPath(String settingsPath)

setOverrideClassloader

@Deprecated
public void setOverrideClassloader(PluginClassLoader pluginClassloader)
Deprecated. 


setRootDir

public void setRootDir(File rootDir)
Deprecated. instead of setting the root dir, have your application use a subclass of PluginResourceLoader that returns an appropriately pathed PluginClassLoader from an overridden getClassLoader(Class).

Force the resource loader to look for resources in this root directory. If null, the resource loader will consult the PluginClassLoader for the root directory.

Parameters:
rootDir - the root directory in which to search for resources

getResourceAsBytes

public byte[] getResourceAsBytes(Class<? extends Object> clazz,
                                 String resourcePath)
Description copied from interface: IPluginResourceLoader
Gets a plugin-related resource in the form of an array of bytes. The relevant plugin is inferred from pluginClass. An example of resource path is "resources/html/my.html". IPluginResourceLoader is able to resolve relative paths as it knows where to look for plugin classes and resources.

Specified by:
getResourceAsBytes in interface IPluginResourceLoader
Parameters:
clazz - a class that is part of the plugin package, used to identify the plugin
resourcePath - the (relative) path to a resource
Returns:
a resource as an array of bytes or null if the resource is not found

getResourceAsString

public String getResourceAsString(Class<? extends Object> clazz,
                                  String resourcePath)
                           throws UnsupportedEncodingException
Description copied from interface: IPluginResourceLoader
Gets a plugin-related resource in the form of a String. The relevant plugin is inferred from pluginClass. An example of resource path is "resources/html/my.html". IPluginResourceLoader is able to resolve relative paths as it knows where to look for plugin classes and resources.

This method defaults the character encoding (how this default is chosen is up to the implementor).

Specified by:
getResourceAsString in interface IPluginResourceLoader
Parameters:
clazz - a class that is part of the plugin package, used to identify the plugin
resourcePath - the (relative) path to a resource
Returns:
a resource as a String or null if the resource is not found
Throws:
UnsupportedEncodingException - if there is a problem encoding the string

getResourceAsString

public String getResourceAsString(Class<? extends Object> clazz,
                                  String resourcePath,
                                  String charsetName)
                           throws UnsupportedEncodingException
Description copied from interface: IPluginResourceLoader
Gets a plugin-related resource in the form of a String. The relevant plugin is inferred from pluginClass. An example of resource path is "resources/html/my.html". IPluginResourceLoader is able to resolve relative paths as it knows where to look for plugin classes and resources.

Specified by:
getResourceAsString in interface IPluginResourceLoader
Parameters:
clazz - a class that is part of the plugin package, used to identify the plugin
resourcePath - the (relative) path to a resource
charsetName - the character set to encode the string
Returns:
a resource as a String or null if the resource is not found
Throws:
UnsupportedEncodingException - if there is a problem encoding the string

getSystemRelativePluginPath

public String getSystemRelativePluginPath(ClassLoader classLoader)

getResourceAsStream

public InputStream getResourceAsStream(Class<?> clazz,
                                       String resourcePath)
Description copied from interface: IPluginResourceLoader
Gets a plugin-related resource in the form of an InputStream. The relevant plugin is inferred from pluginClass. An example of resource path is "resources/html/my.html". IPluginResourceLoader is able to resolve relative paths as it knows where to look for plugin classes and resources.

Specified by:
getResourceAsStream in interface IPluginResourceLoader
Parameters:
clazz - a class that is part of the plugin package, used to identify the plugin
resourcePath - the (relative) path to a resource
Returns:
a resource as an InputStream or null if the resource is not found

getResourceAsStream

public InputStream getResourceAsStream(ClassLoader classLoader,
                                       String resourcePath)
Description copied from interface: IPluginResourceLoader
Gets a plugin-related resource in the form of an InputStream. An example of resource path is "resources/html/my.html". IPluginResourceLoader is able to resolve relative paths as it knows where to look for plugin classes and resources.

Specified by:
getResourceAsStream in interface IPluginResourceLoader
Parameters:
classLoader - the ClassLoader which was used to load a plugin
resourcePath - the (relative) path to a resource
Returns:
a resource as an InputStream or null if the resource is not found

findResources

public List<URL> findResources(Class<?> clazz,
                               String namePattern)
Description copied from interface: IPluginResourceLoader
A searching method, yielding a list of plugin-related resources as URLs. This method allows advanced searching by using the namePattern argument. namePattern supports '?' and '*' characters, representing single and multiple wildcard characters respectively.

Specified by:
findResources in interface IPluginResourceLoader
namePattern - a resource name pattern supporting wildcards
Returns:
a list of URLs to the matching resources or empty list if none are found

findResources

public List<URL> findResources(ClassLoader classLoader,
                               String namePattern)
Specified by:
findResources in interface IPluginResourceLoader
See Also:
IPluginResourceLoader.findResources(Class, String)

getResourceBundle

public ResourceBundle getResourceBundle(Class<?> clazz,
                                        String resourcePath)
Description copied from interface: IPluginResourceLoader
Retrieves a localized resource bundle for the plugin represented by pluginClass. baseName is a fully qualified package name or relative path to a bundle name. For example, a baseName of "resources.messages" might represent: Implementations of IPluginResourceLoader.getResourceBundle(Class, String) should behave similar to ResourceBundle.getBundle(String)

Specified by:
getResourceBundle in interface IPluginResourceLoader
Parameters:
clazz - a class that is part of the plugin package, used to identify the plugin
resourcePath - points to a particular resource bundle
Returns:
a ResourceBundle
See Also:
ResourceBundle

getPluginSetting

public String getPluginSetting(Class<?> pluginClass,
                               String key)
Description copied from interface: IPluginResourceLoader
Searches for the plugin setting with the specified key. The method returns null if the setting is not found.

Specified by:
getPluginSetting in interface IPluginResourceLoader
Parameters:
pluginClass - a class that is part of the plugin package, used to identify the plugin
key - the setting key
Returns:
the plugin setting value or null if setting not found

getPluginSetting

public String getPluginSetting(Class<?> pluginClass,
                               String key,
                               String defaultVal)
Description copied from interface: IPluginResourceLoader
Searches for the plugin setting with the specified key.

Specified by:
getPluginSetting in interface IPluginResourceLoader
Parameters:
pluginClass - a class that is part of the plugin package, used to identify the plugin
key - the setting key
defaultVal - the value to return if no value is found for the setting key
Returns:
the plugin setting value or defaultValue if setting not found

getPluginSetting

public String getPluginSetting(ClassLoader classLoader,
                               String key,
                               String defaultVal)
Description copied from interface: IPluginResourceLoader
Searches for the plugin setting with the specified key.

Specified by:
getPluginSetting in interface IPluginResourceLoader
Parameters:
classLoader - the classloader that was used to load the plugin, must be a PluginClassLoader
key - the setting key
defaultVal - the value to return if no value is found for the setting key
Returns:
the plugin setting value or defaultValue if setting not found