001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: */
022: package org.enhydra.jdbc.core;
023:
024: import org.enhydra.jdbc.util.Logger;
025: import org.enhydra.jdbc.util.JdbcUtil;
026:
027: import java.io.Serializable;
028: import java.io.PrintWriter;
029: import java.util.Hashtable;
030: import javax.naming.Context;
031: import javax.naming.Name;
032: import javax.naming.NamingException;
033: import javax.naming.Reference;
034: import javax.naming.Referenceable;
035: import javax.naming.StringRefAddr;
036: import javax.naming.spi.ObjectFactory;
037:
038: /**
039: * Provides a Data Source which can be used to generate JDBC connections.
040: * <P>
041: * This class is generic in the sense that it does not rely upon anything other
042: * than standard Java APIs. It uses java.sql.DriverManager and preconfigured
043: * properties to construct a JDBC connection.
044: */
045: public class CoreDataSource extends JdbcUtil implements Referenceable,
046: ObjectFactory, Serializable {
047:
048: // Standard Data Source properties
049: private int loginTimeout; // timeout for database logins
050: transient public PrintWriter logWriter; // the log writer
051: public String user; // user for the database
052: public String password; // password for the database
053: private String description; // description of the datasource
054: private boolean debug; // debug flag
055: private boolean verbose; // verbose flag
056: private JdbcThreadFactory threadFactory; // thread factory
057:
058: /**
059: * Constructor
060: */
061: public CoreDataSource() {
062: loginTimeout = 0; // Default value for loginTimeout
063: logWriter = null;
064: user = null;
065: password = null;
066: description = null;
067: debug = false;
068: verbose = false;
069: threadFactory = null;
070: }
071:
072: /**
073: * Setter/Getter defined for standard properties
074: */
075: public String getDescription() {
076: return description;
077: }
078:
079: public String getPassword() {
080: return password;
081: }
082:
083: public String getUser() {
084: return user;
085: }
086:
087: public JdbcThreadFactory getThreadFactory() {
088: return threadFactory;
089: }
090:
091: public boolean isDebug() {
092: return debug;
093: }
094:
095: public boolean isVerbose() {
096: return verbose;
097: }
098:
099: public void setDescription(String description) {
100: this .description = description;
101: }
102:
103: public void setPassword(String password) {
104: this .password = password;
105: }
106:
107: public void setUser(String user) {
108: this .user = user;
109: }
110:
111: public void setDebug(boolean debug) {
112: this .debug = debug;
113: }
114:
115: public void setVerbose(boolean verbose) {
116: this .verbose = verbose;
117: }
118:
119: public void setThreadFactory(JdbcThreadFactory f) {
120: this .threadFactory = f;
121: }
122:
123: public PrintWriter getLogWriter() {
124: return log;
125: }
126:
127: public void setLogWriter(PrintWriter out) {
128: log = (Logger) out;
129: }
130:
131: /**
132: * shutdown is a placeholder for datasources which should shut down
133: * any pools which they maintain.
134: */
135: public void shutdown(boolean force) {
136: }
137:
138: public void setLoginTimeout(int seconds) {
139: loginTimeout = seconds;
140: }
141:
142: public int getLoginTimeout() {
143: return loginTimeout;
144: }
145:
146: /**
147: * Methods inherited from referenceable
148: */
149: public Reference getReference() throws NamingException {
150: // Note that we use getClass().getName() to provide the factory
151: // class name. It is assumed that this class, and all of its
152: // descendants are their own factories.
153:
154: Reference ref = new Reference(getClass().getName(), getClass()
155: .getName(), null);
156: ref.add(new StringRefAddr("user", getUser()));
157: ref.add(new StringRefAddr("password", getPassword()));
158: ref.add(new StringRefAddr("description", getDescription()));
159: ref.add(new StringRefAddr("loginTimeout", Integer
160: .toString(getLoginTimeout())));
161: log.debug("CoreDataSource:getReference object returned");
162: return ref;
163: }
164:
165: /**
166: * Methods inherited from ObjectFactory
167: */
168: public Object getObjectInstance(Object refObj, Name name,
169: Context nameCtx, Hashtable env) throws Exception {
170: Reference ref = (Reference) refObj;
171:
172: this .setUser((String) ref.get("user").getContent());
173: this .setPassword((String) ref.get("password").getContent());
174: this .setDescription((String) ref.get("description")
175: .getContent());
176: this .setLoginTimeout(Integer.parseInt((String) ref.get(
177: "loginTimeout").getContent()));
178: log.debug("CoreDataSource:getObjectInstance instance created");
179: return this ;
180: }
181:
182: public String toString() {
183: StringBuffer sb = new StringBuffer();
184: sb.append("CoreDataSource :\n");
185: sb.append(" debug =<" + this .debug + ">\n");
186: sb.append(" description =<" + this .description + ">\n");
187: sb.append(" login time out =<" + this .loginTimeout + ">\n");
188: sb.append(" user =<" + this .user + ">\n");
189: sb.append(" verbose =<" + this .verbose + ">\n");
190:
191: return sb.toString();
192: }
193: }
|