Java – Database – Get DB Metadata

Background

Wanted to quickly review our DataSource Connection.

Code


   
    private String getClassName(Object obj)
    {

        Class  objClass;
        String strClassName;    
    
        objClass = obj.getClass();
        
        strClassName = objClass.getName();
         
        return (strClassName);
                    
    }


    private void fetchDBMetadata
    (
        PrintWriter out
    )
    {
        
        String strURL;
        String[] arrayOfToken  = null;
        
        int iLen = 0;
        int i =0;
        String strData = null;      
        
        try 
        {
            
            /*
                Create InitialContext
            */          
            objCTX = new InitialContext();

            Object obj = null;
       
            /*
                Lookup specific context
            */                 
            obj = objCTX.lookup
                    (
                        strDBID
                    );

            if (obj instanceof DataSource)
            {               
                objDS = (DataSource) obj;
            }
            else
            {
                
                strClassName = getClassName(obj);
                 
                strLog = "Unexpected Object type returned from context "
                            + "ID " + strDBID
                            + " Object Type " 
                            + strClassName
                            ;
                            
                throw new Exception(strLog);
                
            }     
            
            try
            {

                objDBConn = objDS.getConnection();
                
                objDBMetadata = objDBConn.getMetaData();
                
                strURL = objDBMetadata.getURL();
                
                out.println("URL :- " + strURL + CHAR_CRLF);

                out.println(CHAR_CRLF + CHAR_CRLF);             
                
                int iProp = 0;
                
                String strURLProp;
                
                iProp = strURL.indexOf(";");
                
                //out.println("iProp :- " + iProp + CHAR_CRLF);             
                
                strURLProp = strURL.substring(iProp + 1);
                
                //out.println("URLProp :- " + strURLProp + CHAR_CRLF);
                
                if (strURLProp != null)
                {
                    
                    // get token    
                    arrayOfToken = strURLProp.split(";");
                    
                    // get token's length                       
                    if (arrayOfToken == null)
                    {
                        iLen = -1;
                    }
                    else
                    {
                        iLen = arrayOfToken.length;                 
                    }   
                    
                    strLog = "Number of Properties " 
                                + iLen
                                + CHAR_CRLF
                                ;
                    
                    out.println(strLog);
                    
                    out.println(CHAR_CRLF);                                 
                
                    for (i = 0; i < iLen; i++)
                    {
                        strData = arrayOfToken[i];
                        
                        if (strData != null)
                        {
                            
                            strLog = CHAR_TAB
                                        + i 
                                        + ")"
                                        + " " 
                                        + strData
                                        ;
                            
                            out.println(strLog + CHAR_CRLF);                    
                            
                        }   
                        
                    }   
                        
                } // for i
                
            }
            catch (Exception ex)
            {
                          
                out.println("Exception in fetchMetadata ");
                    
                ex.printStackTrace();
                
                out.println(ex.getMessage());
            
            }
        
      
            
        } 
        catch (SQLException ex) 
        {
            
           System.out.println(ex.getMessage());         
           
           ex.printStackTrace();
                    
           out.println(ex.getMessage());
           
        }
        catch (NamingException ex) 
        {
           System.out.println(ex.getMessage());         
           
           ex.printStackTrace();
           
           out.println(ex.getMessage());
           
        }
        catch (Exception ex) 
        {
           System.out.println(ex.getMessage());         
           
           ex.printStackTrace();
                    
           out.println(ex.getMessage());
           
        }       
        finally 
        {
            
           if (objDBConn != null)
           {
                try
                {
                    objDBConn.close();
                }
                catch (SQLException ex) 
                {
							
					System.out.println(ex.getMessage());         

                    ex.printStackTrace();
					
					out.println(ex.getMessage());

                } 
                
           }
           
       } // finally
   
    } //fetchDBMetaData

Output

Output – StackOverflow2010

Error 404: javax.servlet.UnavailableException: SRVE0202E: Servlet was found, but is corrupted: SRVE0227E:

Background

Playing around with Liberty, but not getting far.

One of the errors that I am seeing is the one posted below.

Error

Error Image

SRVE0202E_20180721_0626PM

Error Text


 Error 404: javax.servlet.UnavailableException: 

SRVE0202E: Servlet [helloWorld]: wasdev.helloWorld was found, but is corrupted: 

SRVE0227E: Check that the class resides in the proper package directory. 

SRVE0228E: Check that the classname has been defined in the server using the proper case and fully qualified package. 

SRVE0229E: Check that the class was transferred to the filesystem using a binary transfer mode. 

SRVE0230E: Check that the class was compiled using the proper case (as defined in the class definition). 

SRVE0231E: Check that the class file was not renamed after it was compiled.

Trouble Shooting

Class File Name & Folder Structure

