Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Context>
  3. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  4. <Resource
  5. name="jdbc/MyDB"
  6. auth="Container"
  7. type="javax.sql.DataSource"
  8. removeAbandoned="true"
  9. removeAbandonedTimeout="15"
  10. maxActive="5"
  11. maxIdle="5"
  12. maxWait="7000"
  13. username="${db.mydb.uid}"
  14. password="${db.mydb.pwd}"
  15. driverClassName="${db.mydb.driver}"
  16. url="${db.mydb.url}${db.mydb.dbName}?autoReconnectForPools=true&characterEncoding=UTF-8"
  17. factory="com.mycompany.util.configuration.CustomDataSourceFactory"
  18. validationQuery="SELECT '1';"
  19. testOnBorrow="true"/>
  20. </Context>
  21.  
  22. package com.mycompany.util.configuration;
  23.  
  24. import java.util.Hashtable;
  25. import java.util.regex.Matcher;
  26. import java.util.regex.Pattern;
  27. import javax.naming.Context;
  28. import javax.naming.Name;
  29. import javax.naming.RefAddr;
  30. import javax.naming.Reference;
  31. import javax.naming.StringRefAddr;
  32. import javax.naming.spi.ObjectFactory;
  33. import org.apache.commons.dbcp.BasicDataSourceFactory;
  34.  
  35. public class CustomDataSourceFactory extends BasicDataSourceFactory implements ObjectFactory {
  36.  
  37. private static final Pattern _propRefPattern = Pattern.compile("\$\{.*?\}");
  38.  
  39. //http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#Adding_Custom_Resource_Factories
  40. @Override
  41. public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
  42. if (obj instanceof Reference) {
  43. Reference ref = (Reference) obj;
  44. System.out.println("Resolving context reference values dynamically");
  45.  
  46. for(int i = 0; i < ref.size(); i++) {
  47. RefAddr addr = ref.get(i);
  48. String tag = addr.getType();
  49. String value = (String) addr.getContent();
  50.  
  51. Matcher matcher = _propRefPattern.matcher(value);
  52. if (matcher.find()) {
  53. String resolvedValue = resolve(value);
  54. System.out.println("Resolved " + value + " to " + resolvedValue);
  55. ref.remove(i);
  56. ref.add(i, new StringRefAddr(tag, resolvedValue));
  57. }
  58. }
  59. }
  60. // Return the customized instance
  61. return super.getObjectInstance(obj, name, nameCtx, environment);
  62. }
  63.  
  64. private String resolve(String value) {
  65. //Given the placeholder, do stuff to figure out what it's true value should be, and return that String.
  66. //This could be decryption, or maybe using a properties file.
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement