001/*
002 *  jDTAUS Core RI Servlet Container
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.container.ri.servlet;
022
023import java.io.ObjectStreamException;
024import java.util.Locale;
025import org.jdtaus.core.container.ContainerFactory;
026
027/**
028 * Gets thrown when the {@code HttpSession} for a thread of execution is
029 * {@code null}.
030 *
031 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
032 * @version $JDTAUS: SessionLostException.java 8641 2012-09-27 06:45:17Z schulte $
033 */
034public class SessionLostException extends IllegalStateException
035{
036    //--Constants---------------------------------------------------------------
037
038    /** Serial version UID for backwards compatibility with 1.0.x classes. */
039    private static final long serialVersionUID = -7663896854184348690L;
040
041    //---------------------------------------------------------------Constants--
042    //--SessionLostException----------------------------------------------------
043
044    /**
045     * The locale of the exception.
046     * @serial
047     */
048    private Locale locale;
049
050    /***
051     * The thread accessing the session.
052     * @serial
053     */
054    private Thread thread;
055
056    /**
057     * Creates a new instance of {@code SessionLostException} taking
058     * the {@code Thread} trying to access the session.
059     *
060     * @param thread the {@code Thread} accessing the session.
061     * @deprecated Replaced by {@link SessionLostException#SessionLostException(java.util.Locale, java.lang.Thread) }
062     */
063    public SessionLostException( final Thread thread )
064    {
065        super();
066        this.thread = thread;
067        this.locale = Locale.getDefault();
068    }
069
070    /**
071     * Creates a new instance of {@code SessionLostException} taking
072     * the {@code Locale} of the exception and the {@code Thread} trying to
073     * access the session.
074     *
075     * @param locale The locale of the exception.
076     * @param thread the {@code Thread} accessing the session.
077     */
078    public SessionLostException( final Locale locale, final Thread thread )
079    {
080        super();
081        this.locale = locale;
082        this.thread = thread;
083    }
084
085    /**
086     * Gets the thread accessing the session.
087     *
088     * @return the thread accessing the session or {@code null}.
089     */
090    public Thread getThread()
091    {
092        return this.thread;
093    }
094
095    /**
096     * Returns the detail message string of this throwable.
097     *
098     * @return the detail message string of this {@code Throwable} instance or
099     * {@code null}.
100     */
101    public String getMessage()
102    {
103        return this.getSessionLostMessage(
104            this.locale, this.thread == null
105                         ? null : this.thread.getName() );
106
107    }
108
109    //----------------------------------------------------SessionLostException--
110    //--Serializable------------------------------------------------------------
111
112    /**
113     * Takes care of initializing fields when constructed from an 1.0.x object
114     * stream.
115     *
116     * @throws ObjectStreamException if resolution fails.
117     */
118    private Object readResolve() throws ObjectStreamException
119    {
120        if ( this.locale == null )
121        {
122            this.locale = Locale.getDefault();
123        }
124
125        return this;
126    }
127
128    //------------------------------------------------------------Serializable--
129    //--Messages----------------------------------------------------------------
130
131// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
132    // This section is managed by jdtaus-container-mojo.
133
134    /**
135     * Gets the text of message <code>sessionLost</code>.
136     * <blockquote><pre>Keine Sitzung für Thread {0}.</pre></blockquote>
137     * <blockquote><pre>No session available for thread {0}.</pre></blockquote>
138     *
139     * @param locale The locale of the message instance to return.
140     * @param threadName Name of the thread without session.
141     *
142     * @return Information about the thread without session.
143     */
144    private String getSessionLostMessage( final Locale locale,
145            final java.lang.String threadName )
146    {
147        return ContainerFactory.getContainer().
148            getMessage( this, "sessionLost", locale,
149                new Object[]
150                {
151                    threadName
152                });
153
154    }
155
156// </editor-fold>//GEN-END:jdtausMessages
157
158    //----------------------------------------------------------------Messages--
159}