accesslog.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 788 行 · 第 1/2 页

JAVA
788
字号
    for (int i = 0; i < len; i++) {      Segment segment = _segments[i];      String value = null;      CharBuffer cbValue = null;      CharSegment csValue = null;      switch (segment._code) {      case Segment.TEXT:        int sublen = segment._data.length;        byte []data = segment._data;        for (int j = 0; j < sublen; j++)          buffer[offset++] = data[j];	break;              case Segment.CHAR:        buffer[offset++] = segment._ch;	break;      case 'b':        if (response.getStatusCode() == 304)          buffer[offset++] = (byte) '-';        else          offset = print(buffer, offset, response.getContentLength());	break;        // cookie      case 'c':        Cookie cookie = request.getCookie(segment._string);        if (cookie == null)          cookie = response.getCookie(segment._string);        if (cookie == null)          buffer[offset++] = (byte) '-';        else          offset = print(buffer, offset, cookie.getValue());	break;                // set cookie      case Segment.SET_COOKIE:        ArrayList cookies = response.getCookies();        if (cookies == null || cookies.size() == 0)          buffer[offset++] = (byte) '-';        else {          _cb.clear();          response.fillCookie(_cb, (Cookie) cookies.get(0), 0, 0, false);          offset = print(buffer, offset, _cb.getBuffer(), 0, _cb.getLength());        }	break;      case 'h':	if (isHostnameDnsLookup()) {	  String addrName = request.getRemoteAddr();	  InetAddress addr = InetAddress.getByName(addrName);	  offset = print(buffer, offset, addr.getHostName());	}	else	  offset = request.printRemoteAddr(buffer, offset);	break;        // input header      case 'i':	csValue = request.getHeaderBuffer(segment._string);        if (csValue == null)          buffer[offset++] = (byte) '-';        else          offset = print(buffer, offset, csValue);	break;      case 'l':        buffer[offset++] = (byte) '-';	break;        // request attribute      case 'n':        Object oValue = request.getAttribute(segment._string);        if (oValue == null)          buffer[offset++] = (byte) '-';        else          offset = print(buffer, offset, String.valueOf(oValue));	break;        // output header      case 'o':	value = response.getHeader(segment._string);        if (value == null)          buffer[offset++] = (byte) '-';        else          offset = print(buffer, offset, value);	break;      case 'r':	offset = print(buffer, offset, request.getMethod());                buffer[offset++] = (byte) ' ';                data = request.getUriBuffer();        sublen = request.getUriLength();	System.arraycopy(data, 0, buffer, offset, sublen);        offset += sublen;        buffer[offset++] = (byte) ' ';        	offset = print(buffer, offset, request.getProtocol());	break;      case 's':        int status = response.getStatusCode();        buffer[offset++] = (byte) ('0' + (status / 100) % 10);        buffer[offset++] = (byte) ('0' + (status / 10) % 10);        buffer[offset++] = (byte) ('0' + status % 10);	break;      case 't':        long date = Alarm.getCurrentTime();                if (date / 1000 != _lastTime / 1000)          fillTime(date);	sublen = _timeBuffer.getLength();	data = _timeBuffer.getBuffer();		synchronized (_timeBuffer) {	  System.arraycopy(data, 0, buffer, offset, sublen);	}        offset += sublen;	break;      case 'T':	{	  long startTime = request.getStartTime();	  long endTime = Alarm.getExactTime();	  offset = print(buffer, offset, (int) ((endTime - startTime + 500) / 1000));	  break;	}      case 'D':	{	  long startTime = request.getStartTime();	  long endTime = Alarm.getExactTime();	  offset = print(buffer, offset, (int) ((endTime - startTime) * 1000));	  break;	}      case 'u':	value = request.getRemoteUser(false);        if (value == null)          buffer[offset++] = (byte) '-';        else {          buffer[offset++] = (byte) '"';          offset = print(buffer, offset, value);          buffer[offset++] = (byte) '"';        }	break;      case 'U':        offset = print(buffer, offset, request.getRequestURI());	break;      default:	throw new IOException();      }    }    return offset;  }  /**   * Prints a CharSegment to the log.   *   * @param buffer receiving byte buffer.   * @param offset offset into the receiving buffer.   * @param cb the new char segment to be logged.   * @return the new offset into the byte buffer.   */  private int print(byte []buffer, int offset, CharSegment cb)  {    char []charBuffer = cb.getBuffer();    int cbOffset = cb.getOffset();    int length = cb.getLength();    // truncate for hacker attacks    if (buffer.length - offset - 256 < length)      length =  buffer.length - offset - 256;    for (int i = length - 1; i >= 0; i--)      buffer[offset + i] = (byte) charBuffer[cbOffset + i];    return offset + length;  }  /**   * Prints a String to the log.   *   * @param buffer receiving byte buffer.   * @param offset offset into the receiving buffer.   * @param s the new string to be logged.   * @return the new offset into the byte buffer.   */  private int print(byte []buffer, int offset, String s)  {    int length = s.length();    _cb.ensureCapacity(length);    char []cBuf = _cb.getBuffer();    s.getChars(0, length, cBuf, 0);    for (int i = length - 1; i >= 0; i--)      buffer[offset + i] = (byte) cBuf[i];    return offset + length;  }  /**   * Prints a String to the log.   *   * @param buffer receiving byte buffer.   * @param offset offset into the receiving buffer.   * @param s the new string to be logged.   * @return the new offset into the byte buffer.   */  private int print(byte []buffer, int offset,                    char []cb, int cbOff, int length)  {    for (int i = length - 1; i >= 0; i--)      buffer[offset + i] = (byte) cb[cbOff + i];    return offset + length;  }  /**   * Prints an integer to the log.   *   * @param buffer receiving byte buffer.   * @param offset offset into the receiving buffer.   * @param v the new integer to be logged.   * @return the new offset into the byte buffer.   */  private int print(byte []buffer, int offset, long v)  {    if (v == 0) {      buffer[offset] = (byte) '0';      return offset + 1;    }    if (v < 0) {      buffer[offset++] = (byte) '-';      v = -v;    }    int length = 0;    int exp = 10;        for (; exp <= v && exp > 0; length++)      exp = 10 * exp;    offset += length;    for (int i = 0; i <= length; i++) {      buffer[offset - i] = (byte) (v % 10 + '0');      v = v / 10;    }    return offset + 1;  }  /**   * Flushes the log.   */  public void flush()  {    // server/0213, 021q    _logWriter.flush();    _logWriter.waitForFlush(5000L);    _logWriter.rollover();  }  /**   * The alarm listener.   */  public void handleAlarm(Alarm alarm)  {    try {      flush();    } finally {      alarm = _alarm;      if (alarm != null && _autoFlushTime > 0)	alarm.queue(_autoFlushTime);    }  }  /**   * Closes the log, flushing the results.   */  public void destroy()    throws IOException  {    _isActive = false;        Alarm alarm = _alarm;;    _alarm = null;    if (alarm != null)      alarm.dequeue();    flush();    _logWriter.close();  }  /**   * Fills the time buffer with the formatted time.   *   * @param date current time in milliseconds   */  private void fillTime(long date)    throws IOException  {    if (date / 1000 == _lastTime / 1000)      return;    synchronized (_timeBuffer) {      if (_timeFormatSecondOffset >= 0 && date / 60000 == _lastTime / 60000) {	byte []bBuf = _timeBuffer.getBuffer();		int sec = (int) (date / 1000 % 60);	bBuf[_timeFormatSecondOffset + 0] = (byte) ('0' + sec / 10);	bBuf[_timeFormatSecondOffset + 1] = (byte) ('0' + sec % 10);	return;      }            _timeCharBuffer.clear();      QDate.formatLocal(_timeCharBuffer, date, _timeFormat);      if (_timeFormatSecondOffset >= 0)	_timeFormatSecondOffset = _timeCharBuffer.lastIndexOf(':') + 1;            char []cBuf = _timeCharBuffer.getBuffer();      int length = _timeCharBuffer.getLength();      _timeBuffer.setLength(length);      byte []bBuf = _timeBuffer.getBuffer();      for (int i = length - 1; i >= 0; i--)	bBuf[i] = (byte) cBuf[i];    }          _lastTime = date;  }  /**   * Represents one portion of the access log.   */  static class Segment {    final static int TEXT = 0;    final static int CHAR = 1;    final static int SET_COOKIE = 2;        int _code;    byte []_data;    byte _ch;    String _string;    AccessLog _log;    /**     * Creates a new log segment.     *     * @param log the owning log     * @param code the segment code, telling what kind of segment it is     * @param string the parameter for the segment code.     */    Segment(AccessLog log, int code, String string)    {      _log = log;      _code = code;            _string = string;      if (string != null) {        if (code == 'o' && string.equalsIgnoreCase("Set-Cookie"))          _code = SET_COOKIE;                  _data = _string.getBytes();        if (code == TEXT && _string.length() == 1) {          _ch = (byte) _string.charAt(0);          _code = CHAR;        }      }    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?