/*  Copyright (c) 2001 by Oracle Corporation. All Rights Reserved. */
package oracle.portal.provider.v2.cache.invalidation;

import oracle.portal.log.LogManager;

/**
* Used to generate an <code>InvalidationContext</code> implementation
* object defined in the system property 
* <code>oracle.portal.provider.invalidationclass.</code>
* <p>e.g.
* <code><pre>
* private void someFunction()
* {
*   ServletInvalidationContext inv = ServletInvalidationContextFactory.getServletInvalidationContext();
* }
* </pre></code>
* By using this factory class, implementation details are hidden from the 
* client code.
* <p>
* A default class is used if the system property is not set.
*/
public class ServletInvalidationContextFactory
{
    private static String DEFAULT_CLASSNAME = "oracle.webdb.provider.v2.cache.WebCacheInvalidationContext";

    private static String SYSPROPERTY_CLASSNAME = "oracle.portal.invalidationclass";
    /**
     * Instantiate and return an invalidation context implementation class 
     * Uses default if no system property is set.
     *
     * @return the InvalidationContext type object
     * @exception InvalidationException if an invalidation context can not be
     * instantiated.
     */
    public static ServletInvalidationContext getServletInvalidationContext()
        throws InvalidationException
    {
        String className = System.getProperty(SYSPROPERTY_CLASSNAME);
        LogManager.info("System property " + SYSPROPERTY_CLASSNAME + 
                        ": " + className);
        
        if(className == null)
        {
            className = DEFAULT_CLASSNAME;
        }
        try
        {
            LogManager.info("Instantiating " + className);
            ServletInvalidationContext inv = (ServletInvalidationContext)Class.forName(className).newInstance();
            return inv;
        }
        catch(Exception ex)
        {            
            InvalidationException ie = new InvalidationException
                                        ("Unable to create class " +
                                         className + ": " + ex.getMessage());
            LogManager.throwing(ie.getMessage(), ie);
            throw ie;
        }
    }
}
