Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Wednesday, September 17, 2014

Installing Java on Linux

We had a requirement to install java in Linux machine without using the RPM and below are the steps we have done.I am just putting it here for future reference.

1.    Download the server JRE from oracle.com

2.    Change to the directory in which you want to install.
           For example, to install the software in the /u01/java/ directory, Type:
           cd /u01/java/

3.    Move the .tar.gz archive binary to the current directory.

4.    Unpack the tarball and install Java
           For example,  tar zxvf server-jre-7u65-linux-x64.tar.gz 
           The Java files are installed in a directory called jdk1.7.0_65


    To make this Java installation to be available in a system-wide location such as /usr/bin, you must login as the root user and find the current java installation using command  “$which java”

Remove the existing symbolic link and create new using the below commands.

[soa.admin]$ which java
/usr/bin/java

[soa.admin]$ cd /usr/bin/

To remove the existing symbolic link
[soa.admin]$ rm java

[soa.admin]$ ln -s /u01/java/jdk1.7.0_65/bin/java java

[soa.admin]$ java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

This java version is now available for all the users.


If you would like to set the JAVA_HOME for only for the current session $ export  JAVA_HOME=/u01/java/jdk1.7.0_65

Thursday, March 27, 2014

Java/BPEL Authenticate & Search Active Directory

We recently needed to create a BPEL service to authenticate users in Active Directory and also to search through the AD. For this we used the native LDAP classes in Java to connect to Active directory and embed the code in spring bean and expose as web service in Oracle SOA suite.


AD Authentication

We used the following code in the Spring bean for user authentication.
 public boolean isAuthenticated(String user,String pwd){      
     DirContext ctx = null;  
     boolean result=false;  
     Hashtable env = new Hashtable();  
     String ldapURL = "ldap://myhost1.company.ae:389";  
     env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");  
     env.put(Context.SECURITY_AUTHENTICATION, "simple");  
     env.put(Context.SECURITY_PRINCIPAL, user);  
     env.put(Context.SECURITY_CREDENTIALS,pwd);  
     env.put(Context.PROVIDER_URL, ldapURL);  
     try {  
       ctx = new InitialLdapContext(env, null);  
       result=true;  
     } catch (Exception e) {  
       System.err.println("Invalid login for user: " + user);  
       }finally{  
       try {  
         if(ctx!=null)  
         ctx.close();  
       } catch (NamingException e) {  
         System.err.println("Invalid login for user: " + e);  
       }  
     }  
       return result;  
    }   

And below is the java code to search through the Active directory. Since we have multiple domain controls and the servers hosted in Solaris which is not in the domain we had used the ping functionality to see if the domain control is up before connecting to it.


 package sample.com;  
 import java.io.IOException;  
 import java.net.InetAddress;  
 import java.net.UnknownHostException;  
 import java.util.Hashtable;  
 import javax.naming.ldap.*;  
 import javax.naming.directory.*;  
 import javax.naming.*;  
 public class SearchAD {  
   public static void main(String[] args) {  
     Hashtable env = new Hashtable();  
     String userName ="CN=admin,OU=Application Accounts,DC=company,DC=ae";  
     String password = "******";  
     String ldapURL = getReachableHost();  
     env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");  
     env.put(Context.SECURITY_AUTHENTICATION, "simple");  
     env.put(Context.SECURITY_PRINCIPAL, userName);  
     env.put(Context.SECURITY_CREDENTIALS, password);  
     //Used if LDAPS port is used  
     //   env.put(Context.SECURITY_PROTOCOL,"ssl");  
     //connect to domain controller  
     env.put(Context.PROVIDER_URL, ldapURL);  
     try {  
       // Create the initial directory context  
       DirContext ctx = new InitialLdapContext(env, null);  
       SearchControls searchCtls = new SearchControls();  
       //Array of attributes to be returned  
       String returnedAtts[] ={ "sn", "mail", "cn", "givenName", "title" };  
       searchCtls.setReturningAttributes(returnedAtts);  
       //Provide the search scope  
       searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);  
       //Provide the LDAP filter for search  
       String searchFilter = "(&(sn=Siva*)(mail=*))";  
       //Provide the Base for search  
       String searchBase = "DC=company,DC=ae";  
       // Search for objects using the filter  
       NamingEnumeration results =ctx.search(searchBase, searchFilter, searchCtls);  
       //Iterate through the results  
       while (results.hasMoreElements()) {  
         SearchResult sr = (SearchResult)results.next();  
         System.out.println(">>>" + sr.getName());  
         Attributes attribs = sr.getAttributes();  
         if (attribs != null) {  
           try {  
             System.out.println("surname==>" +  
                       attribs.get("sn").get());  
             System.out.println("firstname==>" +  
                       attribs.get("givenName").get());  
             System.out.println("Email==>" +  
                       attribs.get("mail").get());  
             System.out.println("  cn==>" +  
                       attribs.get("cn").get());  
             System.out.println(" Title==>" +  
                       attribs.get("title").get());  
           } catch (NullPointerException e) {  
             System.out.println("Errors listing attributes: " + e);  
             e.printStackTrace();  
           }  
         }  
       }  
       ctx.close();  
     } catch (NamingException e) {  
       System.err.println("Problem searching directory: " + e);  
       e.printStackTrace();  
     }  
   }  
   public boolean isAuthenticated(String user,String pwd){      
     DirContext ctx = null;  
     boolean result=false;  
     Hashtable env = new Hashtable();  
     String ldapURL = getReachableHost();  
     env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");  
     env.put(Context.SECURITY_AUTHENTICATION, "simple");  
     env.put(Context.SECURITY_PRINCIPAL, user);  
     env.put(Context.SECURITY_CREDENTIALS,pwd);  
     env.put(Context.PROVIDER_URL, ldapURL);  
     try {  
       ctx = new InitialLdapContext(env, null);  
       result=true;  
     } catch (Exception e) {  
       System.err.println("Invalid login for user: " + user);  
       }finally{  
       try {  
         if(ctx!=null)  
         ctx.close();  
       } catch (NamingException e) {  
         System.err.println("Invalid login for user: " + e);  
       }  
     }  
       return result;  
    }   
 //Ping the server to see if alive  
   public static String getReachableHost() {  
     String ldapURL = null;  
     try {  
       InetAddress address = InetAddress.getByName("myhost1.company.ae");  
       if (address.isReachable(3000)) {  
         ldapURL = "ldap://myhost1.company.ae:389";  
       } else {  
         address = InetAddress.getByName("myhost2.company.ae");  
         if (address.isReachable(3000)) {  
           ldapURL = "ldap://myhost2.company.ae:389";  
         } else {  
           address = InetAddress.getByName("myhost3.company.ae");  
           if (address.isReachable(3000)) {  
             ldapURL = "ldap://myhost3.company.ae:389";  
           } else {  
             address = InetAddress.getByName("myhost4.company.ae");  
             if (address.isReachable(3000)) {  
               ldapURL = "ldap://myhost4.company.ae:389";  
             }  
           }  
         }  
       }  
     } catch (UnknownHostException e) {  
       System.err.println("Unable to lookup host");  
     } catch (IOException e) {  
       System.err.println("Unable to reach host");  
     }  
     return ldapURL;  
   }  
 }  

