public class

BeanFactory

extends Object
implements Observer
java.lang.Object
   ↳ com.microstrategy.web.beans.BeanFactory

Class Overview

The BeanFactory class allows the user to create both MicroStrategy beans and custom beans. It is used by the MicroStrategy Web application to create beans specified in the page configuration files.

Clients create beans using the newBean method, which takes a bean type name and retuns an instance of the corresponding bean class. To map a bean name to a bean class, the factory uses a set of bean mappings and an ordered list of packages. A bean mapping maps a bean name (ex: "ReportBean") to a fully qualified classname. The package list provides a set of packages that will be searched in the event that a given bean name isn't explicitly mapped to a class.

Specifically, the logic for creating a bean based on the type name is as follows:

  • If the bean name is explicitly mapped to a class, instantiate and return an instance of the mapped-to class.
  • Otherwise, search the package list in order. For each package:
    • If there is a package-level factory associated with the package, attempt to create the bean by delegating to the factory (a LocalBeanFactory instance).
    • Otherwise, search the package for a class with a name of the form "<bean name> + Impl". If such a class exists, instantiate and return a new instance.
  • If the bean name was not explictly mapped and a corresponding class could not be found in any of the packages in the list, throw a BeanNotFoundException.

The list of mappings and packages is populated from an XML configuration file, beanFactoryConfig.xml. Users can use the customization infrastructure to augment or modify the set of mappings and packages. A custom beanFactoryConfig.xml might look like this:

 <beanfactory-config>
     <bean-mappings>
         <bean-mapping bean-name="Bean1" bean-class="test.pkg1.Bean1"/>
     </bean-mappings>
     <bean-packages>
         <bean-package package-name="misc.dummypkg2"/>
         <bean-package package-name="misc.dummypkg4" local-factory="DummyPkg4Factory"/>
     </bean-packages>
 </beanfactory-config>
 
The XML above adds one new mapping, from "Bean1" to test.pkg1.Bean1, to those that already exist. It also appends two packages to the list of packages to search. To create beans in the second package, misc.dummypkg4, the BeanFactory will delegate to an instance of that package's factory, misc.dummypkg4.DummyPkg4Factory.

A package-level bean factory class specified via the local-factory attribute must meet the following requirements:
  1. It must implement the LocalBeanFactory interface. (See AbstractLocalBeanFactory for a default implementation of this interface.)
  2. It must contain a method,
    public static <return-type> getInstance();
    that returns an instance of the factory class.
  3. The newBean method implementation must be thread-safe.
See the documentation for more information on customizing beanFactoryConfig.xml.

Note: BeanFactory is a singleton, so any instance-level setters take effect globally.

Summary

Nested Classes
class BeanFactory.BeanFactoryInfo  
class BeanFactory.BeanMappingInfo  
class BeanFactory.BeanMappingInfoList  
class BeanFactory.BeanPackageInfo  
class BeanFactory.BeanPackageInfoList  
Public Methods
String getConfigurationPath()
Get the path for the BeanFactory configuration file.
static BeanFactory getInstance()
Return a BeanFactory instance.
WebComponent newBean(String beanName)
Create a bean based on the bean type name.
void setConfigurationPath(String configPath)
Specify the path for the application's beanFactoryConfig.xml file, relative to the root of the application.
void update(Observable o, Object arg)
Exposed so that this class can update its state when customizations change.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.Observer

Public Methods

public String getConfigurationPath ()

Get the path for the BeanFactory configuration file.

Returns
  • The path to the configuration file, relative to the application root directory (ex: "/WEB-INF/xml/config/beanFactoryConfig.xml").

public static BeanFactory getInstance ()

Return a BeanFactory instance.

Returns
  • BeanFactory A BeanFactory instance.

public WebComponent newBean (String beanName)

Create a bean based on the bean type name.

Parameters
beanName The type name of the desired bean (ex: "ReportBean").
Returns
Throws
IllegalArgumentException If beanName is null or empty.
BeanNotFoundException If beanName could not be resolved to a class.
MSTRUncheckedException If an error occurred in the process of instantiating the bean instance.

public void setConfigurationPath (String configPath)

Specify the path for the application's beanFactoryConfig.xml file, relative to the root of the application. This method should be called when the application is initialized, before any instances of this class are used. It is not threadsafe with respect to other BeanFactory instance methods. The value of this property is by default an empty string, which means that no application-level configuration file will be used. (In this case, the factory will contain no bean mappings and a single package: com.microstrategy.web.beans.)

Parameters
configPath The path to the configuration file, relative to the application root directory (ex: "/WEB-INF/xml/config/beanFactoryConfig.xml").
Throws
IllegalArgumentException If configPath is null.

public void update (Observable o, Object arg)

Exposed so that this class can update its state when customizations change. Clients should not call this method directly. This method is not threadsafe with respect to other BeanFactory methods.