1 56 57 package org.objectstyle.cayenne.access.util; 58 59 import java.io.PrintWriter ; 60 import java.util.ArrayList ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.apache.log4j.Level; 67 import org.apache.log4j.Logger; 68 import org.objectstyle.cayenne.CayenneException; 69 import org.objectstyle.cayenne.access.OperationObserver; 70 import org.objectstyle.cayenne.access.ResultIterator; 71 import org.objectstyle.cayenne.query.Query; 72 import org.objectstyle.cayenne.util.Util; 73 74 86 public class DefaultOperationObserver implements OperationObserver { 87 88 private static Logger logObj = Logger.getLogger(DefaultOperationObserver.class); 89 90 public static final Level DEFAULT_LOG_LEVEL = Query.DEFAULT_LOG_LEVEL; 91 92 protected List globalExceptions = new ArrayList (); 93 protected Map queryExceptions = new HashMap (); 94 protected Level loggingLevel = DEFAULT_LOG_LEVEL; 95 96 99 public void printExceptions(PrintWriter out) { 100 if (globalExceptions.size() > 0) { 101 if (globalExceptions.size() == 1) { 102 out.println("Global Exception:"); 103 } 104 else { 105 out.println("Global Exceptions:"); 106 } 107 108 Iterator it = globalExceptions.iterator(); 109 while (it.hasNext()) { 110 Throwable th = (Throwable ) it.next(); 111 th.printStackTrace(out); 112 } 113 } 114 115 if (queryExceptions.size() > 0) { 116 if (queryExceptions.size() == 1) { 117 out.println("Query Exception:"); 118 } 119 else { 120 out.println("Query Exceptions:"); 121 } 122 123 Iterator it = queryExceptions.keySet().iterator(); 124 while (it.hasNext()) { 125 Throwable th = (Throwable ) queryExceptions.get(it.next()); 126 th.printStackTrace(out); 127 } 128 } 129 } 130 131 132 public List getGlobalExceptions() { 133 return globalExceptions; 134 } 135 136 137 public Map getQueryExceptions() { 138 return queryExceptions; 139 } 140 141 145 public boolean hasExceptions() { 146 return globalExceptions.size() > 0 || queryExceptions.size() > 0; 147 } 148 149 152 public Level getLoggingLevel() { 153 return loggingLevel; 154 } 155 156 161 public void setLoggingLevel(Level level) { 162 this.loggingLevel = (level == null) ? DEFAULT_LOG_LEVEL : level; 163 } 164 165 public void nextCount(Query query, int resultCount) { 166 } 167 168 public void nextBatchCount(Query query, int[] resultCount) { 169 if (logObj.isDebugEnabled()) { 170 for (int i = 0; i < resultCount.length; i++) { 171 logObj.debug("batch count: " + resultCount[i]); 172 } 173 } 174 } 175 176 public void nextDataRows(Query query, List dataRows) { 177 } 179 180 184 public void nextDataRows(Query query, ResultIterator it) { 185 if (it != null) { 186 try { 187 it.close(); 188 } 189 catch (CayenneException ex) { 190 nextQueryException(query, ex); 192 } 193 } 194 } 195 196 202 public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) { 203 if (keysIterator != null) { 204 try { 205 keysIterator.close(); 206 } 207 catch (CayenneException ex) { 208 nextQueryException(query, ex); 210 } 211 } 212 } 213 214 public void nextQueryException(Query query, Exception ex) { 215 queryExceptions.put(query, Util.unwindException(ex)); 216 } 217 218 public void nextGlobalException(Exception ex) { 219 globalExceptions.add(Util.unwindException(ex)); 220 } 221 222 225 public boolean isIteratedResult() { 226 return false; 227 } 228 } | Popular Tags |