001: package org.apache.ojb.broker.metadata;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.Serializable;
019:
020: import javax.sql.DataSource;
021:
022: import org.apache.ojb.broker.util.logging.Logger;
023: import org.apache.ojb.broker.util.logging.LoggerFactory;
024: import org.apache.ojb.broker.PBKey;
025: import org.apache.commons.lang.builder.ToStringBuilder;
026: import org.apache.commons.lang.builder.ToStringStyle;
027: import org.apache.commons.lang.SystemUtils;
028:
029: /**
030: * JdbcConnectionDescriptor describes all relevant parameters of
031: * JDBC Connections used by the PersistenceBroker.
032: *
033: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
034: * @version $Id: JdbcConnectionDescriptor.java,v 1.31.2.4 2005/07/24 23:32:43 arminw Exp $
035: */
036: public class JdbcConnectionDescriptor extends DescriptorBase implements
037: Serializable, XmlCapable {
038: private static final long serialVersionUID = -600900924512028960L;
039: private Logger logger = LoggerFactory
040: .getLogger(JdbcConnectionDescriptor.class);
041:
042: public static final int AUTO_COMMIT_IGNORE_STATE = 0;
043: public static final int AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE = 1;
044: public static final int AUTO_COMMIT_SET_FALSE = 2;
045:
046: private String m_jcdAlias;
047: private String m_Dbms;
048: private String m_Driver;
049: private String m_Protocol;
050: private String m_SubProtocol;
051: private String m_DbAlias;
052: private String m_DatasourceName;
053: private String m_UserName;
054: private String m_Password;
055: private double m_JdbcLevel = 2.0;
056: private boolean m_eagerRelease = false;
057: private boolean m_batchMode = false;
058: private boolean defaultConnection = false;
059: private int useAutoCommit = AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE;
060: private boolean ignoreAutoCommitExceptions = false;
061: private PBKey pbKey;
062: private ConnectionPoolDescriptor cpd;
063: private SequenceDescriptor sequenceDescriptor;
064: private ObjectCacheDescriptor objectCacheDescriptor;
065: private transient DataSource dataSource;
066:
067: /**
068: * Constructor declaration
069: */
070: public JdbcConnectionDescriptor() {
071: cpd = new ConnectionPoolDescriptor();
072: objectCacheDescriptor = new ObjectCacheDescriptor();
073: }
074:
075: /**
076: * Returns the appropriate {@link ObjectCacheDescriptor}
077: * or <code>null</code> if not specified.
078: */
079: public ObjectCacheDescriptor getObjectCacheDescriptor() {
080: return objectCacheDescriptor;
081: }
082:
083: /**
084: * Sets the {@link ObjectCacheDescriptor} for representing connection/database.
085: */
086: public void setObjectCacheDescriptor(
087: ObjectCacheDescriptor objectCacheDescriptor) {
088: this .objectCacheDescriptor = objectCacheDescriptor;
089: }
090:
091: /**
092: * Returns the data source that this connection descriptor represents if any.
093: *
094: * @return The data source or <code>null</code>
095: */
096: public DataSource getDataSource() {
097: return dataSource;
098: }
099:
100: /**
101: * Sets the data source that this connection descriptor represents.
102: *
103: * @param dataSource The data source
104: */
105: public void setDataSource(DataSource dataSource) {
106: this .dataSource = dataSource;
107: }
108:
109: /**
110: * Get the alias name for this descriptor.
111: */
112: public String getJcdAlias() {
113: return m_jcdAlias;
114: }
115:
116: /**
117: * Set an alias name for this descriptor.
118: */
119: public void setJcdAlias(String jcdAlias) {
120: this .clearPBKey();
121: this .m_jcdAlias = jcdAlias;
122: }
123:
124: /**
125: *
126: */
127: public boolean isDefaultConnection() {
128: return defaultConnection;
129: }
130:
131: public boolean isDataSource() {
132: return (getDataSource() != null)
133: || (getDatasourceName() != null);
134: }
135:
136: /**
137: *
138: */
139: public void setDefaultConnection(boolean defaultConnection) {
140: this .defaultConnection = defaultConnection;
141: }
142:
143: /**
144: * Return the associated <code>SequenceDescriptor</code>
145: * or <code>null</code> if not set.
146: */
147: public SequenceDescriptor getSequenceDescriptor() {
148: return sequenceDescriptor;
149: }
150:
151: /**
152: * Set the <code>SequenceDescriptor</code> for this
153: * connection descriptor.
154: */
155: public void setSequenceDescriptor(
156: SequenceDescriptor sequenceDescriptor) {
157: this .sequenceDescriptor = sequenceDescriptor;
158: }
159:
160: /**
161: * Returns the connection pool descriptor.
162: */
163: public ConnectionPoolDescriptor getConnectionPoolDescriptor() {
164: return cpd;
165: }
166:
167: /**
168: * Sets the connection pool descriptor.
169: */
170: public void setConnectionPoolDescriptor(ConnectionPoolDescriptor cpd) {
171: this .cpd = cpd;
172: }
173:
174: /**
175: * Return a key to identify the connection descriptor.
176: */
177: public PBKey getPBKey() {
178: if (pbKey == null) {
179: this .pbKey = new PBKey(this .getJcdAlias(), this
180: .getUserName(), this .getPassWord());
181: }
182: return pbKey;
183: }
184:
185: private void clearPBKey() {
186: this .pbKey = null;
187: }
188:
189: public int getUseAutoCommit() {
190: return useAutoCommit;
191: }
192:
193: public void setUseAutoCommit(int useAutoCommit) {
194: this .useAutoCommit = useAutoCommit;
195: }
196:
197: public boolean isIgnoreAutoCommitExceptions() {
198: return ignoreAutoCommitExceptions;
199: }
200:
201: public void setIgnoreAutoCommitExceptions(
202: boolean ignoreAutoCommitExceptions) {
203: this .ignoreAutoCommitExceptions = ignoreAutoCommitExceptions;
204: }
205:
206: /**
207: * Returns the database platform name.
208: */
209: public String getDbms() {
210: return m_Dbms;
211: }
212:
213: /**
214: * Sets the database platform name.
215: */
216: public void setDbms(String str) {
217: m_Dbms = str;
218: }
219:
220: /**
221: * Returns the driver name.
222: */
223: public String getDriver() {
224: return m_Driver;
225: }
226:
227: /**
228: * Set the database driver.
229: */
230: public void setDriver(String str) {
231: m_Driver = str;
232: }
233:
234: /**
235: * Returns the database protocol.
236: */
237: public String getProtocol() {
238: return m_Protocol;
239: }
240:
241: /**
242: * Sets the database protocol.
243: */
244: public void setProtocol(String str) {
245: m_Protocol = str;
246: }
247:
248: /**
249: * Returns the database sub-protocol.
250: */
251: public String getSubProtocol() {
252: return m_SubProtocol;
253: }
254:
255: /**
256: * Sets the database sub-protocol.
257: */
258: public void setSubProtocol(String str) {
259: m_SubProtocol = str;
260: }
261:
262: /**
263: * Returns the database alias name
264: * used by OJB.
265: */
266: public String getDbAlias() {
267: return m_DbAlias;
268: }
269:
270: /**
271: * Sets the database alias name. These
272: * names you could find in the repository.dtd.
273: */
274: public void setDbAlias(String str) {
275: m_DbAlias = str;
276: }
277:
278: /**
279: * Returns the database user name.
280: */
281: public String getUserName() {
282: return m_UserName;
283: }
284:
285: /**
286: * Sets the database user name.
287: */
288: public void setUserName(String str) {
289: this .clearPBKey();
290: m_UserName = str;
291: }
292:
293: /**
294: * Returns the database password.
295: */
296: public String getPassWord() {
297: return m_Password;
298: }
299:
300: /**
301: * Sets the database password.
302: */
303: public void setPassWord(String str) {
304: this .clearPBKey();
305: m_Password = str;
306: }
307:
308: /**
309: * Gets the datasourceName.
310: * @return Returns a String
311: */
312: public String getDatasourceName() {
313: return m_DatasourceName;
314: }
315:
316: /**
317: * Sets the datasourceName.
318: * @param datasourceName The datasourceName to set
319: */
320: public void setDatasourceName(String datasourceName) {
321: m_DatasourceName = datasourceName;
322: }
323:
324: /**
325: * Gets the jdbcLevel.
326: * @return Returns a String
327: */
328: public double getJdbcLevel() {
329: return m_JdbcLevel;
330: }
331:
332: /**
333: * Sets the jdbcLevel. parse the string setting and check that it is indeed an integer.
334: * @param jdbcLevel The jdbcLevel to set
335: */
336: public void setJdbcLevel(String jdbcLevel) {
337: if (jdbcLevel != null) {
338: try {
339: double intLevel = Double.parseDouble(jdbcLevel);
340: setJdbcLevel(intLevel);
341: } catch (NumberFormatException nfe) {
342: setJdbcLevel(2.0);
343: logger
344: .info("Specified JDBC level was not numeric (Value="
345: + jdbcLevel
346: + "), used default jdbc level of 2.0 ");
347: }
348: } else {
349: setJdbcLevel(2.0);
350: logger
351: .info("Specified JDBC level was null, used default jdbc level of 2.0 ");
352: }
353: }
354:
355: public void setJdbcLevel(double jdbcLevel) {
356: m_JdbcLevel = jdbcLevel;
357: }
358:
359: public boolean getEagerRelease() {
360: return m_eagerRelease;
361: }
362:
363: public void setEagerRelease(boolean flag) {
364: m_eagerRelease = flag;
365: }
366:
367: public boolean getBatchMode() {
368: return m_batchMode;
369: }
370:
371: public void setBatchMode(boolean flag) {
372: m_batchMode = flag;
373: }
374:
375: /**
376: * Returns a String representation of this class.
377: */
378: public String toString() {
379: ToStringBuilder buf = new ToStringBuilder(this ,
380: ToStringStyle.MULTI_LINE_STYLE);
381: buf.append("jcd-alias", m_jcdAlias).append(
382: "default-connection", defaultConnection).append("dbms",
383: m_Dbms).append("jdbc-level", m_JdbcLevel).append(
384: "driver", m_Driver).append("protocol", m_Protocol)
385: .append("sub-protocol", m_SubProtocol).append(
386: "db-alias", m_DbAlias).append("user",
387: m_UserName).append("password", "*****").append(
388: "eager-release", m_eagerRelease).append(
389: "ConnectionPoolDescriptor", cpd).append(
390: "batchMode", m_batchMode).append(
391: "useAutoCommit",
392: getUseAutoCommitAsString(useAutoCommit))
393: .append("ignoreAutoCommitExceptions",
394: ignoreAutoCommitExceptions).append(
395: "sequenceDescriptor", sequenceDescriptor);
396: return buf.toString();
397: }
398:
399: /*
400: * @see XmlCapable#toXML()
401: */
402: public String toXML() {
403: RepositoryTags tags = RepositoryTags.getInstance();
404: String eol = SystemUtils.LINE_SEPARATOR;
405:
406: StringBuffer strReturn = new StringBuffer(1024);
407: strReturn.append(eol);
408: strReturn.append(" <!-- Descriptor for Connection ");
409: strReturn.append(getProtocol());
410: strReturn.append(":");
411: strReturn.append(getSubProtocol());
412: strReturn.append(":");
413: strReturn.append(getDbAlias());
414: strReturn.append(" -->");
415: strReturn.append(eol);
416:
417: strReturn.append(" ");
418: strReturn
419: .append(tags
420: .getOpeningTagNonClosingById(JDBC_CONNECTION_DESCRIPTOR));
421: strReturn.append(eol);
422: strReturn.append(" ");
423: strReturn.append(tags.getAttribute(JCD_ALIAS, this
424: .getJcdAlias()));
425: strReturn.append(eol);
426: strReturn.append(" ");
427: strReturn.append(tags.getAttribute(DEFAULT_CONNECTION, ""
428: + this .isDefaultConnection()));
429: strReturn.append(eol);
430: strReturn.append(" ");
431: strReturn.append(tags.getAttribute(DBMS_NAME, this .getDbms()));
432: strReturn.append(eol);
433: strReturn.append(" ");
434: strReturn.append(tags.getAttribute(JDBC_LEVEL, ""
435: + this .getJdbcLevel()));
436: strReturn.append(eol);
437:
438: //username is optional
439: String user = getUserName();
440: if (user != null) {
441: strReturn.append(" ");
442: strReturn.append(tags.getAttribute(USER_NAME, user));
443: strReturn.append(eol);
444: }
445: // password is optional
446: String passwd = getPassWord();
447: if (passwd != null) {
448: strReturn.append(" ");
449: strReturn.append(tags.getAttribute(USER_PASSWD, passwd));
450: strReturn.append(eol);
451: }
452:
453: // JDBC Datasource or DriverManager information are alternatives:
454: String dsn = getDatasourceName();
455: if (dsn != null) {
456: strReturn.append(" ");
457: strReturn.append(tags.getAttribute(DATASOURCE_NAME, this
458: .getDatasourceName()));
459: strReturn.append(eol);
460: } else {
461: strReturn.append(" ");
462: strReturn.append(tags.getAttribute(DRIVER_NAME, this
463: .getDriver()));
464: strReturn.append(eol);
465: strReturn.append(" ");
466: strReturn.append(tags.getAttribute(URL_PROTOCOL, this
467: .getProtocol()));
468: strReturn.append(eol);
469: strReturn.append(" ");
470: strReturn.append(tags.getAttribute(URL_SUBPROTOCOL, this
471: .getSubProtocol()));
472: strReturn.append(eol);
473: strReturn.append(" ");
474: strReturn.append(encode(tags.getAttribute(URL_DBALIAS, this
475: .getDbAlias())));
476: strReturn.append(eol);
477: }
478: strReturn.append(" ");
479: strReturn.append(tags.getAttribute(EAGER_RELEASE, ""
480: + this .getEagerRelease()));
481: strReturn.append(eol);
482: strReturn.append(" ");
483: strReturn.append(tags.getAttribute(BATCH_MODE, ""
484: + this .getBatchMode()));
485: strReturn.append(eol);
486: strReturn.append(" ");
487: strReturn.append(tags.getAttribute(USE_AUTOCOMMIT, ""
488: + this .getUseAutoCommit()));
489: strReturn.append(eol);
490: strReturn.append(" ");
491: strReturn.append(tags.getAttribute(IGNORE_AUTOCOMMIT_EXCEPTION,
492: "" + this .isIgnoreAutoCommitExceptions()));
493: strReturn.append(eol);
494:
495: strReturn.append(" >");
496: strReturn.append(eol);
497: strReturn.append(eol);
498:
499: strReturn.append(this .getConnectionPoolDescriptor().toXML());
500: strReturn.append(eol);
501: if (this .getSequenceDescriptor() != null) {
502: strReturn.append(this .getSequenceDescriptor().toXML());
503: }
504: strReturn.append(eol);
505: strReturn.append(" ");
506: strReturn.append(tags
507: .getClosingTagById(JDBC_CONNECTION_DESCRIPTOR));
508: strReturn.append(eol);
509: return strReturn.toString();
510: }
511:
512: private static String encode(String toBeEncoded) {
513: StringBuffer retval = new StringBuffer();
514: char c;
515: for (int i = 0; i < toBeEncoded.length(); i++) {
516: c = toBeEncoded.charAt(i);
517: if (c == '<') {
518: retval.append("<");
519: } else if (c == '>') {
520: retval.append(">");
521: }
522: //else if (c == '"')
523: //{
524: // retval.append(""");
525: //}
526: else if (c == '&') {
527: retval.append("&");
528: } else if (c == ' ') {
529: retval.append(" ");
530: } else {
531: retval.append(c);
532: }
533: }
534: return retval.toString();
535: }
536:
537: private static String getUseAutoCommitAsString(int state) {
538: switch (state) {
539: case AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE:
540: return "AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE";
541: case AUTO_COMMIT_SET_FALSE:
542: return "AUTO_COMMIT_SET_FALSE";
543: case AUTO_COMMIT_IGNORE_STATE:
544: return "AUTO_COMMIT_IGNORE_STATE";
545: default:
546: return "UNKOWN_STATE";
547: }
548: }
549: }
|