KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > ConnectionPoolDescriptor


1 package org.apache.ojb.broker.metadata;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.lang.SystemUtils;
22 import org.apache.ojb.broker.util.pooling.PoolConfiguration;
23 import org.apache.ojb.broker.util.XmlHelper;
24
25 /**
26  * Encapsulates connection pooling and JDBC-driver configuration properties managed by
27  * {@link org.apache.ojb.broker.metadata.JdbcConnectionDescriptor}.
28  * <p>
29  * Every new instantiated <code>ConnectionPoolDescriptor</code> is associated with
30  * default connection pool attributes.
31  *
32  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
33  * @version $Id: ConnectionPoolDescriptor.java,v 1.14.2.2 2005/04/30 20:55:15 mkalen Exp $
34  */

35 public class ConnectionPoolDescriptor extends PoolConfiguration implements Serializable JavaDoc, XmlCapable
36 {
37     private static final long serialVersionUID = -3071461685659671879L;
38
39     /** String prefix for JDBC properties passed to DriverManager. */
40     public static final String JavaDoc JDBC_PROPERTY_NAME_PREFIX = "jdbc.";
41     private static final int JDBC_PROPERTY_NAME_LENGTH = JDBC_PROPERTY_NAME_PREFIX.length();
42     /**
43      * String prefix for DBCP properties.
44      * Currently OJB only uses this for setting DBCP parameters for pooling of Statement,
45      * not the max/test-parameters etc for the DBCP Connection pool
46      * (since there is only a JDBC2.0+ version of the Basic-classes ie BasicDataSource
47      * and no DriverManager-based one).
48      */

49     public static final String JavaDoc DBCP_PROPERTY_NAME_PREFIX = "dbcp.";
50     private static final int DBCP_PROPERTY_NAME_LENGTH = DBCP_PROPERTY_NAME_PREFIX.length();
51
52     /** JDBC properties configured in OJB (not used for DataSource connections). */
53     protected Properties JavaDoc jdbcProperties;
54     /** DBCP Statement cache properties configured in OJB (not used for DataSource connections). */
55     protected Properties JavaDoc dbcpProperties;
56
57     /** Configuration attribute name for JDBC fetchSize hint. */
58     public static final String JavaDoc FETCH_SIZE = "fetchSize";
59
60     private Class JavaDoc connectionFactory;
61
62
63     public ConnectionPoolDescriptor()
64     {
65         super();
66         init();
67     }
68
69     /**
70      * Set some initial values.
71      */

72     private void init()
73     {
74         jdbcProperties = new Properties JavaDoc();
75         dbcpProperties = new Properties JavaDoc();
76         setFetchSize(0);
77         this.setTestOnBorrow(true);
78         this.setTestOnReturn(false);
79         this.setTestWhileIdle(false);
80         this.setLogAbandoned(false);
81         this.setRemoveAbandoned(false);
82     }
83
84     public Class JavaDoc getConnectionFactory()
85     {
86         return this.connectionFactory;
87     }
88
89     public void setConnectionFactory(Class JavaDoc connectionFactory)
90     {
91         if (connectionFactory == null) throw new MetadataException("Given ConnectionFactory was null");
92         this.connectionFactory = connectionFactory;
93     }
94
95     /**
96      * Returns the fetchSize hint set for this connection pool.
97      * @return fetchSize hint or 0 if JDBC-driver specific default is used
98      */

99     public int getFetchSize()
100     {
101         // We depend on init() to always set fetchSize hint
102
return Integer.parseInt(getProperty(FETCH_SIZE));
103     }
104
105     /**
106      * Sets the fetchSize hint for this connection pool.
107      * @param fetchSize fetchSize hint or 0 to use JDBC-driver specific default
108      */

109     public void setFetchSize(int fetchSize)
110     {
111         setProperty(FETCH_SIZE, Integer.toString(fetchSize));
112     }
113
114     /**
115      * Returns the JDBC properties to be used by the ConnectionFactory
116      * when creating connections from DriverManager.
117      * @return JDBC-driver specific properties (might be empty, never null)
118      */

119     public Properties JavaDoc getJdbcProperties()
120     {
121         return jdbcProperties;
122     }
123
124     /**
125      * Returns the DBCP properties to be used for Statement caching
126      * when creating DBCP connection pool in OJB ConnectionFactory.
127      * @return DBCP properties (might be empty, never null)
128      */

129     public Properties JavaDoc getDbcpProperties()
130     {
131         return dbcpProperties;
132     }
133
134     /**
135      * Sets a custom configuration attribute.
136      * @param attributeName the attribute name. Names starting with
137      * {@link #JDBC_PROPERTY_NAME_PREFIX} will be used (without the prefix) by the
138      * ConnectionFactory when creating connections from DriverManager
139      * (not used for external DataSource connections). Names starting with
140      * {@link #DBCP_PROPERTY_NAME_PREFIX} to Commons DBCP (if used, also without prefix).
141      * @param attributeValue the attribute value
142      */

143     public void addAttribute(String JavaDoc attributeName, String JavaDoc attributeValue)
144     {
145         if (attributeName != null && attributeName.startsWith(JDBC_PROPERTY_NAME_PREFIX))
146         {
147             final String JavaDoc jdbcPropertyName = attributeName.substring(JDBC_PROPERTY_NAME_LENGTH);
148             jdbcProperties.setProperty(jdbcPropertyName, attributeValue);
149         }
150         else if (attributeName != null && attributeName.startsWith(DBCP_PROPERTY_NAME_PREFIX))
151         {
152             final String JavaDoc dbcpPropertyName = attributeName.substring(DBCP_PROPERTY_NAME_LENGTH);
153             dbcpProperties.setProperty(dbcpPropertyName, attributeValue);
154         }
155         else
156         {
157             super.addAttribute(attributeName, attributeValue);
158         }
159     }
160
161     public String JavaDoc toXML()
162     {
163         RepositoryTags tags = RepositoryTags.getInstance();
164         String JavaDoc eol = SystemUtils.LINE_SEPARATOR;
165         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
166         //opening tag + attributes
167
buf.append(" ").append(tags.getOpeningTagById(CONNECTION_POOL)).append(eol);
168         buf.append(" " + tags.getAttribute(RepositoryElements.CON_MAX_ACTIVE, "" + getMaxActive()) + eol);
169         buf.append(" " + tags.getAttribute(RepositoryElements.CON_MAX_IDLE, "" + getMaxIdle()) + eol);
170         buf.append(" " + tags.getAttribute(RepositoryElements.CON_MAX_WAIT, "" + getMaxWait()) + eol);
171         buf.append(" " + tags.getAttribute(RepositoryElements.CON_MIN_EVICTABLE_IDLE_TIME_MILLIS, "" +
172                 getMinEvictableIdleTimeMillis()) + eol);
173         buf.append(" " + tags.getAttribute(RepositoryElements.CON_NUM_TESTS_PER_EVICTION_RUN, "" +
174                 getNumTestsPerEvictionRun()) + eol);
175         buf.append(" " + tags.getAttribute(RepositoryElements.CON_TEST_ON_BORROW, "" + isTestOnBorrow()) + eol);
176         buf.append(" " + tags.getAttribute(RepositoryElements.CON_TEST_ON_RETURN, "" + isTestOnReturn()) + eol);
177         buf.append(" " + tags.getAttribute(RepositoryElements.CON_TEST_WHILE_IDLE, "" + isTestWhileIdle()) + eol);
178         buf.append(" " + tags.getAttribute(RepositoryElements.CON_TIME_BETWEEN_EVICTION_RUNS_MILLIS, "" +
179                 getTimeBetweenEvictionRunsMillis()) + eol);
180         buf.append(" " + tags.getAttribute(RepositoryElements.CON_WHEN_EXHAUSTED_ACTION, "" +
181                 getWhenExhaustedAction()) + eol);
182         buf.append(" " + tags.getAttribute(RepositoryElements.VALIDATION_QUERY, "" + getValidationQuery()) + eol);
183
184         buf.append(" " + tags.getAttribute(RepositoryElements.CON_LOG_ABANDONED, "" + isLogAbandoned()) + eol);
185         buf.append(" " + tags.getAttribute(RepositoryElements.CON_REMOVE_ABANDONED, "" +
186                 isRemoveAbandoned()) + eol);
187         buf.append(" " + tags.getAttribute(RepositoryElements.CON_REMOVE_ABANDONED_TIMEOUT, "" +
188                 getRemoveAbandonedTimeout()) + eol);
189
190         buf.append(" <!-- ");
191         buf.append(eol);
192         buf.append(" Add JDBC-level properties here, like fetchSize.");
193         buf.append(" Attributes with name prefix \"jdbc.\" are passed directly to the JDBC driver.");
194         buf.append(eol);
195         buf.append(" e.g. <attribute attribute-name=\"fetchSize\" attribute-value=\"100\"/>");
196         buf.append(eol);
197         buf.append(" -->");
198         XmlHelper.appendSerializedAttributes(buf, " ", this);
199
200         buf.append(" ").append(tags.getClosingTagById(CONNECTION_POOL)).append(eol);
201         return buf.toString();
202     }
203
204 }
205
Popular Tags