1 19 20 package org.apache.cayenne.conn; 21 22 import java.io.Serializable ; 23 24 import org.apache.cayenne.conf.PasswordEncoding; 25 import org.apache.cayenne.conf.PlainTextPasswordEncoder; 26 import org.apache.cayenne.util.Util; 27 28 37 public class DataSourceInfo implements Cloneable , Serializable { 38 39 protected String userName; 40 protected String password; 41 protected String jdbcDriver; 42 protected String dataSourceUrl; 43 protected String adapterClassName; 44 protected int minConnections = 1; 45 protected int maxConnections = 1; 46 47 public static final String PASSWORD_LOCATION_CLASSPATH = "classpath"; 49 public static final String PASSWORD_LOCATION_EXECUTABLE = "executable"; 50 public static final String PASSWORD_LOCATION_MODEL = "model"; 51 public static final String PASSWORD_LOCATION_URL = "url"; 52 53 protected String passwordEncoderClass = PasswordEncoding.standardEncoders[0]; 55 protected String passwordEncoderSalt = ""; 56 protected String passwordSourceExecutable = ""; 57 protected String passwordSourceFilename = ""; 58 protected String passwordSourceModel = "Not Applicable"; 59 protected String passwordSourceUrl = ""; 60 protected String passwordLocation = PASSWORD_LOCATION_MODEL; 61 62 public boolean equals(Object obj) { 63 if (obj == this) 64 return true; 65 66 if (obj == null) 67 return false; 68 69 if (obj.getClass() != this.getClass()) 70 return false; 71 72 DataSourceInfo dsi = (DataSourceInfo) obj; 73 74 if (!Util.nullSafeEquals(this.userName, dsi.userName)) 75 return false; 76 if (!Util.nullSafeEquals(this.password, dsi.password)) 77 return false; 78 if (!Util.nullSafeEquals(this.jdbcDriver, dsi.jdbcDriver)) 79 return false; 80 if (!Util.nullSafeEquals(this.dataSourceUrl, dsi.dataSourceUrl)) 81 return false; 82 if (!Util.nullSafeEquals(this.adapterClassName, dsi.adapterClassName)) 83 return false; 84 if (this.minConnections != dsi.minConnections) 85 return false; 86 if (this.maxConnections != dsi.maxConnections) 87 return false; 88 if (!Util.nullSafeEquals(this.passwordEncoderClass, dsi.passwordEncoderClass)) 89 return false; 90 if (!Util.nullSafeEquals(this.passwordEncoderSalt, dsi.passwordEncoderSalt)) 91 return false; 92 if (!Util.nullSafeEquals(this.passwordSourceFilename, dsi.passwordSourceFilename)) 93 return false; 94 if (!Util.nullSafeEquals(this.passwordSourceModel, dsi.passwordSourceModel)) 95 return false; 96 if (!Util.nullSafeEquals(this.passwordSourceUrl, dsi.passwordSourceUrl)) 97 return false; 98 if (!Util.nullSafeEquals(this.passwordLocation, dsi.passwordLocation)) 99 return false; 100 101 return true; 102 } 103 104 public DataSourceInfo cloneInfo() { 105 try { 106 return (DataSourceInfo) super.clone(); 107 } 108 catch (CloneNotSupportedException ex) { 109 throw new RuntimeException ("Cloning error", ex); 110 } 111 } 112 113 public String toString() { 114 StringBuffer buf = new StringBuffer (); 115 buf.append("[").append(this.getClass().getName()).append(":").append( 116 "\n user name: ").append(userName).append("\n password: "); 117 118 buf.append("**********"); 119 buf 120 .append("\n driver: ") 121 .append(jdbcDriver) 122 .append("\n db adapter class: ") 123 .append(adapterClassName) 124 .append("\n url: ") 125 .append(dataSourceUrl) 126 .append("\n min. connections: ") 127 .append(minConnections) 128 .append("\n max. connections: ") 129 .append(maxConnections); 130 131 if (!PlainTextPasswordEncoder.class.getName().equals(passwordEncoderClass)) { 132 buf.append("\n encoder class: ").append(passwordEncoderClass).append( 133 "\n encoder salt: ").append(passwordEncoderSalt); 134 } 135 136 if (!PASSWORD_LOCATION_MODEL.equals(passwordLocation)) { 137 buf.append("\n password location: ").append(passwordLocation).append( 138 "\n password source: ").append(getPasswordSource()); 139 } 140 141 buf.append("\n]"); 142 return buf.toString(); 143 } 144 145 public String getAdapterClassName() { 146 return adapterClassName; 147 } 148 149 public void setAdapterClassName(String adapterClassName) { 150 this.adapterClassName = adapterClassName; 151 } 152 153 public void setMinConnections(int minConnections) { 154 this.minConnections = minConnections; 155 } 156 157 public int getMinConnections() { 158 return minConnections; 159 } 160 161 public void setMaxConnections(int maxConnections) { 162 this.maxConnections = maxConnections; 163 } 164 165 public int getMaxConnections() { 166 return maxConnections; 167 } 168 169 public void setUserName(String userName) { 170 this.userName = userName; 171 } 172 173 public String getUserName() { 174 return userName; 175 } 176 177 public void setPassword(String password) { 178 this.password = password; 179 } 180 181 public String getPassword() { 182 return password; 183 } 184 185 public void setJdbcDriver(String jdbcDriver) { 186 this.jdbcDriver = jdbcDriver; 187 } 188 189 public String getJdbcDriver() { 190 return jdbcDriver; 191 } 192 193 public void setDataSourceUrl(String dataSourceUrl) { 194 this.dataSourceUrl = dataSourceUrl; 195 } 196 197 public String getDataSourceUrl() { 198 return dataSourceUrl; 199 } 200 201 public PasswordEncoding getPasswordEncoder() { 202 PasswordEncoding encoder = null; 203 204 try { 205 encoder = (PasswordEncoding) Thread 206 .currentThread() 207 .getContextClassLoader() 208 .loadClass(getPasswordEncoderClass()) 209 .newInstance(); 210 } 213 catch (InstantiationException exception) { 214 exception.printStackTrace(); 216 } 217 catch (IllegalAccessException exception) { 218 exception.printStackTrace(); 220 } 221 catch (ClassNotFoundException exception) { 222 exception.printStackTrace(); 224 } 225 226 return encoder; 227 } 228 229 232 public String getPasswordEncoderClass() { 233 return passwordEncoderClass; 234 } 235 236 239 public void setPasswordEncoderClass(String passwordEncoderClass) { 240 if (passwordEncoderClass == null) 241 this.passwordEncoderClass = PasswordEncoding.standardEncoders[0]; 242 else 243 this.passwordEncoderClass = passwordEncoderClass; 244 } 245 246 249 public String getPasswordEncoderSalt() { 250 return passwordEncoderSalt; 251 } 252 253 256 public void setPasswordEncoderSalt(String passwordEncoderSalt) { 257 this.passwordEncoderSalt = passwordEncoderSalt; 258 } 259 260 263 public String getPasswordSourceFilename() { 264 return passwordSourceFilename; 265 } 266 267 270 public void setPasswordSourceFilename(String passwordSourceFilename) { 271 this.passwordSourceFilename = passwordSourceFilename; 272 } 273 274 277 public String getPasswordSourceModel() { 278 return passwordSourceModel; 279 } 280 281 284 public String getPasswordSourceUrl() { 285 return passwordSourceUrl; 286 } 287 288 291 public void setPasswordSourceUrl(String passwordSourceUrl) { 292 this.passwordSourceUrl = passwordSourceUrl; 293 } 294 295 298 public String getPasswordSourceExecutable() { 299 return passwordSourceExecutable; 300 } 301 302 305 public void setPasswordSourceExecutable(String passwordSourceExecutable) { 306 this.passwordSourceExecutable = passwordSourceExecutable; 307 } 308 309 public String getPasswordSource() { 310 if (getPasswordLocation().equals(PASSWORD_LOCATION_CLASSPATH)) 311 return getPasswordSourceFilename(); 312 else if (getPasswordLocation().equals(PASSWORD_LOCATION_EXECUTABLE)) 313 return getPasswordSourceExecutable(); 314 else if (getPasswordLocation().equals(PASSWORD_LOCATION_MODEL)) 315 return getPasswordSourceModel(); 316 else if (getPasswordLocation().equals(PASSWORD_LOCATION_URL)) 317 return getPasswordSourceUrl(); 318 319 throw new RuntimeException ("Invalid password source detected"); 320 } 321 322 public void setPasswordSource(String passwordSource) { 323 if (getPasswordLocation().equals(PASSWORD_LOCATION_CLASSPATH)) 325 setPasswordSourceFilename(passwordSource); 326 else if (getPasswordLocation().equals(PASSWORD_LOCATION_EXECUTABLE)) 327 setPasswordSourceExecutable(passwordSource); 328 else if (getPasswordLocation().equals(PASSWORD_LOCATION_URL)) 329 setPasswordSourceUrl(passwordSource); 330 } 331 332 335 public String getPasswordLocation() { 336 return passwordLocation; 337 } 338 339 342 public void setPasswordLocation(String passwordLocation) { 343 if (passwordLocation == null) 344 this.passwordLocation = DataSourceInfo.PASSWORD_LOCATION_MODEL; 345 else 346 this.passwordLocation = passwordLocation; 347 } 348 } 349 | Popular Tags |