001: /* tjws - SimpleDataSource.java
002: * Copyright (C) 1999-2007 Dmitriy Rogatkin. All rights reserved.
003: * Redistribution and use in source and binary forms, with or without
004: * modification, are permitted provided that the following conditions
005: * are met:
006: * 1. Redistributions of source code must retain the above copyright
007: * notice, this list of conditions and the following disclaimer.
008: * 2. Redistributions in binary form must reproduce the above copyright
009: * notice, this list of conditions and the following disclaimer in the
010: * documentation and/or other materials provided with the distribution.
011: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
012: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
013: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
015: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
016: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
017: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
018: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
019: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
020: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
021: * SUCH DAMAGE.
022: *
023: * Visit http://tjws.sourceforge.net to get the latest infromation
024: * about Rogatkin's products.
025: * $Id: SimpleDataSource.java,v 1.3 2007/04/25 04:43:16 rogatkin Exp $
026: * Created on Mar 25, 2007
027: * @author Dmitriy
028: */
029: package rogatkin.app;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileNotFoundException;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.PrintWriter;
037: import java.net.MalformedURLException;
038: import java.net.URL;
039: import java.sql.Connection;
040: import java.sql.DriverManager;
041: import java.sql.SQLException;
042: import java.util.Properties;
043:
044: import javax.naming.InitialContext;
045: import javax.naming.NamingException;
046: import javax.sql.DataSource;
047:
048: public class SimpleDataSource implements DataSource {
049: Properties connectionProperties;
050:
051: public SimpleDataSource(String definitionPropertiesLocation) {
052: InputStream propertiesStream = null;
053: File f = new File(definitionPropertiesLocation);
054: try {
055: if (f.exists())
056: propertiesStream = new FileInputStream(f);
057: else {
058: propertiesStream = new URL(definitionPropertiesLocation)
059: .openStream();
060: }
061: connectionProperties = new Properties();
062: if (definitionPropertiesLocation.toLowerCase().endsWith(
063: ".xml"))
064: connectionProperties.loadFromXML(propertiesStream);
065: else
066: connectionProperties.load(propertiesStream);
067: Class.forName(connectionProperties
068: .getProperty("driver-class"));
069: String jndiName = connectionProperties
070: .getProperty("jndi-name");
071: if (jndiName != null) {
072: if (jndiName.startsWith("jdbc/"))
073: jndiName = "java:comp/env/" + jndiName;
074: new InitialContext().bind(jndiName, this );
075: }
076: } catch (FileNotFoundException e) {
077: throw new IllegalArgumentException(e);
078: } catch (MalformedURLException e) {
079: throw new IllegalArgumentException(e);
080: } catch (IOException e) {
081: throw new IllegalArgumentException(e);
082: } catch (ClassNotFoundException e) {
083: throw new IllegalArgumentException(e);
084: } catch (NamingException e) {
085: throw new IllegalArgumentException(e);
086: } finally {
087: if (propertiesStream != null)
088: try {
089: propertiesStream.close();
090: } catch (IOException e) {
091: }
092: }
093: }
094:
095: public Connection getConnection() throws SQLException {
096: if (connectionProperties.getProperty("user") != null)
097: return DriverManager.getConnection(connectionProperties
098: .getProperty("url"), connectionProperties
099: .getProperty("user"), connectionProperties
100: .getProperty("password"));
101: return DriverManager.getConnection(connectionProperties
102: .getProperty("url"));
103: }
104:
105: public Connection getConnection(String arg0, String arg1)
106: throws SQLException {
107: return DriverManager.getConnection(connectionProperties
108: .getProperty("url"), arg0, arg1);
109: }
110:
111: public PrintWriter getLogWriter() throws SQLException {
112: // TODO Auto-generated method stub
113: return null;
114: }
115:
116: public int getLoginTimeout() throws SQLException {
117: // TODO Auto-generated method stub
118: return 0;
119: }
120:
121: public void setLogWriter(PrintWriter arg0) throws SQLException {
122: // TODO Auto-generated method stub
123:
124: }
125:
126: public void setLoginTimeout(int arg0) throws SQLException {
127: // TODO Auto-generated method stub
128:
129: }
130:
131: public boolean isWrapperFor(Class<?> arg0) throws SQLException {
132: // TODO Auto-generated method stub
133: return false;
134: }
135:
136: public <T> T unwrap(Class<T> arg0) throws SQLException {
137: // TODO Auto-generated method stub
138: return null;
139: }
140:
141: }
|