Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.LogElement

LineHitsNoteSource
1  /*
2   * $Id: LogElement.java 1537 2009-07-13 14:30:55Z amandel $
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   */
33  package org.jcoderz.commons.logging;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Set;
38  
39  import org.jcoderz.commons.BusinessImpact;
40  import org.jcoderz.commons.Category;
41  import org.jcoderz.commons.Loggable;
42  import org.jcoderz.commons.LoggableImpl;
43  import org.jcoderz.commons.types.Date;
44  
45  
46  
47  /**
48   * This is an implementation of LogItem based upon LogRecords. On instanciation
49   * it creates a nested structure of LogElements according to the structure
50   * of the LogRecord.
51   *
52   */
53 (1)public class LogElement
54        extends LogItem
55  {
56     private static final String TRACEMSG = "TRACEMSG";
57  
58     private Loggable mLoggable;
59     private final java.util.logging.LogRecord mLogRecord;
60     private Throwable mThrown;
61  
62     /**
63      * Creates a root LogElement from a LogRecord
64      *
65      * @param record The log record from which to create this.
66      */
67     LogElement (final java.util.logging.LogRecord record)
68100    {
69100       mThrown = null;
70100       mLogRecord = record;
71100       Loggable loggable = null;
72  
73100       if (! ((record.getParameters() == null)
74              || record.getParameters().length == 0))
75        {
76100          if (record.getParameters()[0] instanceof Loggable)
77            {
78100             loggable = (Loggable) record.getParameters()[0];
79           }
80        }
81100       if (loggable != null)
82        {
83100          init(loggable);
84        }
85        else
86        {
87100          init();
88        }
89100    }
90  
91     /**
92      * Creates a nested LogElement from a LogRecord and nested LogRecord
93      * elements.
94      *
95      * @param record The log record from which to create this.
96      * @param thrown The current nested element of LogRecord.
97      * @param parent The parent element of this.
98      */
99     private LogElement (
100           final java.util.logging.LogRecord record,
101           final Throwable thrown,
102           final LogElement parent)
103100    {
104100       mLogRecord = record;
105100       setParentItem(parent);
106100       init(thrown);
107100    }
108  
109     private void init ()
110     {
111100       mLoggable = null;
112100       initData();
113  
114100       final Throwable thrown = mLogRecord.getThrown();
115100       if (thrown != null)
116        {
117100          setNestedItem(new LogElement(mLogRecord, thrown, this));
118        }
119100    }
120  
121     private void init (Throwable thrown)
122     {
123100       if (thrown instanceof Loggable)
124        {
125100          init((Loggable) thrown);
126        }
127        else
128        {
129100          final Throwable nestedThrown = thrown.getCause();
130100          mLoggable = null;
131100          mThrown = thrown;
132100          initData();
133100          if (nestedThrown != null)
134           {
135100             setNestedItem(new LogElement(mLogRecord, nestedThrown, this));
136           }
137        }
138100    }
139  
140     private void init (Loggable loggable)
141     {
142100       mLoggable = loggable;
143100       mThrown = null;
144100       initData();
145100       if (loggable.getCause() != null)
146        {
147100          setNestedItem(new LogElement(mLogRecord, loggable.getCause(), this));
148        }
149100    }
150  
151     private void initData ()
152     {
153100       initType();
154100       setLoggerLevel(mLogRecord.getLevel());
155100       setSourceClass(mLogRecord.getSourceClassName());
156100       setSourceMethod(mLogRecord.getSourceMethodName());
157100       if (mLoggable != null)
158        {
159100          setBusinessImpact(mLoggable.getLogMessageInfo().getBusinessImpact());
160100          setCategory(mLoggable.getLogMessageInfo().getCategory());
161100          setInstanceId(mLoggable.getInstanceId());
162100          setMessage(mLoggable.getMessage());
163100          setNodeId(mLoggable.getNodeId());
164100          setSolution(mLoggable.getLogMessageInfo().getSolution());
165100          setSymbol(mLoggable.getLogMessageInfo().getSymbol());
166100          setSymbolId(
167                 Integer.toHexString(mLoggable.getLogMessageInfo().toInt()));
168100          setThreadId(mLoggable.getThreadId());
169           try
170           {
171100              setThreadName(mLoggable.getThreadName());
172            }
1730           catch (AbstractMethodError ex)
174            {
175                // We have a old loggable that does not support
176                // thread name jet.
1770               setThreadName(Thread.currentThread().getName());
178100           }
179  
180100          setTimestamp(Date.fromUtilDate(
181                 new java.util.Date(mLoggable.getEventTime())));
182100          setTrackingNumber(mLoggable.getTrackingNumber());
183100          setParameters();
184100          if (mLoggable instanceof Throwable)
185           {
186100             initStackTrace((Throwable) mLoggable);
187           }
188        }
189100       else if (mThrown != null)
190        {
191100          setMessage(mThrown.getMessage());
192100          initStackTrace(mThrown);
193        }
194        else
195        {
196100          setBusinessImpact(BusinessImpact.UNDEFINED);
197100          setCategory(Category.TECHNICAL);
198100          setInstanceId(LoggableImpl.INSTANCE_ID);
199100          setMessage(mLogRecord.getMessage());
200100          setNodeId(LoggableImpl.NODE_ID);
201100          setSymbol(TRACEMSG);
202100          setSymbolId(TRACEMSG);
203100          setThreadId(mLogRecord.getThreadID());
204100          setThreadName(Thread.currentThread().getName());
205100          setTimestamp(Date.fromLong(mLogRecord.getMillis()));
206100          setTrackingNumber(Integer.toHexString(
207                 (int) mLogRecord.getSequenceNumber()));
208        }
209100    }
210  
211     private void setParameters ()
212     {
213100       if (mLoggable != null)
214        {
215100          final Set names = mLoggable.getParameterNames();
216100          if (names != null && ! names.isEmpty())
217           {
218100             for (final Iterator iter = names.iterator(); iter.hasNext(); )
219              {
220100                final String name = (String) iter.next();
221100                if (! name.startsWith(INTERNAL_PARAMETER_PREFIX))
222                 {
223100                   final List parameters = mLoggable.getParameter(name);
224100                   addToParameters(name, parameters);
225                 }
226100             }
227           }
228        }
229100    }
230  
231     private void initStackTrace (final Throwable thrown)
232     {
233        // this is a nop, currently not interested in stack trace
234100    }
235  
236  
237     private void initType ()
238     {
239100       if (mLoggable == null)
240        {
241100          if (getParentItem() == null)
242           {
243100             if (mLogRecord.getThrown() == null)
244              {
245100                setType(String.valueOf(
246                       LogLineFormat.TRACE_MESSAGE.getTypeSpecifier()));
247              }
248              else
249              {
250100                setType(String.valueOf(
251                       LogLineFormat.EXCEPTION_MESSAGE.getTypeSpecifier()));
252              }
253           }
254        }
255        else
256        {
257100          if ((mLoggable.getCause() == null) || (
258                 mLoggable.getCause() instanceof Loggable))
259           {
260100             setType(String.valueOf(
261                    LogLineFormat.LOG_MESSAGE.getTypeSpecifier()));
262           }
263           else
264           {
265100             setType(String.valueOf(
266                    LogLineFormat.ERROR_MESSAGE.getTypeSpecifier()));
267           }
268        }
269100    }
270  }

Findings in this File

c (1) 53 : 0 Type Javadoc comment is missing an @author tag.