View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.db;
15  
16  import java.sql.Connection;
17  import java.sql.SQLException;
18  
19  import javax.naming.Context;
20  import javax.naming.InitialContext;
21  import javax.naming.NamingException;
22  
23  // PortableRemoteObject was introduced in JDK 1.3. We won't use it.
24  // import javax.rmi.PortableRemoteObject;
25  import javax.sql.DataSource;
26  
27  /**
28   * The <id>JNDIConnectionSource</id> is an implementation of
29   * {@link ConnectionSource} that obtains a {@link javax.sql.DataSource} from a
30   * JNDI provider and uses it to obtain a {@link java.sql.Connection}. It is
31   * primarily designed to be used inside of J2EE application servers or
32   * application server clients, assuming the application server supports remote
33   * access of {@link javax.sql.DataSource}s. In this way one can take advantage
34   * of connection pooling and whatever other goodies the application server
35   * provides.
36   * <p>
37   * For more information about this component, please refer to the online manual at
38   * http://logback.qos.ch/manual/appenders.html#DBAppender
39   * 
40   * @author <a href="mailto:rdecampo@twcny.rr.com">Ray DeCampo</a>
41   */
42  public class JNDIConnectionSource extends ConnectionSourceBase {
43      private String jndiLocation = null;
44      private DataSource dataSource = null;
45  
46      public void start() {
47          if (jndiLocation == null) {
48              addError("No JNDI location specified for JNDIConnectionSource.");
49          }
50          discoverConnectionProperties();
51      }
52  
53      public Connection getConnection() throws SQLException {
54          Connection conn = null;
55          try {
56              if (dataSource == null) {
57                  dataSource = lookupDataSource();
58              }
59              if (getUser() != null) {
60                  addWarn("Ignoring property [user] with value [" + getUser() + "] for obtaining a connection from a DataSource.");
61              }
62              conn = dataSource.getConnection();
63          } catch (final NamingException ne) {
64              addError("Error while getting data source", ne);
65              throw new SQLException("NamingException while looking up DataSource: " + ne.getMessage());
66          } catch (final ClassCastException cce) {
67              addError("ClassCastException while looking up DataSource.", cce);
68              throw new SQLException("ClassCastException while looking up DataSource: " + cce.getMessage());
69          }
70  
71          return conn;
72      }
73  
74      /**
75       * Returns the jndiLocation.
76       * 
77       * @return String
78       */
79      public String getJndiLocation() {
80          return jndiLocation;
81      }
82  
83      /**
84       * Sets the jndiLocation.
85       * 
86       * @param jndiLocation
87       *          The jndiLocation to set
88       */
89      public void setJndiLocation(String jndiLocation) {
90          this.jndiLocation = jndiLocation;
91      }
92  
93      private DataSource lookupDataSource() throws NamingException, SQLException {
94          addInfo("Looking up [" + jndiLocation + "] in JNDI");
95          DataSource ds;
96          Context initialContext = new InitialContext();
97          Object obj = initialContext.lookup(jndiLocation);
98  
99          // PortableRemoteObject was introduced in JDK 1.3. We won't use it.
100         // ds = (DataSource)PortableRemoteObject.narrow(obj, DataSource.class);
101         ds = (DataSource) obj;
102 
103         if (ds == null) {
104             throw new SQLException("Failed to obtain data source from JNDI location " + jndiLocation);
105         } else {
106             return ds;
107         }
108     }
109 }