View Javadoc

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