1 55 56 package org.jboss.axis.handlers; 57 58 import org.jboss.axis.AxisFault; 59 import org.jboss.axis.Message; 60 import org.jboss.axis.MessageContext; 61 import org.jboss.axis.utils.Messages; 62 import org.jboss.logging.Logger; 63 64 import java.io.FileWriter ; 65 import java.io.IOException ; 66 import java.io.PrintWriter ; 67 68 80 public class LogHandler extends BasicHandler 81 { 82 private static Logger log = Logger.getLogger(LogHandler.class.getName()); 83 84 long start = -1; 85 private boolean writeToConsole = false; 86 private String filename = "axis.log"; 87 88 public void init() 89 { 90 super.init(); 91 92 Object opt = this.getOption("LogHandler.writeToConsole"); 93 if (opt != null && opt instanceof String && 94 "true".equalsIgnoreCase((String )opt)) 95 writeToConsole = true; 96 97 opt = this.getOption("LogHandler.fileName"); 98 if (opt != null && opt instanceof String ) 99 filename = (String )opt; 100 } 101 102 public void invoke(MessageContext msgContext) throws AxisFault 103 { 104 log.debug("Enter: LogHandler::invoke"); 105 if (msgContext.getPastPivot() == false) 106 { 107 start = System.currentTimeMillis(); 108 } 109 else 110 { 111 logMessages(msgContext); 112 } 113 log.debug("Exit: LogHandler::invoke"); 114 } 115 116 private void logMessages(MessageContext msgContext) throws AxisFault 117 { 118 try 119 { 120 PrintWriter writer = null; 121 122 writer = getWriter(msgContext); 123 124 Message inMsg = msgContext.getRequestMessage(); 125 Message outMsg = msgContext.getResponseMessage(); 126 127 writer.println("======================================================="); 128 if (start != -1) 129 { 130 writer.println("= " + Messages.getMessage("elapsed00", 131 "" + (System.currentTimeMillis() - start))); 132 } 133 writer.println("= " + Messages.getMessage("inMsg00", 134 (inMsg == null ? "null" : inMsg.getSOAPPartAsString()))); 135 writer.println("= " + Messages.getMessage("outMsg00", 136 (outMsg == null ? "null" : outMsg.getSOAPPartAsString()))); 137 writer.println("======================================================="); 138 139 if (!writeToConsole) 141 { 142 writer.close(); 143 } 144 146 } 147 catch (Exception e) 148 { 149 log.error(Messages.getMessage("exception00"), e); 150 throw AxisFault.makeFault(e); 151 } 152 } 153 154 private PrintWriter getWriter(MessageContext msgContext) throws IOException 155 { 156 PrintWriter writer; 157 158 if (writeToConsole) 160 { 161 writer = new PrintWriter (System.out); 163 } 164 else 165 { 166 if (filename == null) 168 { 169 filename = "axis.log"; 170 } 171 writer = new PrintWriter (new FileWriter (filename, true)); 172 } 173 return writer; 174 } 175 176 177 public void onFault(MessageContext msgContext) 178 { 179 try 180 { 181 logMessages(msgContext); 182 } 183 catch (AxisFault axisFault) 184 { 185 log.error(Messages.getMessage("exception00"), axisFault); 186 } 187 } 188 } 189 190 ; 191 | Popular Tags |