1 package org.apache.ojb.broker.metadata; 2 3 17 18 import java.io.Serializable ; 19 import java.util.Properties ; 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 35 public class ConnectionPoolDescriptor extends PoolConfiguration implements Serializable , XmlCapable 36 { 37 private static final long serialVersionUID = -3071461685659671879L; 38 39 40 public static final String JDBC_PROPERTY_NAME_PREFIX = "jdbc."; 41 private static final int JDBC_PROPERTY_NAME_LENGTH = JDBC_PROPERTY_NAME_PREFIX.length(); 42 49 public static final String DBCP_PROPERTY_NAME_PREFIX = "dbcp."; 50 private static final int DBCP_PROPERTY_NAME_LENGTH = DBCP_PROPERTY_NAME_PREFIX.length(); 51 52 53 protected Properties jdbcProperties; 54 55 protected Properties dbcpProperties; 56 57 58 public static final String FETCH_SIZE = "fetchSize"; 59 60 private Class connectionFactory; 61 62 63 public ConnectionPoolDescriptor() 64 { 65 super(); 66 init(); 67 } 68 69 72 private void init() 73 { 74 jdbcProperties = new Properties (); 75 dbcpProperties = new Properties (); 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 getConnectionFactory() 85 { 86 return this.connectionFactory; 87 } 88 89 public void setConnectionFactory(Class connectionFactory) 90 { 91 if (connectionFactory == null) throw new MetadataException("Given ConnectionFactory was null"); 92 this.connectionFactory = connectionFactory; 93 } 94 95 99 public int getFetchSize() 100 { 101 return Integer.parseInt(getProperty(FETCH_SIZE)); 103 } 104 105 109 public void setFetchSize(int fetchSize) 110 { 111 setProperty(FETCH_SIZE, Integer.toString(fetchSize)); 112 } 113 114 119 public Properties getJdbcProperties() 120 { 121 return jdbcProperties; 122 } 123 124 129 public Properties getDbcpProperties() 130 { 131 return dbcpProperties; 132 } 133 134 143 public void addAttribute(String attributeName, String attributeValue) 144 { 145 if (attributeName != null && attributeName.startsWith(JDBC_PROPERTY_NAME_PREFIX)) 146 { 147 final String 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 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 toXML() 162 { 163 RepositoryTags tags = RepositoryTags.getInstance(); 164 String eol = SystemUtils.LINE_SEPARATOR; 165 StringBuffer buf = new StringBuffer (); 166 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 |