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: import java.util.Properties;
020:
021: import org.apache.commons.lang.SystemUtils;
022: import org.apache.ojb.broker.util.pooling.PoolConfiguration;
023: import org.apache.ojb.broker.util.XmlHelper;
024:
025: /**
026: * Encapsulates connection pooling and JDBC-driver configuration properties managed by
027: * {@link org.apache.ojb.broker.metadata.JdbcConnectionDescriptor}.
028: * <p>
029: * Every new instantiated <code>ConnectionPoolDescriptor</code> is associated with
030: * default connection pool attributes.
031: *
032: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
033: * @version $Id: ConnectionPoolDescriptor.java,v 1.14.2.2 2005/04/30 20:55:15 mkalen Exp $
034: */
035: public class ConnectionPoolDescriptor extends PoolConfiguration
036: implements Serializable, XmlCapable {
037: private static final long serialVersionUID = -3071461685659671879L;
038:
039: /** String prefix for JDBC properties passed to DriverManager. */
040: public static final String JDBC_PROPERTY_NAME_PREFIX = "jdbc.";
041: private static final int JDBC_PROPERTY_NAME_LENGTH = JDBC_PROPERTY_NAME_PREFIX
042: .length();
043: /**
044: * String prefix for DBCP properties.
045: * Currently OJB only uses this for setting DBCP parameters for pooling of Statement,
046: * not the max/test-parameters etc for the DBCP Connection pool
047: * (since there is only a JDBC2.0+ version of the Basic-classes ie BasicDataSource
048: * and no DriverManager-based one).
049: */
050: public static final String DBCP_PROPERTY_NAME_PREFIX = "dbcp.";
051: private static final int DBCP_PROPERTY_NAME_LENGTH = DBCP_PROPERTY_NAME_PREFIX
052: .length();
053:
054: /** JDBC properties configured in OJB (not used for DataSource connections). */
055: protected Properties jdbcProperties;
056: /** DBCP Statement cache properties configured in OJB (not used for DataSource connections). */
057: protected Properties dbcpProperties;
058:
059: /** Configuration attribute name for JDBC fetchSize hint. */
060: public static final String FETCH_SIZE = "fetchSize";
061:
062: private Class connectionFactory;
063:
064: public ConnectionPoolDescriptor() {
065: super ();
066: init();
067: }
068:
069: /**
070: * Set some initial values.
071: */
072: private void init() {
073: jdbcProperties = new Properties();
074: dbcpProperties = new Properties();
075: setFetchSize(0);
076: this .setTestOnBorrow(true);
077: this .setTestOnReturn(false);
078: this .setTestWhileIdle(false);
079: this .setLogAbandoned(false);
080: this .setRemoveAbandoned(false);
081: }
082:
083: public Class getConnectionFactory() {
084: return this .connectionFactory;
085: }
086:
087: public void setConnectionFactory(Class connectionFactory) {
088: if (connectionFactory == null)
089: throw new MetadataException(
090: "Given ConnectionFactory was null");
091: this .connectionFactory = connectionFactory;
092: }
093:
094: /**
095: * Returns the fetchSize hint set for this connection pool.
096: * @return fetchSize hint or 0 if JDBC-driver specific default is used
097: */
098: public int getFetchSize() {
099: // We depend on init() to always set fetchSize hint
100: return Integer.parseInt(getProperty(FETCH_SIZE));
101: }
102:
103: /**
104: * Sets the fetchSize hint for this connection pool.
105: * @param fetchSize fetchSize hint or 0 to use JDBC-driver specific default
106: */
107: public void setFetchSize(int fetchSize) {
108: setProperty(FETCH_SIZE, Integer.toString(fetchSize));
109: }
110:
111: /**
112: * Returns the JDBC properties to be used by the ConnectionFactory
113: * when creating connections from DriverManager.
114: * @return JDBC-driver specific properties (might be empty, never null)
115: */
116: public Properties getJdbcProperties() {
117: return jdbcProperties;
118: }
119:
120: /**
121: * Returns the DBCP properties to be used for Statement caching
122: * when creating DBCP connection pool in OJB ConnectionFactory.
123: * @return DBCP properties (might be empty, never null)
124: */
125: public Properties getDbcpProperties() {
126: return dbcpProperties;
127: }
128:
129: /**
130: * Sets a custom configuration attribute.
131: * @param attributeName the attribute name. Names starting with
132: * {@link #JDBC_PROPERTY_NAME_PREFIX} will be used (without the prefix) by the
133: * ConnectionFactory when creating connections from DriverManager
134: * (not used for external DataSource connections). Names starting with
135: * {@link #DBCP_PROPERTY_NAME_PREFIX} to Commons DBCP (if used, also without prefix).
136: * @param attributeValue the attribute value
137: */
138: public void addAttribute(String attributeName, String attributeValue) {
139: if (attributeName != null
140: && attributeName.startsWith(JDBC_PROPERTY_NAME_PREFIX)) {
141: final String jdbcPropertyName = attributeName
142: .substring(JDBC_PROPERTY_NAME_LENGTH);
143: jdbcProperties
144: .setProperty(jdbcPropertyName, attributeValue);
145: } else if (attributeName != null
146: && attributeName.startsWith(DBCP_PROPERTY_NAME_PREFIX)) {
147: final String dbcpPropertyName = attributeName
148: .substring(DBCP_PROPERTY_NAME_LENGTH);
149: dbcpProperties
150: .setProperty(dbcpPropertyName, attributeValue);
151: } else {
152: super .addAttribute(attributeName, attributeValue);
153: }
154: }
155:
156: public String toXML() {
157: RepositoryTags tags = RepositoryTags.getInstance();
158: String eol = SystemUtils.LINE_SEPARATOR;
159: StringBuffer buf = new StringBuffer();
160: //opening tag + attributes
161: buf.append(" ").append(
162: tags.getOpeningTagById(CONNECTION_POOL)).append(eol);
163: buf.append(" "
164: + tags.getAttribute(RepositoryElements.CON_MAX_ACTIVE,
165: "" + getMaxActive()) + eol);
166: buf.append(" "
167: + tags.getAttribute(RepositoryElements.CON_MAX_IDLE, ""
168: + getMaxIdle()) + eol);
169: buf.append(" "
170: + tags.getAttribute(RepositoryElements.CON_MAX_WAIT, ""
171: + getMaxWait()) + eol);
172: buf
173: .append(" "
174: + tags
175: .getAttribute(
176: RepositoryElements.CON_MIN_EVICTABLE_IDLE_TIME_MILLIS,
177: ""
178: + getMinEvictableIdleTimeMillis())
179: + eol);
180: buf
181: .append(" "
182: + tags
183: .getAttribute(
184: RepositoryElements.CON_NUM_TESTS_PER_EVICTION_RUN,
185: ""
186: + getNumTestsPerEvictionRun())
187: + eol);
188: buf.append(" "
189: + tags.getAttribute(
190: RepositoryElements.CON_TEST_ON_BORROW, ""
191: + isTestOnBorrow()) + eol);
192: buf.append(" "
193: + tags.getAttribute(
194: RepositoryElements.CON_TEST_ON_RETURN, ""
195: + isTestOnReturn()) + eol);
196: buf.append(" "
197: + tags.getAttribute(
198: RepositoryElements.CON_TEST_WHILE_IDLE, ""
199: + isTestWhileIdle()) + eol);
200: buf
201: .append(" "
202: + tags
203: .getAttribute(
204: RepositoryElements.CON_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
205: ""
206: + getTimeBetweenEvictionRunsMillis())
207: + eol);
208: buf.append(" "
209: + tags.getAttribute(
210: RepositoryElements.CON_WHEN_EXHAUSTED_ACTION,
211: "" + getWhenExhaustedAction()) + eol);
212: buf.append(" "
213: + tags.getAttribute(
214: RepositoryElements.VALIDATION_QUERY, ""
215: + getValidationQuery()) + eol);
216:
217: buf.append(" "
218: + tags.getAttribute(
219: RepositoryElements.CON_LOG_ABANDONED, ""
220: + isLogAbandoned()) + eol);
221: buf.append(" "
222: + tags.getAttribute(
223: RepositoryElements.CON_REMOVE_ABANDONED, ""
224: + isRemoveAbandoned()) + eol);
225: buf
226: .append(" "
227: + tags
228: .getAttribute(
229: RepositoryElements.CON_REMOVE_ABANDONED_TIMEOUT,
230: ""
231: + getRemoveAbandonedTimeout())
232: + eol);
233:
234: buf.append(" <!-- ");
235: buf.append(eol);
236: buf
237: .append(" Add JDBC-level properties here, like fetchSize.");
238: buf
239: .append(" Attributes with name prefix \"jdbc.\" are passed directly to the JDBC driver.");
240: buf.append(eol);
241: buf
242: .append(" e.g. <attribute attribute-name=\"fetchSize\" attribute-value=\"100\"/>");
243: buf.append(eol);
244: buf.append(" -->");
245: XmlHelper.appendSerializedAttributes(buf, " ", this );
246:
247: buf.append(" ").append(
248: tags.getClosingTagById(CONNECTION_POOL)).append(eol);
249: return buf.toString();
250: }
251:
252: }
|