2 Replies Latest reply on Feb 27, 2017 5:01 AM by benboss

    Windup report InitialContext lookup

    benboss

      Hi there,

       

      I'm going to through the mandatory incidents of my Windup report migrating from Jboss 5 to Jboss 7.

       

      So before I made  any changes to our Java scripts I ran the windup tool and it obviously found reference to the datasources from the original oracle-ds.xml file.

       

      Context.lookupOLD.jpg

       

      From oracle-ds.xml

      oracle-ds.xml.jpg

       

       

      So I updated the Java script with what I believe is the correct syntax for the initialContext.lookup for JBoss 7 compliance with reference to my new datasource from my standalone.xml and the Windup tool is still highlighting the syntax as a problem.

      I think initCtx.lookup is ok but I'm new at this. Can I use the following call to the datasource in JBoss 7.1.1 ?  If I need to provide more information , just let me know. Kind regards Ben

       

      try {
      initCtx = new InitialContext();
      DataSource e = null;

      try {
      e = (DataSource)initCtx.lookup("java:jboss/datasources/jdbc/CLIENT/SYSTEM");

       

      Context.lookupNEW.jpg

      From standalone.xml

      standalone.xml.jpg

        • 1. Re: Windup report InitialContext lookup
          andey

          Hi,

           

          Follow the below steps:

           

          Install a JDBC driver as a core module:

           

          1.Create a directory under $JBOSS_HOME/modules. In this example: "$JBOSS_HOME/modules/com/oracle/jdbc/main".

          2.Put the the JDBC driver jar (ojdbc6.jar) in this directory

          3.Create a module configuration file module.xml:

           

          ~~~

          <module xmlns="urn:jboss:module:1.1" name="com.oracle.jdbc">

            <resources>

              <resource-root path="ojdbc6.jar"/>

            </resources>

            <dependencies>

              <module name="javax.api"/>

              <module name="javax.transaction.api"/>

            </dependencies>

          </module>

          ~~~

           

          Note that the jdbc driver jar must contain a META-INF/services/java.sql.Driver text file that specifies the jdbc Driver, otherwise the Java service provider mechanism used to load the driver will not work. From the main/common vendors only Informix does not have this out of the box.

           

           

          Configure a datasource setting in standalone.xml or domain.xml:

           

          You can now edit your standalone(-*).xml or domain(-*).xml to configure a datasource that references this module:

           

          ~~~

            <subsystem xmlns="urn:jboss:domain:datasources:1.2">

              <datasources>

                <datasource jndi-name="java:jboss/datasources/OracleDS" pool-name="OracleDS" enabled="true" use-java-context="true">

                  <connection-url>jdbc:oracle:thin:@myhostname:1521:oracle</connection-url>

                  <driver>oracle</driver>

                  <pool>

                    <min-pool-size>10</min-pool-size>

                    <max-pool-size>20</max-pool-size>

                    <prefill>true</prefill>

                  </pool>

                  <security>

                    <user-name>myuser</user-name>

                    <password>mypass</password>

                  </security>

                  <validation>

                    <validate-on-match>true</validate-on-match>

                    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"></valid-connection-checker>

                    <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"></stale-connection-checker>

                    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"></exception-sorter>

                  </validation>

                </datasource>

                <drivers>

                  <driver name="oracle" module="com.oracle.jdbc">

                    <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>

                  </driver>

                </drivers>

              </datasources>

            </subsystem>

          ~~~

           

          JNDI name for datasource can be configured using java:/xxxx or java:jboss/xxxx.

          The above two JNDI names are supported for datasource in JBoss EAP 6.

          Using the JNDI name java:jboss/xxxx for datasource is the preferred way.

           

           

          Look up daatsource inside Java scripts or JSP:

          ------------------------------------------------------------------------

           

          Connection con=null;

          DataSource ds=null;

            

            try

             {

            Context ctx=new InitialContext();

            ds=(DataSource)ctx.lookup("java:jboss/datasources/OracleDS");

           

                  System.out.println("Got DataSource ds  = " + ds);

                  out.println("Got DataSource : = " + ds);

                  System.out.println("Looking Up an Object in the JNDI tree...");

           

                 con = ds.getConnection();                  

                 out.println("Got Connection  : " + con);

                 System.out.println("Got Connection  : " + con);

           

               } catch(Exception e) {

            e.printStackTrace();

               } finally {

                 try {

                          if (con != null && !con.isClosed()) {

                            con.close();

                           }

                      } finally {

                           con = null;

                      }

                }

          • 2. Re: Windup report InitialContext lookup
            benboss

            Hi,

             

            I think you've answered my question in a round about way. In that syntactically I can use e = (DataSource)initCtx.lookup("java:jboss/datasources/jdbc/CLIENT/SYSTEM");      in my java code.

             

            And that the Windup report is producing a false positive. It would be nice if someone could confirm that. Remember this is jboss AS 7.1.1

             

            Thanks,

             

            try {
                     initCtx = new InitialContext();
                     DataSource e = null;

                     try {
                         e = (DataSource)initCtx.lookup("java:jboss/datasources/jdbc/CLIENT/SYSTEM");

                 
            From standalone.xml

            <datasource jta="true" jndi-name="java:jboss/datasources/jdbc/CLIENT/SYSTEM" pool-name="jdbc/CLIENT/SYSTEM" enabled="true" use-ccm="false">
            <connection-url>jdbc:sqlserver://JMETER:1433;database=######</connection-url>
                    <driver>sqlserver</driver>
                    <security>
              <security-domain>CLIENTSYSTEM</security-domain>
                    </security>