Coverage Report - net.admin4j.entity.ExceptionInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionInfo
89%
34/38
71%
10/14
1.667
ExceptionInfo$TimeStamp
50%
3/6
N/A
1.667
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.entity;
 15  
 
 16  
 import java.util.Date;
 17  
 
 18  
 import net.admin4j.deps.commons.lang3.Validate;
 19  
 import net.admin4j.deps.commons.lang3.builder.EqualsBuilder;
 20  
 import net.admin4j.deps.commons.lang3.builder.HashCodeBuilder;
 21  
 import net.admin4j.exception.ExceptionTracker;
 22  
 import net.admin4j.util.ExpiringCache;
 23  
 
 24  
 import org.slf4j.Logger;
 25  
 import org.slf4j.LoggerFactory;
 26  
 
 27  
 /**
 28  
  * Represents an Exception and information associated with it.
 29  
  * @author D. Ashmore
 30  
  * @since 1.0
 31  
  */
 32  
 public class ExceptionInfo extends ExceptionInfoBase {
 33  
     
 34  15
     private static Logger logger = LoggerFactory.getLogger(ExceptionTracker.class);
 35  
     
 36  
     /**
 37  
      * @deprecated Only provided so that we can read release 1.0_rc1 created files.
 38  
      * @param exceptionClass
 39  
      * @param trace
 40  
      * @param id
 41  
      */
 42  
     @SuppressWarnings("rawtypes")
 43  39
     public ExceptionInfo(Class exceptionClass, StackTraceElement[] trace, String id) {
 44  
         Validate.notNull(exceptionClass, "Null exceptionClass not allowed.");
 45  39
         if (trace == null) {
 46  0
             this.stackTrace = new StackTraceElement[0];
 47  
         }
 48  39
         else this.stackTrace = trace;
 49  
         
 50  39
         this.setExceptionClassName(exceptionClass.getName());
 51  
         
 52  39
         if (id == null) {
 53  0
             this.alternateId = deriveId();
 54  
         }
 55  39
     }
 56  
         
 57  
         public ExceptionInfo(String exceptionClassName, StackTraceElement[] trace) {
 58  75
         this(exceptionClassName, trace, null);
 59  75
     }
 60  
     
 61  126
     public ExceptionInfo(String exceptionClassName, StackTraceElement[] trace, String id) {
 62  
         Validate.notEmpty(exceptionClassName, "Null or blank exceptionClassName not allowed.");
 63  126
         if (trace == null) {
 64  0
             this.stackTrace = new StackTraceElement[0];
 65  
         }
 66  126
         else this.stackTrace = trace;
 67  
         
 68  126
         this.setExceptionClassName(exceptionClassName);
 69  
         
 70  126
         if (id == null) {
 71  75
             this.alternateId = deriveId();
 72  
         }
 73  126
     }
 74  
         
 75  15
         private static ExpiringCache occuranceTimeStampMap = 
 76  
                 new ExpiringCache(60000,60000);
 77  
         private String alternateId;
 78  
         private String deriveId() {
 79  75
             StringBuffer buffer = new StringBuffer(1024);
 80  75
             buffer.append(this.getExceptionClassName());
 81  75
             buffer.append("|");
 82  
             
 83  1914
             for (StackTraceElement element: this.getStackTrace()) {
 84  1839
                 buffer.append(element);
 85  
             }
 86  
             
 87  75
             return buffer.toString();
 88  
         }
 89  
         
 90  
         public void postOccurance(long systemIdHash) {
 91  36
             TimeStamp time = (TimeStamp)occuranceTimeStampMap.get(systemIdHash);            
 92  36
                 if (time == null) {
 93  36
                     this.setLastOccuranceDt(new Date());
 94  36
                 this.totalNbrExceptions.addAndGet(1);
 95  36
                         occuranceTimeStampMap.put(systemIdHash, new TimeStamp(System.currentTimeMillis()));
 96  
                 }
 97  0
                 else logger.debug("Duplicate posting detected -- sys id hash={}", systemIdHash);
 98  36
         }
 99  
         
 100  
         /**
 101  
          * This complication is needed so exceptions thrown at the same timestamp don't get collapsed when put into a Set.
 102  
          * @author D. Ashmore
 103  
          *
 104  
          */
 105  
         public static class TimeStamp {
 106  36
             public TimeStamp(long time) {
 107  36
                 timestampInMillis = time;
 108  36
             }
 109  
             public Long timestampInMillis;
 110  
         public Long getTimestampInMillis() {
 111  0
             return timestampInMillis;
 112  
         }
 113  
         public void setTimestampInMillis(Long timestampInMillis) {
 114  0
             this.timestampInMillis = timestampInMillis;
 115  0
         }
 116  
         }
 117  
 
 118  
     /* (non-Javadoc)
 119  
      * @see net.admin4j.exception.ExceptionInfoBase#equals(java.lang.Object)
 120  
      */
 121  
     @Override
 122  
     public boolean equals(Object obj) {
 123  117
         if (obj == null) {
 124  3
             return false;
 125  
         }
 126  
         return new EqualsBuilder()
 127  
             .appendSuper(super.equals(obj))
 128  
             .isEquals();
 129  
     }
 130  
 
 131  
     /* (non-Javadoc)
 132  
      * @see net.admin4j.exception.ExceptionInfoBase#hashCode()
 133  
      */
 134  
     @Override
 135  
     public int hashCode() {
 136  
         return new HashCodeBuilder(17,37)
 137  
             .appendSuper(super.hashCode())
 138  
             .toHashCode();
 139  
     }
 140  
 
 141  
     public String getAlternateId() {
 142  72
         return alternateId;
 143  
     }
 144  
 
 145  
     public void setAlternateId(String id) {
 146  84
         this.alternateId = id;
 147  84
     }
 148  
 
 149  
 }