1 16 17 package org.apache.commons.dbcp; 18 19 import java.text.SimpleDateFormat ; 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 36 public class AbandonedTrace { 37 38 private static SimpleDateFormat format = new SimpleDateFormat 39 ("'DBCP object created' yyyy-MM-dd HH:mm:ss " + 40 "'by the following code was never closed:'"); 41 42 private AbandonedConfig config = null; 44 private AbandonedTrace parent; 46 private Exception createdBy; 48 private long createdTime; 49 private List trace = new ArrayList (); 51 private long lastUsed = 0; 53 54 58 public AbandonedTrace() { 59 init(parent); 60 } 61 62 67 public AbandonedTrace(AbandonedConfig config) { 68 this.config = config; 69 init(parent); 70 } 71 72 77 public AbandonedTrace(AbandonedTrace parent) { 78 this.config = parent.getConfig(); 79 init(parent); 80 } 81 82 87 private void init(AbandonedTrace parent) { 88 if (parent != null) { 89 parent.addTrace(this); 90 } 91 92 if (config == null) { 93 return; 94 } 95 if (config.getLogAbandoned()) { 96 createdBy = new Exception (); 97 createdTime = System.currentTimeMillis(); 98 } 99 } 100 101 106 protected AbandonedConfig getConfig() { 107 return config; 108 } 109 110 115 protected long getLastUsed() { 116 if (parent != null) { 117 return parent.getLastUsed(); 118 } 119 return lastUsed; 120 } 121 122 126 protected void setLastUsed() { 127 if (parent != null) { 128 parent.setLastUsed(); 129 } else { 130 lastUsed = System.currentTimeMillis(); 131 } 132 } 133 134 139 protected void setLastUsed(long time) { 140 if (parent != null) { 141 parent.setLastUsed(time); 142 } else { 143 lastUsed = time; 144 } 145 } 146 147 152 protected void setStackTrace() { 153 if (config == null) { 154 return; 155 } 156 if (config.getLogAbandoned()) { 157 createdBy = new Exception (); 158 createdTime = System.currentTimeMillis(); 159 } 160 if (parent != null) { 161 parent.addTrace(this); 162 } 163 } 164 165 171 protected void addTrace(AbandonedTrace trace) { 172 synchronized(this) { 173 this.trace.add(trace); 174 } 175 setLastUsed(); 176 } 177 178 182 protected synchronized void clearTrace() { 183 if (this.trace != null) { 184 this.trace.clear(); 185 } 186 } 187 188 193 protected List getTrace() { 194 return trace; 195 } 196 197 201 public void printStackTrace() { 202 if (createdBy != null) { 203 System.out.println(format.format(new Date (createdTime))); 204 createdBy.printStackTrace(System.out); 205 } 206 synchronized(this) { 207 Iterator it = this.trace.iterator(); 208 while (it.hasNext()) { 209 AbandonedTrace at = (AbandonedTrace)it.next(); 210 at.printStackTrace(); 211 } 212 } 213 } 214 215 220 protected synchronized void removeTrace(AbandonedTrace trace) { 221 if (this.trace != null) { 222 this.trace.remove(trace); 223 } 224 } 225 226 } 227 | Popular Tags |