1 package org.naftulin.configmgr.content; 2 3 import java.util.LinkedList; 4 import java.util.List; 5 6 import org.naftulin.configmgr.ConfigurationManagerException; 7 import org.naftulin.configmgr.parsers.ConfigEntryParser; 8 import org.naftulin.configmgr.parsers.DbDirectParserImpl; 9 10 /*** 11 * Represents a db direct entry, with it's key, name-value pairs and parser class. 12 * Intended to mirror master configuration rules for external configuration. 13 * The external configuraton xml is: 14 * <pre> 15 * <dbdirect key="the key" 16 driverClass="database driver class" 17 connectionURl="database connection url" 18 sql="sql that selects at least key and value columns from any table(s) or view with any static where condition" 19 user="user name to login to db if any" 20 password="password for the user if any" 21 keyColumnName="column name where the key is selected" 22 valueColumnName="column name where the value is selected" 23 * 24 * > 25 <param name="key1" value="value1" /> 26 ..... 27 <param name="keyn" value="valuen" /> 28 </dbdirect> 29 * </pre> 30 * 31 * @author Henry Henry 32 * @since 1.3 33 */ 34 public class DbDirectRecordImpl extends AbstractRecordImpl { 35 private String driverClass; 36 private String connectionUrl; 37 private String sql; 38 private String user; 39 private String password; 40 private String keyColumnName; 41 private String valueColumnName; 42 private final List<NameValuePairImpl> nameValuePairs = new LinkedList<NameValuePairImpl>(); 43 private ConfigEntryParser parser; 44 45 46 public String getDriverClass() { 47 return driverClass; 48 } 49 50 public void setDriverClass(final String driverClass) { 51 this.driverClass = driverClass; 52 } 53 54 public String getKeyColumnName() { 55 return keyColumnName; 56 } 57 58 public void setKeyColumnName(final String keyColumnName) { 59 this.keyColumnName = keyColumnName; 60 } 61 62 public List<NameValuePairImpl> getNameValuePairs() { 63 return nameValuePairs; 64 } 65 66 /*** 67 * Adds a name-value pair as it is read from the master configuration. 68 * @param nv the name value pair. 69 */ 70 public void addNameValuePair(final NameValuePairImpl nv) { 71 nameValuePairs.add(nv); 72 } 73 74 public String getPassword() { 75 return password; 76 } 77 78 public void setPassword(final String password) { 79 this.password = password; 80 } 81 82 public String getSql() { 83 return sql; 84 } 85 86 public void setSql(final String sql) { 87 this.sql = sql; 88 } 89 90 public String getUser() { 91 return user; 92 } 93 94 public void setUser(final String user) { 95 this.user = user; 96 } 97 98 public String getValueColumnName() { 99 return valueColumnName; 100 } 101 102 public void setValueColumnName(final String valueColumnName) { 103 this.valueColumnName = valueColumnName; 104 } 105 106 /*** 107 * Returns a database parser instance. 108 * @return a database parser instance. 109 */ 110 public synchronized ConfigEntryParser getParser() throws ConfigurationManagerException { 111 if (parser == null) { 112 parser = new DbDirectParserImpl(driverClass, connectionUrl, sql, user, password, keyColumnName, valueColumnName, nameValuePairs); 113 } 114 return parser; 115 } 116 117 public String getConnectionUrl() { 118 return connectionUrl; 119 } 120 121 public void setConnectionUrl(final String connectionUrl) { 122 this.connectionUrl = connectionUrl; 123 } 124 125 /*** 126 * Since it is not a file-based cofiguration, we need to overwrite the method. 127 * @return false 128 */ 129 public boolean isGetUrlForFile() { 130 return false; 131 } 132 133 public String getFileName() { 134 return "driver : " + driverClass + " connection " + connectionUrl + " sql " + sql + " user " + user; 135 } 136 }