Below are some of the common exceptions thrown by the API.

Invalid AD username or password 

[LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1
javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1


SSL Port instead of HTTP


javax.naming.CommunicationException: simple bind failed: myhost1.company.ae:389 [Root exception is javax.net.ssl.SSLException: java.net.SocketException: Software caused connection abort: recv failed]
javax.naming.CommunicationException: simple bind failed: myhost1.company.ae:389 [Root exception is javax.net.ssl.SSLException: java.net.SocketException: Software caused connection abort: recv failed]

Friday, February 7, 2014

Java Connect to Informatica Data Service using jdbc

We had a requirement to connect to informatica and get the values from the virtual table.Using the below java code we were able to connect and fetch the records.


The Infadsjdbc.jar will need to be kept in the classpath.


 package com.sample;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
 import java.sql.Statement;  
 import java.util.ArrayList;  
 import java.util.List;  
 /**  
 * @author venky  
 *  
 */   
     public class ConnectInfaImpl{   
     static final String DB_URL = "jdbc:informatica:sqlds//@hostname:port?dis=DIS&sqlds=N_Dummy.DS_N_Dummy";  
     static final String USER = "Administrator";  
     static final String PASS = "********";  
     public static void main(String[] args) {  
     Connection conn = null;  
     Statement stmt = null;  
     List al =null;  
     try{  
      //STEP 2: Register JDBC driver  
      Class.forName("com.informatica.ds.sql.jdbcdrv.INFADriver");  
      //STEP 3: Open a connection  
      System.out.println("Connecting to database...");  
       System.out.println("DB_URL==>"+DB_URL);  
       System.out.println("USER==>"+USER);  
       System.out.println("PASS==>"+PASS);  
       System.out.println("Connecting to database...");  
       conn = DriverManager.getConnection(DB_URL,USER,PASS);  
      //STEP 4: Execute a query  
      System.out.println("Creating statement...");  
      stmt = conn.createStatement();  
      String sql;  
      sql = "select * from my_table";  
      ResultSet rs = stmt.executeQuery(sql);  
      //STEP 5: Extract data from result set  
       al =new ArrayList();  
      while(rs.next()){  
            System.out.println("result==:"+rs.next());  
      }  
     }catch(SQLException se){  
      //Handle errors for JDBC  
      se.printStackTrace();  
     }catch(Exception e){  
      //Handle errors for Class.forName  
      e.printStackTrace();  
     }finally{  
      //finally block used to close resources  
      try{  
        if(stmt!=null)  
         stmt.close();  
      }catch(SQLException se2){  
        se2.printStackTrace();  
      }  
      try{  
        if(conn!=null)  
         conn.close();  
      }catch(SQLException se){  
        se.printStackTrace();  
      }  
     }  
     System.out.println("Goodbye!");  
   }  
   }