PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/cayenne-3.0.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/DataSourceInfo.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 342 lines | 225 code | 53 blank | 64 comment | 39 complexity | 87385382a1f9bff01192be89fe36c8e3 MD5 | raw file
  1. /*****************************************************************
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. ****************************************************************/
  19. package org.apache.cayenne.conn;
  20. import java.io.Serializable;
  21. import org.apache.cayenne.conf.PasswordEncoding;
  22. import org.apache.cayenne.conf.PlainTextPasswordEncoder;
  23. import org.apache.cayenne.util.Util;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. /**
  27. * Helper JavaBean class that holds DataSource login information.
  28. *
  29. */
  30. public class DataSourceInfo implements Cloneable, Serializable {
  31. private static Log logger = LogFactory.getLog(DataSourceInfo.class);
  32. protected String userName;
  33. protected String password;
  34. protected String jdbcDriver;
  35. protected String dataSourceUrl;
  36. protected String adapterClassName;
  37. protected int minConnections = 1;
  38. protected int maxConnections = 1;
  39. // Constants for passwordLocation
  40. public static final String PASSWORD_LOCATION_CLASSPATH = "classpath";
  41. public static final String PASSWORD_LOCATION_EXECUTABLE = "executable";
  42. public static final String PASSWORD_LOCATION_MODEL = "model";
  43. public static final String PASSWORD_LOCATION_URL = "url";
  44. // Extended parameters
  45. protected String passwordEncoderClass = PasswordEncoding.standardEncoders[0];
  46. protected String passwordEncoderKey = "";
  47. protected String passwordLocation = PASSWORD_LOCATION_MODEL;
  48. protected String passwordSourceExecutable = "";
  49. protected String passwordSourceFilename = "";
  50. protected final String passwordSourceModel = "Not Applicable";
  51. protected String passwordSourceUrl = "";
  52. @Override
  53. public boolean equals(Object obj) {
  54. if (obj == this)
  55. return true;
  56. if (obj == null)
  57. return false;
  58. if (obj.getClass() != this.getClass())
  59. return false;
  60. DataSourceInfo dsi = (DataSourceInfo) obj;
  61. if (!Util.nullSafeEquals(this.userName, dsi.userName))
  62. return false;
  63. if (!Util.nullSafeEquals(this.password, dsi.password))
  64. return false;
  65. if (!Util.nullSafeEquals(this.jdbcDriver, dsi.jdbcDriver))
  66. return false;
  67. if (!Util.nullSafeEquals(this.dataSourceUrl, dsi.dataSourceUrl))
  68. return false;
  69. if (!Util.nullSafeEquals(this.adapterClassName, dsi.adapterClassName))
  70. return false;
  71. if (this.minConnections != dsi.minConnections)
  72. return false;
  73. if (this.maxConnections != dsi.maxConnections)
  74. return false;
  75. if (!Util.nullSafeEquals(this.passwordEncoderClass, dsi.passwordEncoderClass))
  76. return false;
  77. if (!Util.nullSafeEquals(this.passwordEncoderKey, dsi.passwordEncoderKey))
  78. return false;
  79. if (!Util.nullSafeEquals(this.passwordSourceFilename, dsi.passwordSourceFilename))
  80. return false;
  81. if (!Util.nullSafeEquals(this.passwordSourceModel, dsi.passwordSourceModel))
  82. return false;
  83. if (!Util.nullSafeEquals(this.passwordSourceUrl, dsi.passwordSourceUrl))
  84. return false;
  85. if (!Util.nullSafeEquals(this.passwordLocation, dsi.passwordLocation))
  86. return false;
  87. return true;
  88. }
  89. public DataSourceInfo cloneInfo() {
  90. try {
  91. return (DataSourceInfo) super.clone();
  92. }
  93. catch (CloneNotSupportedException ex) {
  94. throw new RuntimeException("Cloning error", ex);
  95. }
  96. }
  97. @Override
  98. public String toString() {
  99. StringBuilder buf = new StringBuilder();
  100. buf.append("[").append(this.getClass().getName()).append(":").append(
  101. "\n user name: ").append(userName).append("\n password: ");
  102. buf.append("**********");
  103. buf
  104. .append("\n driver: ")
  105. .append(jdbcDriver)
  106. .append("\n db adapter class: ")
  107. .append(adapterClassName)
  108. .append("\n url: ")
  109. .append(dataSourceUrl)
  110. .append("\n min. connections: ")
  111. .append(minConnections)
  112. .append("\n max. connections: ")
  113. .append(maxConnections);
  114. if (!PlainTextPasswordEncoder.class.getName().equals(passwordEncoderClass)) {
  115. buf.append("\n encoder class: ").append(passwordEncoderClass).append(
  116. "\n encoder key: ").append(passwordEncoderKey);
  117. }
  118. if (!PASSWORD_LOCATION_MODEL.equals(passwordLocation)) {
  119. buf.append("\n password location: ").append(passwordLocation).append(
  120. "\n password source: ").append(getPasswordSource());
  121. }
  122. buf.append("\n]");
  123. return buf.toString();
  124. }
  125. public String getAdapterClassName() {
  126. return adapterClassName;
  127. }
  128. public void setAdapterClassName(String adapterClassName) {
  129. this.adapterClassName = adapterClassName;
  130. }
  131. public void setMinConnections(int minConnections) {
  132. this.minConnections = minConnections;
  133. }
  134. public int getMinConnections() {
  135. return minConnections;
  136. }
  137. public void setMaxConnections(int maxConnections) {
  138. this.maxConnections = maxConnections;
  139. }
  140. public int getMaxConnections() {
  141. return maxConnections;
  142. }
  143. public void setUserName(String userName) {
  144. this.userName = userName;
  145. }
  146. public String getUserName() {
  147. return userName;
  148. }
  149. public void setPassword(String password) {
  150. this.password = password;
  151. }
  152. public String getPassword() {
  153. return password;
  154. }
  155. public void setJdbcDriver(String jdbcDriver) {
  156. this.jdbcDriver = jdbcDriver;
  157. }
  158. public String getJdbcDriver() {
  159. return jdbcDriver;
  160. }
  161. public void setDataSourceUrl(String dataSourceUrl) {
  162. this.dataSourceUrl = dataSourceUrl;
  163. }
  164. public String getDataSourceUrl() {
  165. return dataSourceUrl;
  166. }
  167. public PasswordEncoding getPasswordEncoder() {
  168. try {
  169. return (PasswordEncoding)
  170. Util.getJavaClass(getPasswordEncoderClass()).newInstance();
  171. }
  172. catch (InstantiationException e) {
  173. ; // Swallow it -- no need to throw/etc.
  174. }
  175. catch (IllegalAccessException e) {
  176. ; // Swallow it -- no need to throw/etc.
  177. }
  178. catch (ClassNotFoundException e) {
  179. ; // Swallow it -- no need to throw/etc.
  180. }
  181. logger.error("Failed to obtain specified Password Encoder '" +
  182. getPasswordEncoderClass() + "' -- please check CLASSPATH");
  183. return null;
  184. }
  185. /**
  186. * @return the passwordEncoderClass
  187. */
  188. public String getPasswordEncoderClass() {
  189. return passwordEncoderClass;
  190. }
  191. /**
  192. * @param passwordEncoderClass the passwordEncoderClass to set
  193. */
  194. public void setPasswordEncoderClass(String passwordEncoderClass) {
  195. if (passwordEncoderClass == null)
  196. this.passwordEncoderClass = PasswordEncoding.standardEncoders[0];
  197. else
  198. this.passwordEncoderClass = passwordEncoderClass;
  199. }
  200. /**
  201. * @return the passwordEncoderKey
  202. */
  203. public String getPasswordEncoderKey() {
  204. return passwordEncoderKey;
  205. }
  206. /**
  207. * @param passwordEncoderKey the passwordEncoderKey to set
  208. */
  209. public void setPasswordEncoderKey(String passwordEncoderKey) {
  210. this.passwordEncoderKey = passwordEncoderKey;
  211. }
  212. /**
  213. * @return the passwordLocationFilename
  214. */
  215. public String getPasswordSourceFilename() {
  216. return passwordSourceFilename;
  217. }
  218. /**
  219. * @param passwordSourceFilename the passwordSourceFilename to set
  220. */
  221. public void setPasswordSourceFilename(String passwordSourceFilename) {
  222. this.passwordSourceFilename = passwordSourceFilename;
  223. }
  224. /**
  225. * @return the passwordLocationModel
  226. */
  227. public String getPasswordSourceModel() {
  228. return passwordSourceModel;
  229. }
  230. /**
  231. * @return the passwordLocationUrl
  232. */
  233. public String getPasswordSourceUrl() {
  234. return passwordSourceUrl;
  235. }
  236. /**
  237. * @param passwordSourceUrl the passwordSourceUrl to set
  238. */
  239. public void setPasswordSourceUrl(String passwordSourceUrl) {
  240. this.passwordSourceUrl = passwordSourceUrl;
  241. }
  242. /**
  243. * @return the passwordLocationExecutable
  244. */
  245. public String getPasswordSourceExecutable() {
  246. return passwordSourceExecutable;
  247. }
  248. /**
  249. * @param passwordSourceExecutable the passwordSourceExecutable to set
  250. */
  251. public void setPasswordSourceExecutable(String passwordSourceExecutable) {
  252. this.passwordSourceExecutable = passwordSourceExecutable;
  253. }
  254. public String getPasswordSource() {
  255. if (getPasswordLocation().equals(PASSWORD_LOCATION_CLASSPATH))
  256. return getPasswordSourceFilename();
  257. else if (getPasswordLocation().equals(PASSWORD_LOCATION_EXECUTABLE))
  258. return getPasswordSourceExecutable();
  259. else if (getPasswordLocation().equals(PASSWORD_LOCATION_MODEL))
  260. return getPasswordSourceModel();
  261. else if (getPasswordLocation().equals(PASSWORD_LOCATION_URL))
  262. return getPasswordSourceUrl();
  263. throw new RuntimeException("Invalid password source detected");
  264. }
  265. public void setPasswordSource(String passwordSource) {
  266. // The location for the model is omitted since it cannot change
  267. if (getPasswordLocation().equals(PASSWORD_LOCATION_CLASSPATH))
  268. setPasswordSourceFilename(passwordSource);
  269. else if (getPasswordLocation().equals(PASSWORD_LOCATION_EXECUTABLE))
  270. setPasswordSourceExecutable(passwordSource);
  271. else if (getPasswordLocation().equals(PASSWORD_LOCATION_URL))
  272. setPasswordSourceUrl(passwordSource);
  273. }
  274. /**
  275. * @return the passwordLocation
  276. */
  277. public String getPasswordLocation() {
  278. return passwordLocation;
  279. }
  280. /**
  281. * @param passwordLocation the passwordLocation to set
  282. */
  283. public void setPasswordLocation(String passwordLocation) {
  284. if (passwordLocation == null)
  285. this.passwordLocation = DataSourceInfo.PASSWORD_LOCATION_MODEL;
  286. else
  287. this.passwordLocation = passwordLocation;
  288. }
  289. }