| 1 28 29 package com.idaremedia.antx.print; 30 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.PrintStream ; 34 import java.util.List ; 35 import java.util.Properties ; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.ErrorSnapshot; 39 import com.idaremedia.antx.helpers.Strings; 40 import com.idaremedia.antx.helpers.Tk; 41 import com.idaremedia.apis.UIStringManager; 42 43 55 56 public final class ErrorPrinter implements DisplayStrategy 57 { 58 61 public ErrorPrinter() 62 { 63 } 64 65 66 72 private Properties propertiesFromHeader(String id, ErrorSnapshot es) 73 { 74 75 Properties allP = new Properties (); 76 77 Object thrown = es.getThrown(); 78 if (thrown==null) { thrown = ""; } 79 allP.setProperty(uistrs().get("snapshot.print.thrown",id),thrown.toString()); 80 81 allP.setProperty(uistrs().get("snapshot.print.location",id),es.getLocation().toString()); 82 allP.setProperty(uistrs().get("snapshot.print.task",id),es.getTaskName()); 83 allP.setProperty(uistrs().get("snapshot.print.target",id),es.getTargetName()); 84 allP.setProperty(uistrs().get("snapshot.print.comment",id),es.getComment()); 85 86 return allP; 87 } 88 89 90 97 private Properties propertiesFromEnv(String id, ErrorSnapshot es, String list) 98 { 99 String ll= Tk.lowercaseFrom(list); 100 if (ll.length()==0 || Strings.NONE.equals(ll)) { 101 return null; 102 } 103 104 105 Properties allP = es.copyOfProperties(); 106 107 108 if (!Strings.ALL.equals(ll)) { 109 List wanted = Tk.splitList(list,","); 110 allP.keySet().retainAll(wanted); 111 112 if (wanted.size()>allP.size()) { for (int i=0,N=wanted.size();i<N;i++) { 114 String key = (String )wanted.get(i); 115 if (allP.getProperty(key)==null) { 116 allP.setProperty(key,Strings.NULL); 117 } 118 } 119 } 120 wanted=null; 121 } 122 123 return allP; 124 } 125 126 127 134 public void print(final DisplayRequest info, OutputStream os) 135 throws IOException  136 { 137 Object target = info.getObjectToBeDisplayed(); 138 139 140 if (target instanceof ErrorSnapshot) { 141 142 ErrorSnapshot es = (ErrorSnapshot)target; 143 String id = info.getName(); 144 if (id==null) { 145 id = es.getName(); 146 if (id==null) { 147 id = Strings.NULL; 148 } 149 } 150 151 Properties allP; 152 153 154 new PrintStream (os).println("########"); 155 allP = propertiesFromHeader(id,es); 156 allP.store(os,uistrs().get("snapshot.print.header",id)); 157 allP.clear(); 158 allP=null; 159 160 161 String list = info.getFilter(); 162 if (list==null) { 163 list = Strings.ALL; 164 } 165 allP = propertiesFromEnv(id,es,list); 166 if (allP!=null) { 167 allP.store(os,uistrs().get("snapshot.print.content",id)); 168 allP.clear(); 169 allP=null; 170 } 171 } 172 173 else if (target instanceof Throwable ) { 174 175 Throwable thrown = (Throwable )target; 176 PrintStream ps = new PrintStream (os); 177 178 ps.println("########"); 179 ps.print("#"); 180 ps.println(uistrs().get("thrown.print.header",Tk.leafNameFrom(thrown.getClass()))); 181 ps.print("#"); 182 ps.println(new java.util.Date ()); 183 thrown.printStackTrace(ps); 184 ps.flush(); 185 ps=null; 186 } 187 } 188 189 190 private UIStringManager uistrs() 191 { 192 return AntX.uistrs(); 193 } 194 } 195 196 197 | Popular Tags |