KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > core > CoreDataSource


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22  package org.enhydra.jdbc.core;
23
24 import org.enhydra.jdbc.util.Logger;
25 import org.enhydra.jdbc.util.JdbcUtil;
26
27 import java.io.Serializable JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.Referenceable JavaDoc;
35 import javax.naming.StringRefAddr JavaDoc;
36 import javax.naming.spi.ObjectFactory JavaDoc;
37
38 /**
39  * Provides a Data Source which can be used to generate JDBC connections.
40  * <P>
41  * This class is generic in the sense that it does not rely upon anything other
42  * than standard Java APIs. It uses java.sql.DriverManager and preconfigured
43  * properties to construct a JDBC connection.
44  */

45 public class CoreDataSource
46     extends JdbcUtil
47     implements Referenceable JavaDoc, ObjectFactory JavaDoc, Serializable JavaDoc {
48
49     // Standard Data Source properties
50
private int loginTimeout; // timeout for database logins
51
transient public PrintWriter JavaDoc logWriter; // the log writer
52
public String JavaDoc user; // user for the database
53
public String JavaDoc password; // password for the database
54
private String JavaDoc description; // description of the datasource
55
private boolean debug; // debug flag
56
private boolean verbose; // verbose flag
57
private JdbcThreadFactory threadFactory; // thread factory
58

59     /**
60      * Constructor
61      */

62     public CoreDataSource() {
63         loginTimeout = 60; // Default value for loginTimeout
64
logWriter = null;
65         user = null;
66         password = null;
67         description = null;
68         debug = false;
69         verbose = false;
70         threadFactory = null;
71     }
72
73     /**
74      * Setter/Getter defined for standard properties
75      */

76     public String JavaDoc getDescription() {
77         return description;
78     }
79     public String JavaDoc getPassword() {
80         return password;
81     }
82     public String JavaDoc getUser() {
83         return user;
84     }
85     public JdbcThreadFactory getThreadFactory() {
86         return threadFactory;
87     }
88     public boolean isDebug() {
89         return debug;
90     }
91     public boolean isVerbose() {
92         return verbose;
93     }
94
95     public void setDescription(String JavaDoc description) {
96         this.description = description;
97     }
98     public void setPassword(String JavaDoc password) {
99         this.password = password;
100     }
101     public void setUser(String JavaDoc user) {
102         this.user = user;
103     }
104     public void setDebug(boolean debug) {
105         this.debug = debug;
106     }
107     public void setVerbose(boolean verbose) {
108         this.verbose = verbose;
109     }
110     public void setThreadFactory(JdbcThreadFactory f) {
111         this.threadFactory = f;
112     }
113
114     public PrintWriter JavaDoc getLogWriter() {
115         return log;
116     }
117
118     public void setLogWriter(PrintWriter JavaDoc out) {
119         log = (Logger) out;
120     }
121
122     /**
123      * shutdown is a placeholder for datasources which should shut down
124      * any pools which they maintain.
125      */

126     public void shutdown(boolean force) {
127     }
128
129     public void setLoginTimeout(int seconds) {
130         loginTimeout = seconds;
131     }
132
133     public int getLoginTimeout() {
134         return loginTimeout;
135     }
136
137     /**
138      * Methods inherited from referenceable
139      */

140     public Reference JavaDoc getReference() throws NamingException JavaDoc {
141         // Note that we use getClass().getName() to provide the factory
142
// class name. It is assumed that this class, and all of its
143
// descendants are their own factories.
144

145         Reference JavaDoc ref =
146             new Reference JavaDoc(getClass().getName(), getClass().getName(), null);
147         ref.add(new StringRefAddr JavaDoc("user", getUser()));
148         ref.add(new StringRefAddr JavaDoc("password", getPassword()));
149         ref.add(new StringRefAddr JavaDoc("description", getDescription()));
150         ref.add(
151             new StringRefAddr JavaDoc(
152                 "loginTimeout",
153                 Integer.toString(getLoginTimeout())));
154         log.debug("CoreDataSource:getReference object returned");
155         return ref;
156     }
157
158     /**
159      * Methods inherited from ObjectFactory
160      */

161     public Object JavaDoc getObjectInstance(
162         Object JavaDoc refObj,
163         Name JavaDoc name,
164         Context JavaDoc nameCtx,
165         Hashtable JavaDoc env)
166         throws Exception JavaDoc {
167         Reference JavaDoc ref = (Reference JavaDoc) refObj;
168
169         this.setUser((String JavaDoc) ref.get("user").getContent());
170         this.setPassword((String JavaDoc) ref.get("password").getContent());
171         this.setDescription((String JavaDoc) ref.get("description").getContent());
172         this.setLoginTimeout(
173             Integer.parseInt((String JavaDoc) ref.get("loginTimeout").getContent()));
174         log.debug("CoreDataSource:getObjectInstance instance created");
175         return this;
176     }
177     
178     public String JavaDoc toString() {
179         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
180         sb.append("CoreDataSource :\n");
181         sb.append(" debug =<"+this.debug+">\n");
182         sb.append(" description =<"+this.description+">\n");
183         sb.append(" login time out =<"+this.loginTimeout+">\n");
184         sb.append(" user =<"+this.user+">\n");
185         sb.append(" verbose =<"+this.verbose+">\n");
186         
187         return sb.toString();
188     }
189 }
190
Popular Tags