001: /**
002: * $Revision: 2949 $
003: * $Date: 2005-10-10 00:53:06 -0700 (Mon, 10 Oct 2005) $
004: *
005: * Copyright (C) 2004-2005 Jive Software. All rights reserved.
006: *
007: * This software is published under the terms of the GNU Public License (GPL),
008: * a copy of which is included in this distribution.
009: */package org.jivesoftware.database;
010:
011: import org.jivesoftware.util.JiveGlobals;
012: import org.jivesoftware.util.Log;
013:
014: import javax.naming.Context;
015: import javax.naming.InitialContext;
016: import javax.sql.DataSource;
017: import java.sql.Connection;
018: import java.sql.SQLException;
019: import java.util.*;
020:
021: /**
022: * An implementation of ConnectionProvider that utilizes a JDBC 2.0 DataSource
023: * made available via JNDI. This is useful for application servers where a pooled
024: * data connection is already provided so Jive can share the pool with the
025: * other applications.<p>
026: * <p/>
027: * The JNDI location of the DataSource stored as the Jive property
028: * <code>database.JNDIProvider.name</code>. This can be overridden by setting
029: * the provider's <code>name</code> property if required.
030: *
031: * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
032: * @see ConnectionProvider
033: */
034: public class JNDIDataSourceProvider implements ConnectionProvider {
035:
036: private String dataSourceName;
037: private DataSource dataSource;
038:
039: /**
040: * Keys of JNDI properties to query PropertyManager for.
041: */
042: private static final String[] jndiPropertyKeys = { Context.APPLET,
043: Context.AUTHORITATIVE, Context.BATCHSIZE, Context.DNS_URL,
044: Context.INITIAL_CONTEXT_FACTORY, Context.LANGUAGE,
045: Context.OBJECT_FACTORIES, Context.PROVIDER_URL,
046: Context.REFERRAL, Context.SECURITY_AUTHENTICATION,
047: Context.SECURITY_CREDENTIALS, Context.SECURITY_PRINCIPAL,
048: Context.SECURITY_PROTOCOL, Context.STATE_FACTORIES,
049: Context.URL_PKG_PREFIXES };
050:
051: /**
052: * Constructs a new JNDI pool.
053: */
054: public JNDIDataSourceProvider() {
055: dataSourceName = JiveGlobals
056: .getXMLProperty("database.JNDIProvider.name");
057: }
058:
059: public boolean isPooled() {
060: return true;
061: }
062:
063: public void start() {
064: if (dataSourceName == null || dataSourceName.equals("")) {
065: Log
066: .error(
067: "No name specified for DataSource. JNDI lookup will fail",
068: null);
069: return;
070: }
071: try {
072: Properties contextProperties = new Properties();
073: for (String key : jndiPropertyKeys) {
074: String value = JiveGlobals.getXMLProperty(key);
075: if (value != null) {
076: contextProperties.setProperty(key, value);
077: }
078: }
079: Context context;
080: if (contextProperties.size() > 0) {
081: context = new InitialContext(contextProperties);
082: } else {
083: context = new InitialContext();
084: }
085: dataSource = (DataSource) context.lookup(dataSourceName);
086: } catch (Exception e) {
087: Log.error("Could not lookup DataSource at '"
088: + dataSourceName + "'", e);
089: }
090: }
091:
092: public void restart() {
093: destroy();
094: start();
095: }
096:
097: public void destroy() {
098:
099: }
100:
101: public Connection getConnection() {
102: if (dataSource == null) {
103: Log.error("DataSource has not been initialized.", null);
104: return null;
105: }
106: try {
107: return dataSource.getConnection();
108: } catch (SQLException e) {
109: Log.error("Could not retrieve Connection from DataSource",
110: e);
111: return null;
112: }
113: }
114: }
|