View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.status;
15  
16  import java.util.ArrayList;
17  import java.util.Iterator;
18  import java.util.List;
19  
20  abstract public class StatusBase implements Status {
21  
22      static private final List<Status> EMPTY_LIST = new ArrayList<Status>(0);
23  
24      int level;
25      final String message;
26      final Object origin;
27      List<Status> childrenList;
28      Throwable throwable;
29      long date;
30  
31      StatusBase(int level, String msg, Object origin) {
32          this(level, msg, origin, null);
33      }
34  
35      StatusBase(int level, String msg, Object origin, Throwable t) {
36          this.level = level;
37          this.message = msg;
38          this.origin = origin;
39          this.throwable = t;
40          this.date = System.currentTimeMillis();
41      }
42  
43      public synchronized void add(Status child) {
44          if (child == null) {
45              throw new NullPointerException("Null values are not valid Status.");
46          }
47          if (childrenList == null) {
48              childrenList = new ArrayList<Status>();
49          }
50          childrenList.add(child);
51      }
52  
53      public synchronized boolean hasChildren() {
54          return ((childrenList != null) && (childrenList.size() > 0));
55      }
56  
57      public synchronized Iterator<Status> iterator() {
58          if (childrenList != null) {
59              return childrenList.iterator();
60          } else {
61              return EMPTY_LIST.iterator();
62          }
63      }
64  
65      public synchronized boolean remove(Status statusToRemove) {
66          if (childrenList == null) {
67              return false;
68          }
69          // TODO also search in childrens' children
70          return childrenList.remove(statusToRemove);
71      }
72  
73      public int getLevel() {
74          return level;
75      }
76  
77      // status messages are not supposed to contains cycles.
78      // cyclic status arrangements are like to cause deadlocks
79      // when this method is called from different thread on
80      // different status objects lying on the same cycle
81      public synchronized int getEffectiveLevel() {
82          int result = level;
83          int effLevel;
84  
85          Iterator<Status> it = iterator();
86          Status s;
87          while (it.hasNext()) {
88              s = (Status) it.next();
89              effLevel = s.getEffectiveLevel();
90              if (effLevel > result) {
91                  result = effLevel;
92              }
93          }
94          return result;
95      }
96  
97      public String getMessage() {
98          return message;
99      }
100 
101     public Object getOrigin() {
102         return origin;
103     }
104 
105     public Throwable getThrowable() {
106         return throwable;
107     }
108 
109     public Long getDate() {
110         return date;
111     }
112 
113     @Override
114     public String toString() {
115         StringBuilder buf = new StringBuilder();
116         switch (getEffectiveLevel()) {
117         case INFO:
118             buf.append("INFO");
119             break;
120         case WARN:
121             buf.append("WARN");
122             break;
123         case ERROR:
124             buf.append("ERROR");
125             break;
126         }
127         if (origin != null) {
128             buf.append(" in ");
129             buf.append(origin);
130             buf.append(" -");
131         }
132 
133         buf.append(" ");
134         buf.append(message);
135 
136         if (throwable != null) {
137             buf.append(" ");
138             buf.append(throwable);
139         }
140 
141         return buf.toString();
142     }
143 
144     @Override
145     public int hashCode() {
146         final int prime = 31;
147         int result = 1;
148         result = prime * result + level;
149         result = prime * result + ((message == null) ? 0 : message.hashCode());
150         return result;
151     }
152 
153     @Override
154     public boolean equals(Object obj) {
155         if (this == obj)
156             return true;
157         if (obj == null)
158             return false;
159         if (getClass() != obj.getClass())
160             return false;
161         final StatusBase other = (StatusBase) obj;
162         if (level != other.level)
163             return false;
164         if (message == null) {
165             if (other.message != null)
166                 return false;
167         } else if (!message.equals(other.message))
168             return false;
169         return true;
170     }
171 
172 }