001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/datastore/ConnectionInfo.java,v 1.8 2003/09/26 20:23:54 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.datastore;
021:
022: import java.util.Properties;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.jdo.JDOFatalUserException;
027:
028: import javax.naming.InitialContext;
029:
030: import org.xorm.Configurable;
031: import org.xorm.InterfaceManagerFactory;
032:
033: /**
034: * This is the base class for connection information. A ConnectionInfo
035: * object is used to acquire a DatastoreDriver instance. The attributes
036: * of this class may or may not be useful to subclasses, but they reflect
037: * the configuration detail that is standard in JDO.
038: */
039: public abstract class ConnectionInfo implements Configurable {
040: protected String connectionUserName;
041: protected String connectionPassword;
042: protected String connectionURL;
043: protected String connectionDriverName;
044: protected String connectionFactoryName;
045: protected String connectionFactory2Name;
046:
047: protected Object connectionFactory;
048: protected Object connectionFactory2;
049:
050: protected int minPool;
051: protected int maxPool;
052: protected int msWait;
053:
054: protected Properties properties;
055: protected InterfaceManagerFactory factory;
056:
057: public void setFactory(InterfaceManagerFactory factory) {
058: this .factory = factory;
059: }
060:
061: public Properties getProperties() {
062: return properties;
063: }
064:
065: public void setProperties(Properties properties) {
066: this .properties = properties;
067:
068: // iterate through properties
069: Iterator i = properties.entrySet().iterator();
070: while (i.hasNext()) {
071: Map.Entry entry = (Map.Entry) i.next();
072: String key = (String) entry.getKey();
073: String value = (String) entry.getValue();
074: if ("javax.jdo.option.ConnectionURL".equals(key)) {
075: setConnectionURL(value);
076: } else if ("javax.jdo.option.ConnectionUserName"
077: .equals(key)) {
078: setConnectionUserName(value);
079: } else if ("javax.jdo.option.ConnectionPassword"
080: .equals(key)) {
081: setConnectionPassword(value);
082: } else if ("javax.jdo.option.ConnectionDriverName"
083: .equals(key)) {
084: setConnectionDriverName(value);
085: } else if ("javax.jdo.option.ConnectionFactoryName"
086: .equals(key)) {
087: setConnectionFactoryName(value);
088: } else if ("javax.jdo.option.ConnectionFactory2Name"
089: .equals(key)) {
090: setConnectionFactory2Name(value);
091: } else if ("javax.jdo.option.MinPool".equals(key)) {
092: setMinPool(Integer.parseInt(value));
093: } else if ("javax.jdo.option.MaxPool".equals(key)) {
094: setMaxPool(Integer.parseInt(value));
095: }
096: }
097: }
098:
099: public void setConnectionUserName(String s) {
100: connectionUserName = s;
101: }
102:
103: public String getConnectionUserName() {
104: return connectionUserName;
105: }
106:
107: public void setConnectionPassword(String s) {
108: connectionPassword = s;
109: }
110:
111: public void setConnectionURL(String s) {
112: connectionURL = s;
113: }
114:
115: public String getConnectionURL() {
116: return connectionURL;
117: }
118:
119: public void setConnectionDriverName(String s) {
120: connectionDriverName = s;
121: }
122:
123: public String getConnectionDriverName() {
124: return connectionDriverName;
125: }
126:
127: public int getMaxPool() {
128: return maxPool;
129: }
130:
131: public void setMaxPool(int i) {
132: maxPool = i;
133: }
134:
135: public int getMinPool() {
136: return minPool;
137: }
138:
139: public void setMinPool(int i) {
140: minPool = i;
141: }
142:
143: public int getMsWait() {
144: return msWait;
145: }
146:
147: public void setMsWait(int i) {
148: msWait = i;
149: }
150:
151: public void setConnectionFactoryName(String s) {
152: connectionFactoryName = s;
153: connectionFactory = resolveJNDI(s);
154: }
155:
156: public String getConnectionFactoryName() {
157: return connectionFactoryName;
158: }
159:
160: public void setConnectionFactory(Object o) {
161: connectionFactory = o;
162: }
163:
164: public Object getConnectionFactory() {
165: return connectionFactory;
166: }
167:
168: public void setConnectionFactory2Name(String s) {
169: connectionFactory2Name = s;
170: connectionFactory2 = resolveJNDI(s);
171: }
172:
173: public String getConnectionFactory2Name() {
174: return connectionFactory2Name;
175: }
176:
177: public void setConnectionFactory2(Object o) {
178: connectionFactory2 = o;
179: }
180:
181: public Object getConnectionFactory2() {
182: return connectionFactory2;
183: }
184:
185: public static Object resolveJNDI(String name) {
186: try {
187: InitialContext initCtx = new InitialContext();
188: return initCtx.lookup(name);
189: } catch (Exception e) {
190: throw new JDOFatalUserException("Cannot resolve JNDI name "
191: + name, e);
192: }
193: }
194:
195: /**
196: * Returns a new DatastoreDriver that interacts with the datastore
197: * described by this ConnectionInfo object. This method must
198: * be implemented by subclasses.
199: */
200: public abstract DatastoreDriver getDriver();
201:
202: public Table describeTable(String tableName) {
203: throw new JDOFatalUserException(
204: "No driver support for runtime metadata");
205: }
206:
207: /** Release resources. Default implementation of this does nothing. */
208: public void close() {
209: }
210: }
|