Package org.owasp.esapi.util
Class ObjFactory
- java.lang.Object
-
- org.owasp.esapi.util.ObjFactory
-
public class ObjFactory extends java.lang.Object
A generic object factory to create an object of class T. T must be a concrete class that has a no-argument public constructor or a implementor of the Singleton pattern that has a no-arg static getInstance method. If the class being created has a getInstance method, it will be used as a singleton and newInstance() will never be called on the class no matter how many times it comes through this factory.Typical use is something like:
import com.example.interfaces.DrinkingEstablishment; import com.example.interfaces.Beer; ... // Typically these would be populated from some Java properties file String barName = "com.example.foo.Bar"; String beerBrand = "com.example.brewery.Guinness"; ... DrinkingEstablishment bar = ObjFactory.make(barName, "DrinkingEstablishment"); Beer beer = ObjFactory.make(beerBrand, "Beer"); bar.drink(beer); // Drink a Guinness beer at the foo Bar. :) ...
Copyright (c) 2009 - The OWASP Foundation
- Author:
- kevin.w.wall@gmail.com, Chris Schmidt ( chrisisbeef .at. gmail.com )
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
make(java.lang.String className, java.lang.String typeName)
Create an object based on theclassName
parameter.static void
setCache(boolean enable)
Control whether cache for classes and method names should be enabled or disabled.
-
-
-
Method Detail
-
make
public static <T> T make(java.lang.String className, java.lang.String typeName) throws ConfigurationException
Create an object based on theclassName
parameter.- Parameters:
className
- The name of the class to construct. Should be a fully qualified name and generally the same as typeT
typeName
- A type name used in error messages / exceptions.- Returns:
- An object of type
className
, which is cast to typeT
. - Throws:
ConfigurationException
- thrown if class name not found in class path, or does not have a public, no-argument constructor, or is not a concrete class, or if it is not a sub-type ofT
(orT
itself). Usually this is caused by a misconfiguration of the class names specified in the ESAPI.properties file. Also thrown if the CTOR of the specifiedclassName
throws anException
of some type.
-
setCache
public static void setCache(boolean enable)
Control whether cache for classes and method names should be enabled or disabled. Initial state is enabled. Ordinally, you are not expected to want to / have to call this method. It's major purpose is a "just-in-case" something goes wrong is some weird context where multiple ESAPI jars are loaded into a give application and something goes wrong, etc. A secondary purpose is it allows us to easily disable the cache so we can measure its time savings.- Parameters:
enable
- true - enable cache; false - disable cache
-
-