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.db;
19  
20  import org.apache.log4j.spi.LoggingEvent;
21  
22  import java.sql.Connection;
23  import java.sql.SQLException;
24  import java.sql.Statement;
25  import java.util.Set;
26  
27  /**
28   * @author Ceki Gülcü
29   */
30  public class DBHelper {
31  
32      public final static short PROPERTIES_EXIST = 0x01;
33      public final static short EXCEPTION_EXISTS = 0x02;
34  
35      public static short computeReferenceMask(LoggingEvent event) {
36          short mask = 0;
37          Set propertiesKeys = event.getPropertyKeySet();
38          if (propertiesKeys.size() > 0) {
39              mask = PROPERTIES_EXIST;
40          }
41          String[] strRep = event.getThrowableStrRep();
42          if (strRep != null) {
43              mask |= EXCEPTION_EXISTS;
44          }
45          return mask;
46      }
47  
48      static public void closeConnection(Connection connection) {
49          if (connection != null) {
50              try {
51                  connection.close();
52              } catch (SQLException sqle) {
53                  // static utility classes should not log without an explicit repository
54                  // reference
55              }
56          }
57      }
58  
59      public static void closeStatement(Statement statement) {
60          if (statement != null) {
61              try {
62                  statement.close();
63              } catch (SQLException sqle) {
64              }
65          }
66      }
67  }