jueves, 26 de abril de 2012

postgres chkpass on hibernate (2)

In my last post, I've created a database with a chkpass field, in this post, we will create the hibernate project that will be connected to it.
In this project I'm using eclipse v 3.7.1 and the first step will be to create an EJB Project.
Also, you will need to add the jpa project facet to the project. These configurations can be found in the same eclipse documentation site, or you might have different preferences for your GUI and a different method to setup the project needs, basically, as you can see we need an EJB project with capabilities for Java Persistence Api.

The Web Service Session Bean

Figure 1: Creating a Session Bean as external interface.

First component that we will add is the interface that will be published as a web service to the world and give us the options to test that we are doing a good job. We will create a new Session Bean in our project with the following properties:

  • Project: The selected project name, mine is "pgIdentityManager". This will be left as the default values provided by the wizard.
  • Source folder: The ejbModule source code folder in the selected project. This will be left as the default values provided by the wizard.
  • Java package: The package where the components will be grouped, in my case, I'm selecting: com.dtorres.ejb
  • Class name: The name of the class that will manage the requests for our service, I'm selecting: PgIdentityManager

Please select the Remote business interface that will publish our ejb to the container world. Figure 1 shows how these properties looks like in the eclipse wizard. Another property that I will add in this wizard is the mappedName for my sessionBean, it will be "pgIdentityManager". These configuration steps in the eclipse's create session bean wizard, will give you the PgIdentityManager class like this:

package com.dtorres.ejb;
import javax.ejb.Stateless;
/**
  * Session Bean implementation class PgIdentityManager
  */
@Stateless(mappedName = "pgIdentityManager")
public class PgIdentityManager implements PgIdentityManagerRemote {
  /**
   * Default constructor.
   */
  public PgIdentityManager() {
    // TODO Auto-generated constructor stub
  }
}

Also we will see the PgIdentityManagerRemote interface in our destination package. Next, we will create some mock stuff to be sure that we will be able to test our service.

The web service operations

Basically, the operations that we will publish for our interface will be:

  • createUser
  • getUser
  • getAllUsers
  • deleteUser
  • resetPassword

In order to start walking to this goal, first we will create our own exception type that will be used to communicate the unimplemented method message to our clients, later, this exception will communicate any system failure too, in order to create our custom exception, we will create a class PgIdentityException in a new package that we will create as com.dtorres.ejb.exceptions. Make the PgIdentityException class extend from java.lang.Exception and override it's constructors, your code will look like this:

package com.dtorres.ejb.exceptions;
public class PgIdentityException extends Exception {
  private static final long serialVersionUID = 1L;
  public PgIdentityException(String message) {
    super(message);
  }
  public PgIdentityException(Throwable throwable) {
    super(throwable);
  }
  public PgIdentityException(String message, Throwable throwable) {
    super(message, throwable);
  }
}

Next we create some mocks for these operations in our PgIdentityManagerRemote interface:

package com.dtorres.ejb;
import javax.ejb.Remote;
import com.dtorres.ejb.exceptions.PgIdentityException;
@Remote
public interface PgIdentityManagerRemote {
  String createUser(String userName, String password) throws PgIdentityException;
  String getUser(String userName) throws PgIdentityException;
  String getAllUsers() throws PgIdentityException;
  String deleteUser(Long userId) throws PgIdentityException;
  String resetPassword(String userName, String newPassword) throws PgIdentityException;
}

For now, we will work in give some mock functionality with these methods, later we will modify the getUser(String userName) and the getAllUsers() methods to return a more appropriate result type.
After this change, our PgIdentityManager class will complaint about the implementation of the remote interface, we will need to implement each method in there. Implement the remote interface methods in the PgIdentityManager class, throwing in each implemented method the PgIdentityException with the message: "Unimplemented method in service", your code will look like this:

package com.dtorres.ejb;
import javax.ejb.Stateless;
import com.dtorres.ejb.exceptions.PgIdentityException;
/**
 * Session Bean implementation class PgIdentityManager
 */
@Stateless(mappedName = "pgIdentityManager")
public class PgIdentityManager implements PgIdentityManagerRemote {
  @Override
  public String createUser(String userName, String password)
   throws PgIdentityException {
    throw new PgIdentityException("Unimplemented method in service");
  }
  @Override
  public String getUser(String userName) throws PgIdentityException {
    throw new PgIdentityException("Unimplemented method in service");
  }
  @Override
  public String getAllUsers() throws PgIdentityException {
    throw new PgIdentityException("Unimplemented method in service");
  }
  @Override
  public String deleteUser(Long userId) throws PgIdentityException {
    throw new PgIdentityException("Unimplemented method in service");
  }
  @Override
  public String resetPassword(String userName, String newPassword)
   throws PgIdentityException {
    throw new PgIdentityException("Unimplemented method in service");
  }
}

Finally we will instruct the ejb to be published as a web service, for that we will use the @WebService, @WebMethod and @WebParam annotations.
First, we will annotate the class as @WebService this way:

@Stateless(mappedName = "pgIdentityManager")
@WebService
public class PgIdentityManager implements PgIdentityManagerRemote {
...

Next we will annotate each method as @WebMethod and it's parameters as @WebParam as in this example:

@WebMethod
public String createUser(@WebParam(name="userName") String userName, @WebParam(name="password") String password)
...

Do this with all methods in the PgIdentityManager class, then publish to your web server. In my case, I'm using JBOSS 5.1, I will have to export the project as a EJB Jar file and deploy it inside my $SERVER/deploy folder.

Testing The Web Wervice

Figure 2: Published wsdl in Jboss Server.

We will use SOAP-UI to test our web service, in order to do that, I can see my published wsdl in my JBOSS Server as in figure 2. We will copy the link to our published wsdl and use it to create a SOAP-UI project or your preferred SOAP Services client. When you consume any of the methods of this web service, you will receive the following response:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
 <env:Header/>
 <env:Body>
  <env:Fault>
   <faultcode>env:Server</faultcode>
   <faultstring>Unimplemented method in service</faultstring>
   <detail>
    <ns2:PgIdentityException xmlns:ns2="http://ejb.dtorres.com/">
     <message>Unimplemented method in service</message>
    </ns2:PgIdentityException>
   </detail>
  </env:Fault>
 </env:Body>
</env:Envelope>

In our next post we will create and map the persistence entities that will make the chkpass field work and our service take some action. Look again later. Cheers.

No hay comentarios.:

Publicar un comentario