view gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModelFactory.java @ 1022:28a0628b11b0

Added license file and license header. gnv/trunk@1258 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:15:08 +0000
parents a88fc6320cf8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.action.sessionmodel;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 * Creates and restores <code>SessionModel</code> objects from <code>
 * HttpServletRequest</code>.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 */
public class SessionModelFactory {

    /**
     * The id of the <code>SessionModel</code> that must be used to lookup the 
     * model from the given <code>HttpSession<Code> of the request.
     */
    public final static String SESSION_MODEL_ID = "de.intevation.gnv.action." +
                                                 "sessionmodel.SessionModel.ID";

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(SessionModelFactory.class);

    /**
     * The singleton Instance of this Factory.
     */
    private static SessionModelFactory instance = null;

    /**
     * Constructor
     */
    private SessionModelFactory() {
        super();
    }

    /**
     * This Method provides an singleton Instance of this Class.
     * @return an singleton Instance of this Class
     */
    public synchronized static SessionModelFactory getInstance() {
        log.debug("SessionModelFactory.getInstance");
        if (instance == null) {
            instance = new SessionModelFactory();
        }
        return instance;
    }

    /**
     * Getting the ArtifactDatabaseClient
     * @param request the tequest from which the SessionModel should be read.
     * @return the ArtifactDatabaseClient
     */
    public SessionModel getSessionModel(HttpServletRequest request) {
        log.debug("SessionModelFactory.getSessionModel");
        synchronized (request) {
            SessionModel sm = null;
            Object obj = request.getSession().getAttribute(SESSION_MODEL_ID);
            if (obj instanceof SessionModel) {
                sm = (SessionModel) obj;
            } else {
                sm = new DefaultSessionModel(request.getLocale());
                request.getSession().setAttribute(SESSION_MODEL_ID, sm);
            }

            return sm;
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org