001package com.ganteater.ae.web; 002 003import java.io.File; 004import java.util.ArrayList; 005import java.util.List; 006 007import org.apache.commons.lang.ArrayUtils; 008import org.apache.commons.lang.ObjectUtils; 009import org.apache.commons.lang.exception.ExceptionUtils; 010import org.apache.logging.log4j.Level; 011 012import com.ganteater.ae.AELogRecord; 013import com.ganteater.ae.AEWorkspace; 014import com.ganteater.ae.ChoiceTaskRunner; 015import com.ganteater.ae.ILogger; 016import com.ganteater.ae.Logger; 017import com.ganteater.ae.RecipeRunner; 018 019public class WebLogger implements ILogger { 020 021 private List<WebLogRecord> logRecords = new ArrayList<WebLogRecord>(); 022 private RecipeRunner testRunner; 023 private Object monitor = new Object(); 024 private WebInputItem input; 025 private int maxRecordsInMemory = 50; 026 private int startIndex = 0; 027 private Logger logger; 028 private String name; 029 030 protected WebLogger(String name) { 031 this.name = name; 032 logger = new Logger(getName()); 033 } 034 035 public Object error(Object message) { 036 addLogRecord(null, message, Level.ERROR, null); 037 message = logger.error(message); 038 return message; 039 } 040 041 public Object error(Object message, Throwable t) { 042 addLogRecord(null, message, Level.ERROR, t); 043 message = logger.error(message, t); 044 return message; 045 } 046 047 public Object debug(Object message) { 048 addLogRecord(null, message, Level.DEBUG, null); 049 message = logger.debug(message); 050 return message; 051 } 052 053 public Object debug(Object message, Throwable t) { 054 addLogRecord(null, message, Level.DEBUG, t); 055 message = logger.debug(message, t); 056 return message; 057 } 058 059 public Object info(Object message) { 060 addLogRecord(null, message, Level.INFO, null); 061 message = logger.info(message); 062 return message; 063 } 064 065 public Object warn(Object message) { 066 addLogRecord(null, message, Level.WARN, null); 067 message = logger.warn(message); 068 return message; 069 } 070 071 private void addLogRecord(String varName, Object message, Level level, Throwable t) { 072 WebLogRecord element; 073 074 if (message instanceof AELogRecord) { 075 AELogRecord rec = (AELogRecord) message; 076 element = new WebLogRecord(rec, level, varName); 077 } else { 078 String text = ObjectUtils.toString(message); 079 element = new WebLogRecord(new AELogRecord(text, "txt", varName), level, varName); 080 } 081 082 if (t != null) { 083 element.setTrace(ExceptionUtils.getStackTrace(t)); 084 } 085 086 if (logRecords.size() == maxRecordsInMemory) { 087 logRecords.remove(0); 088 startIndex++; 089 } 090 logRecords.add(element); 091 } 092 093 public List<WebLogRecord> getLog(int id) { 094 List<WebLogRecord> result = new ArrayList<WebLogRecord>(); 095 int startIndex = id - this.startIndex; 096 if (startIndex < 0) { 097 startIndex = 0; 098 } 099 for (int i = startIndex; i < logRecords.size(); i++) { 100 WebLogRecord record = logRecords.get(i); 101 record.setId(i + this.startIndex); 102 result.add(record); 103 } 104 return result; 105 } 106 107 public void setTestRunner(RecipeRunner testRunner) { 108 this.testRunner = testRunner; 109 } 110 111 public RecipeRunner getTestRunner() { 112 return testRunner; 113 } 114 115 public String inputValue(String name, String description, String defaultValue, String type) { 116 input = new WebInputItem(name, description, defaultValue, type); 117 synchronized (monitor) { 118 try { 119 monitor.wait(); 120 } catch (InterruptedException e) { 121 Thread.currentThread().interrupt(); 122 } 123 } 124 String value = input.getValue(); 125 input = null; 126 return value; 127 } 128 129 public void applyInput(String[] value) { 130 if (input instanceof WebChoiceItem) { 131 ((WebChoiceItem) input).select(value); 132 133 } else if (input instanceof WebInputItem && ArrayUtils.isNotEmpty(value)) { 134 input.setValue(value[0]); 135 AEWorkspace.getInstance().setDefaultUserConfiguration(".inputValue." + name, value[0]); 136 137 } 138 synchronized (monitor) { 139 monitor.notify(); 140 } 141 } 142 143 public WebInputItem getInput() { 144 return input; 145 } 146 147 public void inputChoice(ChoiceTaskRunner tasksChoice) { 148 input = new WebChoiceItem(tasksChoice); 149 synchronized (monitor) { 150 try { 151 monitor.wait(); 152 } catch (InterruptedException e) { 153 Thread.currentThread().interrupt(); 154 } 155 } 156 tasksChoice.start(); 157 input = null; 158 } 159 160 public String inputSelectChoice(String name, String description, String[] value, String defaultValue) { 161 input = new WebSelectItem(name, description, value, defaultValue); 162 synchronized (monitor) { 163 try { 164 monitor.wait(); 165 } catch (InterruptedException e) { 166 // 167 } 168 } 169 String result = input.getValue(); 170 input = null; 171 return result; 172 } 173 174 public String inputChoiceValue(String name, String desc, Object[] aPossibleValues, String value) { 175 input = new WebSelectItem(name, desc, aPossibleValues, value); 176 synchronized (monitor) { 177 try { 178 monitor.wait(); 179 } catch (InterruptedException e) { 180 // 181 } 182 } 183 value = input.getValue(); 184 if (WebSelectItem.CANCEL_X == value) { 185 value = null; 186 } 187 input = null; 188 return value; 189 } 190 191 public String inputFile(String name, File aDefaultFile, ILogger log) { 192 input = new FileUploadItem(name, aDefaultFile); 193 synchronized (monitor) { 194 try { 195 monitor.wait(); 196 } catch (InterruptedException e) { 197 // 198 } 199 } 200 String value = input.getValue(); 201 if (WebSelectItem.CANCEL_X == value) { 202 value = null; 203 } 204 input = null; 205 return value; 206 } 207 208 public WebLogRecord getRecord(int id) { 209 int index = id - startIndex; 210 if (index < 0) { 211 return new WebLogRecord( 212 new AELogRecord("The record is no longer in a memory. Please see server log file .", "txt", null), 213 Level.ERROR, null); 214 } 215 return logRecords.get(index); 216 } 217 218 public void reset() { 219 synchronized (monitor) { 220 monitor.notify(); 221 } 222 } 223 224 public int getStartIndex() { 225 return startIndex; 226 } 227 228 synchronized public void moveToArchive() { 229 this.startIndex = logRecords.size(); 230 logRecords.clear(); 231 } 232 233 @Override 234 public String getName() { 235 return name; 236 } 237 238 @Override 239 public boolean isFilterEnabled() { 240 // TODO Auto-generated method stub 241 return false; 242 } 243 244 @Override 245 public String filter(Object message) { 246 // TODO Auto-generated method stub 247 return null; 248 } 249 250}