Wednesday, 24 September 2014

EJB 3.0 + websphere 8.5

 
EJB:

EJB stands for Enterprise Java Bean. EJB provides an architecure to develop and deploy component based enterprised applications. Bean will run  in EJB container supported by any application server compliant with J2ee 1.3 standard specification.

Advantages of EJB:


1. Componets can be clustered for high availability of the application.
2. Scalability.
3. transaction management.
4. Security.

All life cyce of EJB managed by container and developer has to focus on business logic.

Simple basic application to show how to deploy the EJB application in websphere 8.5.

1. Creat project called MyComponent in eclipse.

POJO bean class to serve simple request. This bean will return the name as a response.
Annotations to define some EJB features.

@Stateless: This bean is stateless. Dont maintain conversation state.
@Local(NameServiceLocal.class). Local view
@Remote(NameServiceRemote.class).Remote View.
@LocalBean

NameService .java
package com.service.com;
import com.service.com.view.NameServiceLocal;
import com.service.com.view.NameServiceRemote;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;
/**
 * Session Bean implementation class NameService
 */
@Stateless
@Local(NameServiceLocal.class)
@Remote(NameServiceRemote.class)
@LocalBean
public class NameService implements NameServiceRemote, NameServiceLocal
{
    /**
     * Default constructor.
     */
    public NameService()
 {
        // TODO Auto-generated constructor stub
    }
   
public String getName()
{

  return "Prashant Savadi";
 }
}

2.  Create local view for the bean.

package com.service.com.view;
public interface NameServiceLocal
 {
   public String getName();
}
3. Create remote view of the bean

package com.service.com.view;
public interface NameServiceRemote
 {
     public String getName();
}

4. ejb-jar.xml file

<?xml version="1.0" encoding="UTF-8"?><ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
  <display-name>MyComponent</display-name>
 </ejb-jar>



5. Export the the project as JAR file and deploy in server as shown below.

 




6. Generating stub class.

Go to server bin directory and run below command to generate stub class.

Stub will be generated and placed into the JAR file. Keep this JAR file in client project classpath and run the client code.



7.Accessing EJB from client code.

Before running client its required to place below jar in class path.

com.ibm.ws.ejb.thinclient_8.5.0

Below is the client code to look up the EJB which is deployed in the server. After running below code EJB should rerurn the name. Here "compremote" is the JNDI name whcih is configured from server admin console and same can be looked up from the client code.

package com.client;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.service.com.view.NameServiceLocal;
import com.service.com.view.NameServiceRemote;
public class Client {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Properties p = new Properties();
  p.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
  //p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
  p.put(Context.PROVIDER_URL, "corbaloc:iiop:localhost:2813");
  //iiop://localhost:2813/
  //p.put(Context.PROVIDER_URL, "iiop://localhost:2809/");
  //String jndi = "ejblocal:enterprise_app_name/ejb_web_project_name.jar/ejb_name#name.of.local.impl.interface";
  NameServiceRemote ejb=null;
  try {
   InitialContext ctx = new InitialContext(p);
    ctx
    .lookup("compremote");
  //java:global/MyComponent/NameService!com.service.com.view.NameServiceRemote
   Object rejb = (Object) ctx
     .lookup("compremote");

   ejb=(NameServiceRemote)javax.rmi.PortableRemoteObject.narrow(rejb,com.service.com.view.NameServiceRemote.class);
//<terminated>com.client.Client at localhost:57599
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println(ejb.getName());

 }
}

8. Response

"Prashant Savadi"     :)

 

 

 




Sunday, 7 September 2014

Spring LDAP + Apache DS

Software/tools required:

1. LDAP server : Apache Directory Studio 2.0.0
2. IDE :Eclipse Kepler
3. JDK 1.6
4. Spring LDAP core 2.0.2 release
5. MAVEN 4.0.0


Basics of LDAP:

LDAP stands for Light weight Directory Access protocol for accessing directory servers and LDAP server is mainly used by the most of the organisation to maintain the company information like employee details with proper hierarchy.This is very simple example how basically LDAP can be used.

There are different vendors available in market for LDAP server for example Microsoft Active Directory, Novel ,OpenDS and Apache DS etc.

Apache DS :

Installation & Server configuration:

1. Download Apache directory Studio from link  http://directory.apache.org/studio/
2. Install the server
3. Fallow the link for server configuration: http://directory.apache.org/studio/users- guide/apacheds_configuration/configuration_editor_1.5.0_general.html
4. Now start the server. Server should be up and listening to the port : 10389

Connection Configuration:


Adding/Feeding  Data to the server:

Feed the data using UI or upload the data by upload option. Before uploading prepare the data as given below:
https://directory.apache.org/apacheds/basic-ug/2.1.1-adding-entries.html

File format shall be LDIF. LDIF stands for LDAP Data Interchange format.
Example:

# File prashant_savadi.ldif
dn: ou=people,o=sevenSeas
objectclass: organizationalUnit
objectclass: top
ou: people

dn: cn=Prashant Savadi,ou=people,o=sevenSeas
objectclass: inetOrgPerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
cn: Prashant Savadi
description: A pirate captain and Peter Pan's nemesis
sn: Savadi
mail: gsprashant@xyz.com
userpassword: abcdef

 MAVEN 4.0.0:

Add below maven dependency to the POM.xml file.

<dependency>
 <groupId>org.springframework.ldap</groupId>
 <artifactId>spring-ldap-core</artifactId>
 <version>2.0.2.RELEASE</version>
</dependency>

At a minimum, to use Spring LDAP you need:
  • spring-ldap-core (the Spring LDAP library)
  • spring-core (miscellaneous utility classes used internally by the framework)
  • spring-beans (contains interfaces and classes for manipulating Java beans)
  • spring-data-commons (base infrastructure for repository suppport, etc.)
  • slf4j (a simple logging facade, used internally)
  • spring-context

 Spring LDAP:

Before Spring LDAP API Java Naming and Directory Interfaced (JNDI) was used to interact with LDAP based applications.How Spring JDBC simplified the dabase base access in java similary Spring LDAP made easy interaction with LDAP applications.
Pros:
1. No exception handling.
2. No resource handling (opening/closing resource connection).
3. No plumbing code to even achive simple task.

Spring LDAP is designed to simplify LDAP programming in Java. Some of the features provided by the library are:
  • JdbcTemplate-style template simplifications to LDAP programming(LDAPTemplate).
  • JPA/Hibernate-style annotation-based object/directory mapping.
  • Spring Data repository support, including support for QueryDSL.
  • Utilities to simplify building LDAP queries and distinguished names.
  • Proper LDAP connection pooling.
  • Client-side LDAP compensating transaction support.
Below link shall help you understand the SpringLdap API:

http://docs.spring.io/autorepo/docs/spring-ldap/2.0.2.RELEASE/reference/#introduction