View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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 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   /**
114    * @Override
115    */
116   public String toString() {
117     StringBuffer buf = new StringBuffer();
118     switch (getEffectiveLevel()) {
119     case INFO:
120       buf.append("INFO");
121       break;
122     case WARN:
123       buf.append("WARN");
124       break;
125     case ERROR:
126       buf.append("ERROR");
127       break;
128     }
129     if (origin != null) {
130       buf.append(" in ");
131       buf.append(origin);
132       buf.append(" -");
133     }
134 
135     buf.append(" ");
136     buf.append(message);
137 
138     if (throwable != null) {
139       buf.append(" ");
140       buf.append(throwable);
141     }
142 
143     return buf.toString();
144   }
145 
146   @Override
147   public int hashCode() {
148     final int prime = 31;
149     int result = 1;
150     result = prime * result + level;
151     result = prime * result + ((message == null) ? 0 : message.hashCode());
152     return result;
153   }
154 
155   @Override
156   public boolean equals(Object obj) {
157     if (this == obj)
158       return true;
159     if (obj == null)
160       return false;
161     if (getClass() != obj.getClass())
162       return false;
163     final StatusBase other = (StatusBase) obj;
164     if (level != other.level)
165       return false;
166     if (message == null) {
167       if (other.message != null)
168         return false;
169     } else if (!message.equals(other.message))
170       return false;
171     return true;
172   }
173 
174   
175 }