UserLayoutStoreFactory.java :  » Portal » uPortal-3.1.2 » org » jasig » portal » layout » Java Open Source

Java Open Source » Portal » uPortal 3.1.2 
uPortal 3.1.2 » org » jasig » portal » layout » UserLayoutStoreFactory.java
/**
 * Copyright (c) 2000-2009, Jasig, Inc.
 * See license distributed with this file and available online at
 * https://www.ja-sig.org/svn/jasig-parent/tags/rel-10/license-header.txt
 */
package org.jasig.portal.layout;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.layout.dlm.RDBMDistributedLayoutStore;
import org.jasig.portal.properties.PropertiesManager;

/**
 * Produces and manages a static singleton instance of IUserLayoutStore.
 * 
 * The {@link IUserLayoutStore} implementation that we use is that named by the 
 * portal.properties property "org.jasig.portal.layout.UserLayoutStoreFactory.implementation".
 * In the case where that property is not set or the IUserLayoutStore it names cannot
 * be instantiated, we fall back on the {@link RDBMDistributedLayoutStore} as the default.
 * 
 * Prior to uPortal 2.5, this class existed in the package org.jasig.portal.
 * It has been moved to its present package to express that it is part of the
 * user layout infrastructure.
 * 
 * This class is final because it is not designed to be subclassed.
 * 
 * @author Ken Weiner, kweiner@unicon.net
 * @author Michael Ivanov, mvi@immagic.com
 * @version $Revision: 45149 $ $Date: 2009-02-25 00:46:57 -0600 (Wed, 25 Feb 2009) $ 
 */
public final class UserLayoutStoreFactory {

    /**
     * The name of the portal.properties property the value of which is the name
     * of the IUserLayoutStore implementation we should use.
     */
    public static final String LAYOUT_STORE_IMPL_PROPERTY = 
        "org.jasig.portal.layout.UserLayoutStoreFactory.implementation";
    
    /**
     * Commons Logging log instance.
     */
    private static final Log LOG = 
        LogFactory.getLog(UserLayoutStoreFactory.class);
    
    
    /**
     * Static singleton instance of userLayoutStoreImpl.
     */
    private static IUserLayoutStore userLayoutStoreImpl = null;

    /**
     * The default IUserLayoutStore implementation we will fall back upon 
     * in the case where our property is not set or we cannot instantiate the 
     * IUserLayoutStore named by our property.
     * 
     * This field is default scoped so that our testcase can access it.  It is not intended
     * to be part of the public API of this factory.
     */
    private static final Class <? extends IUserLayoutStore> DEFAULT_LAYOUT_STORE = RDBMDistributedLayoutStore.class;

  /**
   * Returns the singleton IUserLayoutStore instance, which will be that specified in portal.properties, 
   * an instance of the default IUserLayoutStore, or null, in that order.
   * 
   * That is, we return a static singleton IUserLayoutStore instance when possible,
   * prefering to return an instance of the IUserLayoutStore implementation named
   * in our portal.properties property.  If that property is not set or that implementation
   * cannot be successfully instantiated, we will return an instance of the default
   * IUserLayoutStore implementation.  If we cannot instantiate that default IUserLayoutStore
   * we will return null.
   * 
   * This method is synchronized to ensure a consistent return value, resolving a 
   * double checked locking problem.  By synchronizing, we ensure that when we
   * write to the userLayoutStoreImpl static field, that write will be available
   * to other threads when they obtain the lock and enter this method.
   * 
   * @return the configured or default IUserLayoutStore implementation, or null
   */
  public static synchronized IUserLayoutStore getUserLayoutStoreImpl() {

        // if we already have a singleton instance, return it.
        if (userLayoutStoreImpl != null) {
            return userLayoutStoreImpl;
        }

        // if we can instantiate one from portal.properties, let's do it

        // initialize the class name before the try so we can log the broken
        // class name if available.
        String className = "unknown";
        try {
            className = PropertiesManager.getProperty(LAYOUT_STORE_IMPL_PROPERTY);
            userLayoutStoreImpl = (IUserLayoutStore) Class.forName(className).newInstance();
            // note that we stored the static singleton instance
            LOG.info("Instantiated and stored singleton IUserLayoutStore of type " + className);
            return userLayoutStoreImpl;
            
        } catch (Exception e) {
            // if anything went wrong, log the problem and fall back on our
            // default layout store
            LOG.error("Unable to instantiate IUserLayoutStore implementation ["
                    + className + "], attempting to fall back on default of " + DEFAULT_LAYOUT_STORE.getName(), e);

            try {
                userLayoutStoreImpl = (IUserLayoutStore) DEFAULT_LAYOUT_STORE
                        .newInstance();
                
            } catch (Exception e2) {
                LOG.error("Error insantiating default layout store ["
                        + DEFAULT_LAYOUT_STORE + "]", e2);
            }
            // this will either return the default implementation if we succeeded in
            // instantiating it, or null if we did not succeed.
            return userLayoutStoreImpl;
        }
    }
  
}



java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.