Class 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 the className parameter.
      static void setCache​(boolean enable)
      Control whether cache for classes and method names should be enabled or disabled.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • make

        public static <T> T make​(java.lang.String className,
                                 java.lang.String typeName)
                          throws ConfigurationException
        Create an object based on the className parameter.
        Parameters:
        className - The name of the class to construct. Should be a fully qualified name and generally the same as type T
        typeName - A type name used in error messages / exceptions.
        Returns:
        An object of type className, which is cast to type T.
        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 of T (or T 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 specified className throws an Exception 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