001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025:
026: package org.nemesis.forum.util.jdbc;
027:
028: import java.sql.Connection;
029: import java.sql.SQLException;
030: import java.util.Properties;
031:
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.sql.DataSource;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.nemesis.forum.config.ConfigLoader;
039:
040: /**
041: * An implementation of DbConnectionProvider that utilizes a JDBC 2.0 DataSource
042: * made available via JNDI. This is useful for application servers where a pooled
043: * data connection is already provided so can share the pool with the
044: * other applications.<p>
045: *
046: * The JNDI location of the DataSource is retrieved from the
047: * {@link com.Yasna.forum.PropertyManager} as the
048: * <code>JNDIDataSource.name</code> property. This can be overiden by setting
049: * the provider's <code>name</code> property if required.
050: *
051: * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
052: *
053: * @see com.Yasna.forum.database.DbConnectionProvider
054: */
055: public class DataSourceConnectionProvider extends DbConnectionProvider {
056:
057: static protected Log log = LogFactory
058: .getLog(DataSourceConnectionProvider.class);
059:
060: private static final boolean POOLED = true;
061:
062: private DataSource dataSource;
063:
064: /**
065: * Keys of JNDI properties to query PropertyManager for.
066: */
067: /*private static final String[] jndiPropertyKeys = {
068: Context.APPLET ,
069: Context.AUTHORITATIVE ,
070: Context.BATCHSIZE ,
071: Context.DNS_URL ,
072: Context.INITIAL_CONTEXT_FACTORY ,
073: Context.LANGUAGE ,
074: Context.OBJECT_FACTORIES ,
075: Context.PROVIDER_URL ,
076: Context.REFERRAL ,
077: Context.SECURITY_AUTHENTICATION ,
078: Context.SECURITY_CREDENTIALS ,
079: Context.SECURITY_PRINCIPAL ,
080: Context.SECURITY_PROTOCOL ,
081: Context.STATE_FACTORIES ,
082: Context.URL_PKG_PREFIXES
083: };
084: */
085: /**
086: * Initialize.
087: */
088: public DataSourceConnectionProvider() {
089:
090: //properties = new Properties();
091: //properties.setProperty("name",Config.DB_JNDI_DATASOURCE);
092:
093: }
094:
095: /**
096: * Lookup DataSource from JNDI context.
097: */
098: protected void start() {
099: //java:comp/env/
100: String name = ConfigLoader.getInstance().getConfig()
101: .getJDBCProviderProperties().getProperty(
102: "JNDIDataSource");
103: if (name == null || name.length() == 0) {
104: log
105: .error("No name specified for DataSource JNDI lookup - 'name' "
106: + "Property should be set.");
107: return;
108: }
109: try {
110: Properties contextProperties = new Properties();
111: contextProperties.setProperty("name", name);
112:
113: /*for (int i=0; i<jndiPropertyKeys.length; i++) {
114: String k = jndiPropertyKeys[i];
115: String v = PropertyManager.getProperty(k);
116: if (v != null) {
117: contextProperties.setProperty(k,v);
118: }
119: }*/
120: Context context = new InitialContext(contextProperties);
121: dataSource = (DataSource) context.lookup(name);
122:
123: //Context initCtx = new InitialContext();
124: //Context envCtx = (Context) initCtx.lookup("java:comp/env");
125: //dataSource = (DataSource) envCtx.lookup("jdbc/MyDataSource");
126:
127: } catch (Exception e) {
128: log
129: .error("Could not lookup DataSource at '" + name
130: + "'"/*,e*/);
131: }
132: }
133:
134: /**
135: * Destroy then start.
136: */
137: protected void restart() {
138:
139: destroy();
140: start();
141: }
142:
143: /**
144: * Save properties.
145: */
146: protected void destroy() {
147:
148: return;
149: }
150:
151: /**
152: * Get new Connection from DataSource.
153: */
154: public Connection getConnection() {
155:
156: if (dataSource == null) {
157: log.error("DataSource has not yet been looked up", null);
158: return null;
159: }
160: try {
161: return dataSource.getConnection();
162: } catch (SQLException e) {
163: log.error("Could not retrieve Connection from DataSource",
164: e);
165: return null;
166: }
167: }
168:
169: }
|