Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. Add the following Jar's to the JUnit test-case's CLASSPATH:
  2.  
  3. TOMCAT_HOME/bin/tomcat-juli.jar (required by catalina.jar)
  4. TOMCAT_HOME/lib/catalina.jar (contains the actual factory)
  5.  
  6. Create the binding that you require in the static "for-all-tests" method:
  7.  
  8. @BeforeClass
  9. public static void setUpClass() throws Exception {
  10. ...
  11. // Use Apache Tomcat's Directory
  12. System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
  13. System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
  14. // Standard hook
  15. InitialContext initialContext = new InitialContext();
  16. // Create binding
  17. initialContext.createSubcontext("java:");
  18. initialContext.createSubcontext("java:comp");
  19. initialContext.createSubcontext("java:comp/env");
  20. initialContext.createSubcontext("java:comp/env/jdbc");
  21. // Construct DataSource
  22. OracleConnectionPoolDataSource dataSource = new OracleConnectionPoolDataSource();
  23. dataSource.setURL("jdbc:oracle:thin:@myserver:1521:MYSID");
  24. dataSource.setUser("username");
  25. dataSource.setPassword("password");
  26.  
  27. initialContext.bind("java:comp/env/jdbc/mydatabase", dataSource);
  28. ...
  29. }
  30.  
  31.  
  32. Then you can create this method in your Singleton class (either lookup method works):
  33.  
  34. public Connection getConnection() throws NamingException, SQLException {
  35. if (dataSource == null) {
  36. Context initialContext = new InitialContext();
  37. boolean bLooksLikeChangeDirectory = false;
  38. if (bLooksLikeChangeDirectory) {
  39. Context context = (Context) initialContext.lookup("java:comp/env");
  40. dataSource = (DataSource) context.lookup("jdbc/mydatabase");
  41. } else {
  42. dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/mydatabase");
  43. }
  44. }
  45. Connection result = dataSource.getConnection();
  46. return result;
  47. }
  48.  
  49. You can use the "com.sun.jndi.fscontext.RefFSContextFactory" (fscontext.jar and providerutil.jar).
  50.  
  51. I wanted "lookup" to be runnable from inside an application server as well, so it would be useful if someone could tell me
  52. for sure if one *has* to use lookup("java:comp/env/jdbc/mydbnickname") instead of lookup("jdbc/mydbnickname") when running
  53. inside an application server.
  54.  
  55. The latter is preferable, because "java:comp/env" does not exist in the stand-alone RefFSContextFactory directory,
  56. so you'd have to have a System property that specifies the JNDI lookup parameter.
  57.  
  58. Overview
  59. You can use "jdbc/mydbnickname" as the argument to "lookup" and to "rebind" (i.e. no "scheme:").
  60. In this case, "RefFSContextFactory" uses the "default scheme", whatever that is ("file:" or "jndi:").
  61. With the following in "jndi.properties" (on the CLASSPATH)
  62. java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
  63. java.naming.provider.url=file:///u:/workdirectory
  64. and this in "persistence.xml" (JTA setup)
  65. <jta-data-source>jdbc/mydbnickname</jta-data-source>
  66. When you use:
  67. ...rebind("jdbc/mydbnickname", ...)
  68. and
  69. ...lookup("jdbc/mydbnickname")
  70. then the ".bindings" file (created by "rebind") is
  71. "u:/workdirectory/.bindings"
  72. and it looks like this (I have sorted it and tested it with just "lookup"):
  73. #This file is used by the JNDI FSContext.
  74. #Thu Jan 09 16:02:17 EST 2014
  75. jdbc/mydbnickname/ClassName=com.jolbox.bonecp.BoneCPDataSource
  76. jdbc/mydbnickname/FactoryName=com.jolbox.bonecp.BoneCPDataSource
  77. jdbc/mydbnickname/RefAddr/0/Content=oracle.jdbc.OracleDriver
  78. jdbc/mydbnickname/RefAddr/0/Encoding=String
  79. jdbc/mydbnickname/RefAddr/0/Type=driverClassName
  80. jdbc/mydbnickname/RefAddr/1/Content=jdbc:oracle:thin:@myserver:1521:mysid
  81. jdbc/mydbnickname/RefAddr/1/Encoding=String
  82. jdbc/mydbnickname/RefAddr/1/Type=jdbcUrl
  83. jdbc/mydbnickname/RefAddr/2/Content=myusername
  84. jdbc/mydbnickname/RefAddr/2/Encoding=String
  85. jdbc/mydbnickname/RefAddr/2/Type=username
  86. jdbc/mydbnickname/RefAddr/3/Content=mypassword
  87. jdbc/mydbnickname/RefAddr/3/Encoding=String
  88. jdbc/mydbnickname/RefAddr/3/Type=password
  89. If you use
  90. "jndi:jdbc/mydbnickname"
  91. instead of
  92. "jdbc/mydbnickname",
  93. then the file created is
  94. u:/workdirectory/jdbc/.bindings
  95. and it looks like this:
  96. mydbnickname/ClassName=com.jolbox.bonecp.BoneCPDataSource
  97. mydbnickname/FactoryName=com.jolbox.bonecp.BoneCPDataSource
  98. mydbnickname/RefAddr/0/Content=oracle.jdbc.OracleDriver
  99. mydbnickname/RefAddr/0/Encoding=String
  100. mydbnickname/RefAddr/0/Type=driverClassName
  101. mydbnickname/RefAddr/1/Content=jdbc:oracle:thin:@myserver:1521:mysid
  102. mydbnickname/RefAddr/1/Encoding=String
  103. mydbnickname/RefAddr/1/Type=jdbcUrl
  104. mydbnickname/RefAddr/2/Content=myusername
  105. mydbnickname/RefAddr/2/Encoding=String
  106. mydbnickname/RefAddr/2/Type=username
  107. mydbnickname/RefAddr/3/Content=mypassword
  108. mydbnickname/RefAddr/3/Encoding=String
  109. mydbnickname/RefAddr/3/Type=password
  110. Rebind (in a JUnit Test)
  111. @BeforeClass
  112. public static void setUpClass() throws Throwable {
  113. final String sMyName = "setUpClass";
  114. try {
  115. if (Boolean.parseBoolean(System.getProperty("test.initialcontext.rebind", "true"))) {
  116. final InitialContext initialContext = new InitialContext();
  117. final String contextName = "jdbc/mydbnickname";
  118. final Reference contextValue = new Reference("com.jolbox.bonecp.BoneCPDataSource", "com.jolbox.bonecp.BoneCPDataSource", null);
  119. contextValue.add(new StringRefAddr("driverClassName", "oracle.jdbc.OracleDriver"));
  120. contextValue.add(new StringRefAddr("jdbcUrl", "jdbc:oracle:thin:@myserver:1521:mysid"));
  121. contextValue.add(new StringRefAddr("username", "myusername"));
  122. contextValue.add(new StringRefAddr("password", "mypassword"));
  123. initialContext.rebind(contextName, contextValue);
  124. }
  125. } catch (final Throwable exception) {
  126. Utils.getInstance().logExceptionStack(logger, Level.ERROR, sMyName, exception);
  127. throw exception;
  128. }
  129. }
  130. Lookup (in production code)
  131. protected Connection getConnection() throws Exception {
  132. Connection result = null;
  133. // "An InitialContext instance is not synchronized against concurrent access by multiple threads"
  134. synchronized (this) {
  135. if (context == null) {
  136. context = new InitialContext();
  137. }
  138. final BoneCPDataSource connectionPool = (BoneCPDataSource) context.lookup("jdbc/mydbnickname");
  139. result = connectionPool.getConnection();
  140. }
  141. return result;
  142. }
  143. CLASSPATH
  144. BoneCP Connection Pool
  145. <classpathentry kind="var" path="JAVA_LIB/bonecp-0.8.0.RELEASE.jar" sourcepath="/JAVA_LIB/bonecp-0.8.0.RELEASE-sources.jar"/>
  146. <classpathentry kind="var" path="JAVA_LIB/slf4j-api-1.7.5.jar" sourcepath="/JAVA_LIB/slf4j-api-1.7.5-sources.jar"/>
  147. <classpathentry kind="var" path="JAVA_LIB/guava-15.0.jar" sourcepath="/JAVA_LIB/guava-15.0-sources.jar"/>
  148. <classpathentry kind="var" path="JAVA_LIB/slf4j-simple-1.7.5.jar" sourcepath="/JAVA_LIB/slf4j-simple-1.7.5-sources.jar"/>
  149. Eclipse JPA (2.5.1)
  150. <classpathentry kind="var" path="JAVA_LIB/javax.persistence-2.1.0.jar"/>
  151. <classpathentry kind="var" path="JAVA_LIB/eclipselink-2.5.1.jar"/>
  152. JNDI
  153. <classpathentry kind="var" path="JAVA_LIB/fscontext.jar"/>
  154. <classpathentry kind="var" path="JAVA_LIB/providerutil.jar"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement