View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.chainsaw;
    19
    20  import java.util.Comparator;
    21
    22  import org.apache.log4j.helpers.Constants;
    23  import org.apache.log4j.spi.LoggingEvent;
    24
    25
    26  /**
    27   *
    28   * @author Claude Duguay
    29   * @author Paul Smith &lt;psmith@apache.org&gt;
    30   * @author Scott Deboy &lt;sdeboy@apache.org&gt;
    31  */
    32  public class ColumnComparator implements Comparator {
    33    protected int index;
    34    protected boolean ascending;
    35    protected String columnName;
    36
    37    public ColumnComparator(String columnName, int index, boolean ascending) {
    38      this.columnName = columnName;
    39      this.index = index;
    40      this.ascending = ascending;
    41    }
    42
    43    public int compare(Object o1, Object o2) {
    44      int sort = 1;
    45
    46      if (o1 instanceof LoggingEventWrapper && o2 instanceof LoggingEventWrapper) {
    47
    48  //		TODO not everything catered for here yet...
    49
    50        LoggingEvent e1 = ((LoggingEventWrapper) o1).getLoggingEvent();
    51        LoggingEvent e2 = ((LoggingEventWrapper) o2).getLoggingEvent();
    52
    53        switch (index + 1) {
    54        case ChainsawColumns.INDEX_LEVEL_COL_NAME:
    55          sort = e1.getLevel().isGreaterOrEqual(e2.getLevel()) ? 1 : (-1);
    56
    57          break;
    58
    59        case ChainsawColumns.INDEX_LOGGER_COL_NAME:
    60          sort = e1.getLoggerName().compareToIgnoreCase(e2.getLoggerName());
    61
    62          break;
    63
    64        case ChainsawColumns.INDEX_MESSAGE_COL_NAME:
    65          sort =
    66            e1.getMessage().toString().compareToIgnoreCase(
    67              e2.getMessage().toString());
    68
    69          break;
    70
    71        case ChainsawColumns.INDEX_NDC_COL_NAME:
    72          if (e1.getNDC() != null && e2.getNDC() != null) {
    73              sort =
    74                  e1.getNDC().compareToIgnoreCase(
    75                  e2.getNDC());
    76          } else if (e1.getNDC() == null && e2.getNDC() == null) {
    77          	sort = 0;
    78          } else if (e1.getNDC() == null) {
    79          	sort = -1;
    80          } else if (e2.getNDC() == null) {
    81          	sort = 1;
    82          }
    83
    84          break;
    85
    86        case ChainsawColumns.INDEX_METHOD_COL_NAME:
    87
    88          if (
    89            (e1.locationInformationExists())
    90              & (e2.locationInformationExists())) {
    91            sort =
    92              e1.getLocationInformation().getMethodName().compareToIgnoreCase(
    93                e2.getLocationInformation().getMethodName());
    94          }
    95
    96          break;
    97
    98        case ChainsawColumns.INDEX_CLASS_COL_NAME:
    99
    100         if (
    101           (e1.locationInformationExists())
    102             & (e2.locationInformationExists())) {
    103           sort =
    104             e1.getLocationInformation().getClassName().compareToIgnoreCase(
    105               e2.getLocationInformation().getClassName());
    106         }
    107
    108         break;
    109
    110       case ChainsawColumns.INDEX_FILE_COL_NAME:
    111
    112         if (
    113           (e1.locationInformationExists())
    114             & (e2.locationInformationExists())) {
    115           sort =
    116             e1.getLocationInformation().getFileName().compareToIgnoreCase(
    117               e2.getLocationInformation().getFileName());
    118         }
    119
    120         break;
    121
    122        case ChainsawColumns.INDEX_TIMESTAMP_COL_NAME:
    123        		sort = (e1.getTimeStamp()<e2.getTimeStamp() ? -1 : (e1.getTimeStamp()==e2.getTimeStamp() ? 0 : 1));
    124        		break;
    125
    126        case ChainsawColumns.INDEX_THREAD_COL_NAME:
    127        		sort = e1.getThreadName().compareToIgnoreCase(e2.getThreadName());
    128        		break;
    129
    130        case ChainsawColumns.INDEX_ID_COL_NAME:
    131             int id1 = Integer.parseInt(e1.getProperty(Constants.LOG4J_ID_KEY));
    132             int id2 = Integer.parseInt(e2.getProperty(Constants.LOG4J_ID_KEY));
    133             if (id1 == id2) {
    134                 sort = 0;
    135             } else if (id1 < id2) {
    136                 sort = 1;
    137             } else {
    138                 sort = -1;
    139             }
    140             break;
    141
    142        case ChainsawColumns.INDEX_THROWABLE_COL_NAME:
    143             if (e1.getThrowableStrRep() != null && e2.getThrowableStrRep() != null) {
    144                String[] s1 = e1.getThrowableStrRep();
    145                String[] s2 = e2.getThrowableStrRep();
    146                boolean foundDiff = false;
    147                for (int i = 0;i<s1.length;i++) {
    148                    if (foundDiff || i > s2.length) {
    149                        break;
    150                    }
    151                    sort = s1[i].compareToIgnoreCase(s2[i]);
    152                    foundDiff = sort != 0;
    153                }
    154             }
    155             break;
    156
    157        case ChainsawColumns.INDEX_LINE_COL_NAME:
    158             if (
    159                 (e1.locationInformationExists())
    160                 & (e2.locationInformationExists())) {
    161                 sort =
    162                     e1.getLocationInformation().getLineNumber().compareToIgnoreCase(
    163                     e2.getLocationInformation().getLineNumber());
    164             }
    165             break;
    166
    167       //other columns may be Property values - see if there is an Property value matching column name
    168       default:
    169           if (e1.getProperty(columnName) != null && e2.getProperty(columnName) != null) {
    170               sort = e1.getProperty(columnName).toString().compareToIgnoreCase(e2.getProperty(columnName).toString());
    171           }
    172       }
    173     }
    174
    175     sort = (sort == 0) ? 0 : ((sort < 0) ? (-1) : 1);
    176
    177     if (!ascending && (sort != 0)) {
    178       sort = (sort < 0) ? 1 : (-1);
    179     }
    180
    181     return sort;
    182   }
    183 }