root/trunk/src/java/org/jcoderz/commons/BaseException.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 checked exceptions.
44 *
45 * <p>In the {@link org.jcoderz.commons package overview} you can find a
46 * general statement when to use checked exceptions.</p>
47 *
48 * <p>This class can never be directly used. Services must implement a
49 * general service specific Exception (generated initially by the service
50 * framework code generator) from which they can derive more concrete
51 * service specific exceptions. There are some common used exceptions
52 * available as direct subclasses of this class. If appropriate this
53 * classes must be used prior generating own 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 BaseException
65      extends Exception
66      implements Loggable
67{
68   private static final long serialVersionUID = 2L;
69   private static final String CLASSNAME = BaseException.class.getName();
70   private static final Logger LOGGER = Logger.getLogger(CLASSNAME);
71
72
73   /** The loggable implementation. */
74   private final LoggableImpl mLoggable;
75
76   /**
77    * Constructor getting an log message info.
78    *
79    * @param messageInfo the log message info for this exception
80    */
81   protected BaseException (LogMessageInfo messageInfo)
82   {
83      super(messageInfo.getSymbol());
84      mLoggable = new LoggableImpl(this, messageInfo);
85   }
86
87   /**
88    * Constructor getting an log message info and a root exception.
89    *
90    * @param messageInfo the log message info for this exception
91    * @param cause the problem that caused this exception to be thrown
92    */
93   protected BaseException (LogMessageInfo messageInfo, Throwable cause)
94   {
95      super(messageInfo.getSymbol(), cause);
96      mLoggable = new LoggableImpl(this, messageInfo, cause);
97   }
98
99   /** {@inheritDoc} */
100   public Throwable initCause (Throwable cause)
101   {
102      super.initCause(cause);
103      mLoggable.initCause(cause);
104      return this;
105   }
106
107   /** {@inheritDoc} */
108   public final void addParameter (String name, Serializable value)
109   {
110      mLoggable.addParameter(name, value);
111   }
112
113   /** {@inheritDoc} */
114   public String getInstanceId ()
115   {
116      return mLoggable.getInstanceId();
117   }
118
119   /** {@inheritDoc} */
120   public final String getMessage ()
121   {
122      return mLoggable.getMessage();
123   }
124
125   /** {@inheritDoc} */
126   public final void log ()
127   {
128      mLoggable.log();
129   }
130
131   /** {@inheritDoc} */
132   public Throwable getCause ()
133   {
134      return mLoggable.getCause();
135   }
136
137   /** {@inheritDoc} */
138   public long getEventTime ()
139   {
140      return mLoggable.getEventTime();
141   }
142
143   /** {@inheritDoc} */
144   public LogMessageInfo getLogMessageInfo ()
145   {
146      return mLoggable.getLogMessageInfo();
147   }
148
149   /** {@inheritDoc} */
150   public String getNodeId ()
151   {
152      return mLoggable.getNodeId();
153   }
154
155   /** {@inheritDoc} */
156   public List getParameter (String name)
157   {
158      return mLoggable.getParameter(name);
159   }
160
161   /** {@inheritDoc} */
162   public Set getParameterNames ()
163   {
164      return mLoggable.getParameterNames();
165   }
166
167   /** {@inheritDoc} */
168   public long getThreadId ()
169   {
170      return mLoggable.getThreadId();
171   }
172
173   /** {@inheritDoc} */
174   public String getTrackingNumber ()
175   {
176      return mLoggable.getTrackingNumber();
177   }
178
179   /** {@inheritDoc} */
180   public String getSourceClass ()
181   {
182      return mLoggable.getSourceClass();
183   }
184
185   /** {@inheritDoc} */
186   public String getSourceMethod ()
187   {
188      return mLoggable.getSourceMethod();
189   }
190
191   /** {@inheritDoc} */
192   public String getThreadName ()
193   {
194       return mLoggable.getThreadName();
195   }
196
197   /** {@inheritDoc} */
198   public String toString ()
199   {
200      return mLoggable.toString();
201   }
202
203   /** {@inheritDoc} */
204   public String toDetailedString ()
205   {
206      return mLoggable.toDetailedString();
207   }
208
209   protected final void logCreation ()
210   {
211       if (LOGGER.isLoggable(
212           RteLogMessage.ExceptionCreated.LOG_LEVEL))
213       {
214           final LogEvent logEvent
215               = new LogEvent(RteLogMessage.EXCEPTION_CREATED, this);
216           RteLogMessage.ExceptionCreated.addParameters(
217               logEvent, mLoggable.getLogMessageInfo().getSymbol(),
218               mLoggable.getMessage());
219           LOGGER.logp(logEvent.getLogMessageInfo().getLogLevel(),
220               logEvent.getSourceClass(), logEvent.getSourceMethod(),
221               logEvent.getMessage(), logEvent);
222       }
223   }
224
225   LoggableImpl getExceptionImpl ()
226   {
227      return mLoggable;
228   }
229}
Note: See TracBrowser for help on using the browser.