001/*
002 *  jDTAUS Banking RI DTAUS
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.banking.dtaus.ri.zka;
022
023import org.jdtaus.core.text.Message;
024import org.jdtaus.core.text.Messages;
025
026/**
027 * Thread-local collections of messages.
028 *
029 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
030 * @version $JDTAUS: ThreadLocalMessages.java 8661 2012-09-27 11:29:58Z schulte $
031 */
032public abstract class ThreadLocalMessages
033{
034
035    /** Maximum number of messages added to the collection. */
036    private static final int MAXIMUM_MESSAGES = 100;
037
038    /** Thread local collections of messages. */
039    private static final ThreadLocal current = new ThreadLocal()
040    {
041
042        public Object initialValue()
043        {
044            return new Messages()
045            {
046
047                /** Number of messages added to the instance. */
048                private int messageCount = 0;
049
050                public void addMessage( final Message message )
051                {
052                    if ( this.messageCount + 1L <= Integer.MAX_VALUE && this.messageCount + 1 < MAXIMUM_MESSAGES )
053                    {
054                        this.messageCount++;
055                        super.addMessage( message );
056                    }
057                }
058
059                public void removeMessage( final Message message )
060                {
061                    if ( this.messageCount - 1 >= 0 )
062                    {
063                        this.messageCount--;
064                        super.removeMessage( message );
065                    }
066                }
067
068                public void clear()
069                {
070                    this.messageCount = 0;
071                    super.clear();
072                }
073
074            };
075        }
076
077    };
078
079    /** {@code ThreadLocal} {@code Boolean}. */
080    private static final ThreadLocal errorsEnabled = new ThreadLocal()
081    {
082
083        protected Object initialValue()
084        {
085            return Boolean.TRUE;
086        }
087
088    };
089
090    /**
091     * Flag indicating that {@code CorruptedException}s are enabled.
092     *
093     * @return {@code true} if a {@code CorruptedException} must be thrown whenever a file error is detected;
094     * {@code false} to not throw any exception when detecting a file error.
095     */
096    public static boolean isErrorsEnabled()
097    {
098        Boolean fatal = (Boolean) errorsEnabled.get();
099
100        if ( fatal == null )
101        {
102            throw new IllegalStateException();
103        }
104
105        return fatal.booleanValue();
106    }
107
108    /**
109     * Setter for property {@code errorsEnabled}.
110     *
111     * @param value {@code true} if a {@code CorruptedException} should be thrown whenever a file error is detected;
112     * {@code false} to not throw any exception when detecting a file error.
113     */
114    public static void setErrorsEnabled( final boolean value )
115    {
116        errorsEnabled.set( value ? Boolean.TRUE : Boolean.FALSE );
117    }
118
119    /**
120     * Gets the collection of messages stored with the current thread of execution.
121     *
122     * @return collection of messages stored with the current thread of execution.
123     */
124    public static Messages getMessages()
125    {
126        return (Messages) current.get();
127    }
128
129}