View Javadoc
1   /*
2    *  jDTAUS Banking RI DTAUS
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.banking.dtaus.ri.zka;
22  
23  import org.jdtaus.core.text.Message;
24  import org.jdtaus.core.text.Messages;
25  
26  /**
27   * Thread-local collections of messages.
28   *
29   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
30   * @version $JDTAUS: ThreadLocalMessages.java 8661 2012-09-27 11:29:58Z schulte $
31   */
32  public abstract class ThreadLocalMessages
33  {
34  
35      /** Maximum number of messages added to the collection. */
36      private static final int MAXIMUM_MESSAGES = 100;
37  
38      /** Thread local collections of messages. */
39      private static final ThreadLocal current = new ThreadLocal()
40      {
41  
42          public Object initialValue()
43          {
44              return new Messages()
45              {
46  
47                  /** Number of messages added to the instance. */
48                  private int messageCount = 0;
49  
50                  public void addMessage( final Message message )
51                  {
52                      if ( this.messageCount + 1L <= Integer.MAX_VALUE && this.messageCount + 1 < MAXIMUM_MESSAGES )
53                      {
54                          this.messageCount++;
55                          super.addMessage( message );
56                      }
57                  }
58  
59                  public void removeMessage( final Message message )
60                  {
61                      if ( this.messageCount - 1 >= 0 )
62                      {
63                          this.messageCount--;
64                          super.removeMessage( message );
65                      }
66                  }
67  
68                  public void clear()
69                  {
70                      this.messageCount = 0;
71                      super.clear();
72                  }
73  
74              };
75          }
76  
77      };
78  
79      /** {@code ThreadLocal} {@code Boolean}. */
80      private static final ThreadLocal errorsEnabled = new ThreadLocal()
81      {
82  
83          protected Object initialValue()
84          {
85              return Boolean.TRUE;
86          }
87  
88      };
89  
90      /**
91       * Flag indicating that {@code CorruptedException}s are enabled.
92       *
93       * @return {@code true} if a {@code CorruptedException} must be thrown whenever a file error is detected;
94       * {@code false} to not throw any exception when detecting a file error.
95       */
96      public static boolean isErrorsEnabled()
97      {
98          Boolean fatal = (Boolean) errorsEnabled.get();
99  
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 }