Here are the steps we took to validate the file:

  1. Checked to ensure that file is indeed present in the war file
    • The War is a simple archive file and was able to use 7-Zip to review the file’s location
      • Package name matches folder structure

Environment

Java Run-time

Task Manager

Using MS Windows Task Manager, reviewed the java run-time.

Task Manager – Details

taskManager_javaw_20180721_0655PM

Task Manager – Process – javaw.exe – Tab – Details
Image

javaw_20180721_0654PM

Image
  1. File Version :- 8.0.1210.13
  2. Product Version :- 8.0.1210.13

Compiler

Java Compiler

javac

Reviewed the java compiler that we are using and noticed that we are using Version 10.

Sample

"C:\Program Files\Java\jdk-10.0.2\bin\javac" -version

Output

javac_Version_20180721_0713PM

Remediation

Java Compiler

Stay with later model Java Compiler and use -release option

Update the script to use the “–release” option.


 >"C:\Program Files\Java\jdk-10.0.2\bin\javac" -cp C:\IBM\WebSphere\Liberty\v18.0.0.2\extract\\wlp\dev\api\spec\com.ibm.websphere.javaee.servlet.3.1_1.0.21.jar;C:\IBM\WebSphere\Liberty\v18.0.0.2\extract\\wlp\dev\api\spec\com.ibm.websphere.javaee.annotation.1.3_1.0.21.jar   --release 8 -d WEB-INF\classes helloWorld.java

Align Java Compiler

Downloaded JDK v8, Installed it, and changed our script from using javac 10 to now use java 8.

Credit

Will have to come back and credit where I got this salient advice from.

As I always say, the whole point of writing is to cite proven sources.

WebSphere – Edition – Liberty ( v18 ) – Installation

Background

It is good to revisit Java J2EE after a long hiatus for me personally.

WebSphere – Liberty

Thankfully IBM avails WebSphere via its Liberty Edition.

Liberty is free and as such it is very accessible.

Version 18.x.y.z

Download

The current version of WebSphere Liberty is v18.0.0.2 and it is available here.

Please download it.

Extract

Extract the downloaded package.

Copy

Copy the extracted files into a neutral location where it will live and be delivered from.

Install

Install Additional features

Here are the additional features that we will be installing:

  1. AdminCenter
AdminCenter
Command
Command – Linux

bin/installUtility install adminCenter-1.0

Command – Windows

bin\installUtility install adminCenter-1.0

Output

adminCenter-1Dot0_20180720_0145PM

Configuration

Security

Server.xml
Security

Please access the server.xml file and add entries that look like what is pasted below.

Server.xml – Security
  1. basicRegistry
    • list each user and password
  2. administrator-role
    • list each user that should be part of the administrator group

    <basicRegistry id='basic'>
      <user name='admin' password='websphere' />
      <user name='guest' password='guest' />
    </basicRegistry>

    <administrator-role>
      <user>admin</user>
    </administrator-role>

    <keyStore 
        id='defaultKeyStore' 
        password='Liberty' 
    />
Features – Enable AdminCenter

     <!-- Enable features -->
    <featureManager>
        <feature>webProfile-8.0</feature>
        
        <!--
            2018-07-20 2:02 PM dadeniji
        -->
        <feature>adminCenter-1.0</feature>
        
    </featureManager>

Control

To control the web server, please launch a command shell and issue commands against the bin folder.

Start

Code

bin\server start
Output

serverStart_20180720_0141PM.png

Stop

Code

bin\server stop
Output

serverStop_20180720_0208PM.png

Web Access

Port Numbers & Host

Server.xml

Get Network Port Numbers and host from server.xml

      <!-- To access this server from a remote client 
       add a host attribute to the following element, 
       e.g. host="*" 
   -->
    <httpEndpoint id="defaultHttpEndpoint"
                  httpPort="9080"
                  httpsPort="9443" 
    />

Username & password

server.xml

Also from server.xml, please get user and password.

    <!-- Define an Administrator and non-Administrator -->
    <basicRegistry id="basic">
      <user name="admin" password="websphere" />
      <user name="guest" password="guest" />
    </basicRegistry>

    <!-- Assign 'admin' to Administrator -->
    <administrator-role>
      <user>admin</user>
    </administrator-role>

Browser

Launch a web browser and enter the URL and Port Number for Admin Center.

As we are logged on locally, please enter http://localhost:9443/AdminCenter.

If trying to connect from remote host, please edit your server.xml.

And, add host=* to the httpEndPoint section.

adminCenter_login_20180720_0212PM.png

 

References

  1. IBM
    • IBM Knowledge Center
      • Home > WebSphere Application Server Liberty base. . .> Administering Liberty using Admin Center
        • Setting up Admin Center
          Link
        • Logging in to Admin Center
          Link