001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.log.log4j.appender.task; 020 021import lucee.commons.lang.ExceptionUtil; 022import lucee.loader.engine.CFMLEngine; 023import lucee.loader.engine.CFMLEngineFactory; 024import lucee.runtime.config.Config; 025import lucee.runtime.exp.PageException; 026import lucee.runtime.spooler.ExecutionPlan; 027import lucee.runtime.spooler.SpoolerTask; 028import lucee.runtime.type.Array; 029import lucee.runtime.type.Struct; 030import lucee.runtime.util.Cast; 031import lucee.runtime.util.Creation; 032 033import org.apache.log4j.Appender; 034import org.apache.log4j.spi.LoggingEvent; 035 036public class Task implements SpoolerTask { 037 038 private static final long serialVersionUID = 5649820047520607442L; 039 040 private String id; 041 private long lastExecution; 042 private long nextExecution; 043 private int tries=0; 044 private final Array exceptions; 045 private final long creation=System.currentTimeMillis(); 046 private boolean closed; 047 private final Struct detail; 048 049 private final Appender appender; 050 private final LoggingEvent le; 051 052 public Task(Appender appender, LoggingEvent le){ 053 this.appender=appender; 054 this.le=le; 055 CFMLEngine engine = CFMLEngineFactory.getInstance(); 056 exceptions=engine.getCreationUtil().createArray(); 057 detail=engine.getCreationUtil().createStruct(); 058 } 059 060 @Override 061 public final Object execute(Config config) throws PageException { 062 lastExecution=System.currentTimeMillis(); 063 tries++; 064 try{ 065 appender.doAppend(le); 066 return null; 067 } 068 catch(Throwable t) { 069 ExceptionUtil.rethrowIfNecessary(t); 070 CFMLEngine engine = CFMLEngineFactory.getInstance(); 071 Cast caster = engine.getCastUtil(); 072 Creation creator = engine.getCreationUtil(); 073 074 PageException pe = caster.toPageException(t); 075 076 Struct exception=creator.createStruct(); 077 exception.put("message", pe.getMessage()); 078 exception.put("detail", pe.getDetail()); 079 exception.put("type", pe.getTypeAsString()); 080 exception.put("stacktrace", pe.getStackTraceAsString()); 081 exception.put("class", pe.getClass().getName()); 082 exception.put("time", caster.toLong(System.currentTimeMillis())); 083 exceptions.appendEL(exception); 084 085 throw pe; 086 } 087 finally{ 088 lastExecution=System.currentTimeMillis(); 089 } 090 } 091 092 public Struct detail() { 093 return detail; 094 } 095 096 @Override 097 public final String subject() { 098 return appender.getName(); 099 } 100 101 @Override 102 public final String getType() { 103 return "log"; 104 } 105 106 @Override 107 public final Array getExceptions() { 108 return exceptions; 109 } 110 111 @Override 112 public final void setClosed(boolean closed) { 113 this.closed=closed; 114 } 115 116 @Override 117 public final boolean closed() { 118 return closed; 119 } 120 121 @Override 122 public final ExecutionPlan[] getPlans() { 123 return null; 124 } 125 126 @Override 127 public final long getCreation() { 128 return creation; 129 } 130 131 @Override 132 public final int tries() { 133 return tries; 134 } 135 136 @Override 137 public final void setLastExecution(long lastExecution) { 138 this.lastExecution=lastExecution; 139 } 140 141 @Override 142 public final long lastExecution() { 143 return lastExecution; 144 } 145 146 @Override 147 public final void setNextExecution(long nextExecution) { 148 149 this.nextExecution=nextExecution; 150 } 151 152 @Override 153 public final long nextExecution() { 154 return nextExecution; 155 } 156 157 @Override 158 public final String getId() { 159 return id; 160 } 161 162 @Override 163 public final void setId(String id) { 164 this.id= id; 165 } 166 167 168 169}