root/trunk/src/java/org/jcoderz/commons/BaseRuntimeException.java

Revision 1492, 6.3 kB (checked in by amandel, 3 years ago)

Add new way to get Loggable dump with nested parameters.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons;
34
35
36import java.io.Serializable;
37import java.util.List;
38import java.util.Set;
39import java.util.logging.Logger;
40
41
42/**
43 * This is the Base class for all runtime exceptions.
44 *
45 * <p>In the {@link org.jcoderz.commons package overview} you can find a
46 * general statement when to use runtime exceptions.</p>
47 *
48 * <p>This class can never be directly used. Services must implement a
49 * general service specific Exception from which they can derive more
50 * concrete service specific exceptions. There are some common used
51 * exceptions available as direct subclasses of this class. If
52 * appropriate this classes must be used prior generating own
53 * classes.</p>
54 *
55 * <p>Most functionality is implemented and documented by the
56 * {@link org.jcoderz.commons.LoggableImpl} which is used a member of
57 * objects of this class. Other stuff is handled by the base class
58 * {@link java.lang.Exception}.</p>
59 *
60 *
61 * @see org.jcoderz.commons
62 * @author Andreas Mandel
63 */
64public class BaseRuntimeException
65      extends RuntimeException
66      implements Loggable
67{
68   private static final long serialVersionUID = 2L;
69   private static final String CLASSNAME = BaseRuntimeException.class.getName();
70   private static final Logger LOGGER = Logger.getLogger(CLASSNAME);
71
72   /** The loggable implementation. */
73   private final LoggableImpl mLoggable;
74
75   /**
76    * Constructor getting an log message info.
77    *
78    * @param messageInfo the log message info for this exception
79    */
80   protected BaseRuntimeException (LogMessageInfo messageInfo)
81   {
82      super(messageInfo.getSymbol());
83      mLoggable = new LoggableImpl(this, messageInfo);
84   }
85
86   /**
87    * Constructor getting an log message info and a root exception.
88    *
89    * @param messageInfo the log message info for this exception
90    * @param cause the problem that caused this exception to be thrown
91    */
92   protected BaseRuntimeException (LogMessageInfo messageInfo, Throwable cause)
93   {
94      super(messageInfo.getSymbol(), cause);
95      mLoggable = new LoggableImpl(this, messageInfo, cause);
96   }
97
98   /** {@inheritDoc} */
99   public Throwable initCause (Throwable cause)
100   {
101      super.initCause(cause);
102      mLoggable.initCause(cause);
103      return this;
104   }
105
106   /** {@inheritDoc} */
107   public final void addParameter (String name, Serializable value)
108   {
109      mLoggable.addParameter(name, value);
110   }
111
112   /** {@inheritDoc} */
113   public String getInstanceId ()
114   {
115      return mLoggable.getInstanceId();
116   }
117
118   /** {@inheritDoc} */
119   public final String getMessage ()
120   {
121      return mLoggable.getMessage();
122   }
123
124   /** {@inheritDoc} */
125   public final void log ()
126   {
127      mLoggable.log();
128   }
129
130   /** {@inheritDoc} */
131   public Throwable getCause ()
132   {
133      return mLoggable.getCause();
134   }
135
136   /** {@inheritDoc} */
137   public long getEventTime ()
138   {
139      return mLoggable.getEventTime();
140   }
141
142   /** {@inheritDoc} */
143   public LogMessageInfo getLogMessageInfo ()
144   {
145      return mLoggable.getLogMessageInfo();
146   }
147
148   /** {@inheritDoc} */
149   public String getNodeId ()
150   {
151      return mLoggable.getNodeId();
152   }
153
154   /** {@inheritDoc} */
155   public List getParameter (String name)
156   {
157      return mLoggable.getParameter(name);
158   }
159
160   /** {@inheritDoc} */
161   public Set getParameterNames ()
162   {
163      return mLoggable.getParameterNames();
164   }
165
166   /** {@inheritDoc} */
167   public long getThreadId ()
168   {
169      return mLoggable.getThreadId();
170   }
171
172   /** {@inheritDoc} */
173   public String getTrackingNumber ()
174   {
175      return mLoggable.getTrackingNumber();
176   }
177
178   /** {@inheritDoc} */
179   public String getSourceClass ()
180   {
181      return mLoggable.getSourceClass();
182   }
183
184   /** {@inheritDoc} */
185   public String getSourceMethod ()
186   {
187      return mLoggable.getSourceMethod();
188   }
189
190   /** {@inheritDoc} */
191   public String getThreadName ()
192   {
193       return mLoggable.getThreadName();
194   }
195
196   /** {@inheritDoc} */
197   public String toString ()
198   {
199      return mLoggable.toString();
200   }
201
202   /** {@inheritDoc} */
203   public String toDetailedString ()
204   {
205      return mLoggable.toDetailedString();
206   }
207
208   protected final void logCreation ()
209   {
210       if (LOGGER.isLoggable(
211           RteLogMessage.RuntimeExceptionCreated.LOG_LEVEL))
212       {
213           final LogEvent logEvent
214               = new LogEvent(RteLogMessage.RUNTIME_EXCEPTION_CREATED, this);
215           RteLogMessage.ExceptionCreated.addParameters(
216               logEvent, mLoggable.getLogMessageInfo().getSymbol(),
217               mLoggable.getMessage());
218           LOGGER.logp(logEvent.getLogMessageInfo().getLogLevel(),
219               logEvent.getSourceClass(), logEvent.getSourceMethod(),
220               logEvent.getMessage(), logEvent);
221       }
222   }
223
224   LoggableImpl getExceptionImpl ()
225   {
226      return mLoggable;
227   }
228
229}
Note: See TracBrowser for help on using the browser.