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;
19  
20  import org.apache.log4j.spi.ErrorHandler;
21  import org.apache.log4j.spi.LoggingEvent;
22  
23  import java.util.Vector;
24  
25  
26  /**
27   * Utility class used in testing to capture errors dispatched
28   * by appenders.
29   *
30   * @author Curt Arnold
31   */
32  public final class VectorErrorHandler implements ErrorHandler {
33    /**
34     * Logger.
35     */
36    private Logger logger;
37  
38    /**
39     * Appender.
40     */
41    private Appender appender;
42  
43    /**
44     * Backup appender.
45     */
46    private Appender backupAppender;
47  
48    /**
49     * Array of processed errors.
50     */
51    private final Vector errors = new Vector();
52  
53    /**
54     * Default constructor.
55     */
56    public VectorErrorHandler() {
57    }
58  
59    /**
60     * {@inheritDoc}
61     */
62    public void setLogger(final Logger logger) {
63      this.logger = logger;
64    }
65  
66    /**
67     * Gets last logger specified by setLogger.
68     * @return logger.
69     */
70    public Logger getLogger() {
71      return logger;
72    }
73  
74    /**
75     * {@inheritDoc}
76     */
77    public void activateOptions() {
78    }
79  
80    /**
81     * {@inheritDoc}
82     */
83    public void error(
84      final String message, final Exception e, final int errorCode) {
85      error(message, e, errorCode, null);
86    }
87  
88    /**
89     * {@inheritDoc}
90     */
91    public void error(final String message) {
92      error(message, null, -1, null);
93    }
94  
95    /**
96     * {@inheritDoc}
97     */
98    public void error(
99      final String message, final Exception e, final int errorCode,
100     final LoggingEvent event) {
101     errors.addElement(
102       new Object[] { message, e, new Integer(errorCode), event });
103   }
104 
105   /**
106    * Gets message from specified error.
107    *
108    * @param index index.
109    * @return message, may be null.
110    */
111   public String getMessage(final int index) {
112     return (String) ((Object[]) errors.elementAt(index))[0];
113   }
114 
115   /**
116    * Gets exception from specified error.
117    *
118    * @param index index.
119    * @return exception.
120    */
121   public Exception getException(final int index) {
122     return (Exception) ((Object[]) errors.elementAt(index))[1];
123   }
124 
125   /**
126    * Gets error code from specified error.
127    *
128    * @param index index.
129    * @return error code, -1 if not specified.
130    */
131   public int getErrorCode(final int index) {
132     return ((Integer) ((Object[]) errors.elementAt(index))[2]).intValue();
133   }
134 
135   /**
136    * Gets logging event from specified error.
137    *
138    * @param index index.
139    * @return exception.
140    */
141   public LoggingEvent getEvent(final int index) {
142     return (LoggingEvent) ((Object[]) errors.elementAt(index))[3];
143   }
144 
145   /**
146    * Gets number of errors captured.
147    * @return number of errors captured.
148    */
149   public int size() {
150     return errors.size();
151   }
152 
153   /**
154    * {@inheritDoc}
155    */
156   public void setAppender(final Appender appender) {
157     this.appender = appender;
158   }
159 
160   /**
161    * Get appender.
162    * @return appender, may be null.
163    */
164   public Appender getAppender() {
165     return appender;
166   }
167 
168   /**
169    * {@inheritDoc}
170    */
171   public void setBackupAppender(final Appender appender) {
172     this.backupAppender = appender;
173   }
174 
175   /**
176    * Get backup appender.
177    * @return backup appender, may be null.
178    */
179   public Appender getBackupAppender() {
180     return backupAppender;
181   }
182 }