Monday, September 08, 2008

TIP: JAX-WS Knowing Client IP (Requester IP)

To find out the client IP (the invoker) in JAX-WS Webservice one will need to use WebServiceContext

   1: import com.sun.xml.ws.transport.Headers;
   2: import javax.annotation.Resource;
   3: import javax.jws.WebMethod;
   4: import javax.jws.WebService;
   5: import javax.xml.ws.WebServiceContext;
   6: import javax.xml.ws.handler.MessageContext; 
   7:  
   8: @WebService
   9: public class XYZService 
  10: { 
  11:  
  12:     @Resource
  13:     WebServiceContext wsContext; 
  14:  
  15:     /**
  16:      * Web service operation
  17:      */
  18:     @WebMethod(operationName = "doOperation")
  19:     public String doOperation() 
  20:     {
  21:         MessageContext mc = wsContext.getMessageContext();
  22:         HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);
  23:         String clientIP = req.getRemoteAddr();
  24:         System.out.println("Client IP = " + clientIP); 
  25:         .....
  26:     } 
  27:  
  28: }

Wednesday, September 03, 2008

Tip: Hibernate Annotations - Default Column Values

If you wanted to set the default value using hibernate annotations, you’ve probably had some difficulties, as it was the case for me. Some posts on the web talk about default values to the members of the Java class. That is, if you declare

   1: class AEntity { 
   2:  
   3:     private Integer count = 3; 
   4:  
   5:     @Column(name = "count", nullable = false) 
   6:     public Integer getCount() {
   7:         return count;
   8:     }
   9: }

you should have the default value set in the database.

Well, this does not seem to work (at least not for me). So I tried the usage of “columndefinition”. This ofcourse is database dependent, since Hibernate specifies the usage of “columndefinition” attribute for database specific declarations. The following will work well with MySQL - the database of choice for my current project:

   1: class AEntity { 
   2:  
   3:     private Integer count = 3; 
   4:  
   5:     @Column(name = "count", nullable = false, columnDefinition = "bigint(20) default 0") 
   6:     public Integer getCount() {
   7:         return count;
   8:     }
   9: }

NOTE that this is database dependent, so use it if your project is db dependent.

Another way to do it would be to set the property value in the default constructor of that entity as follows:

   1: class AEntity { 
   2:  
   3:     private Integer count; 
   4:  
   5:     public AEntity(){
   6:         count=3;
   7:     }
   8:  
   9:     @Column(name = "count", nullable = false)
  10:     public Integer getCount() {
  11:         return count;
  12:     }
  13: }

Remember though, whenever you create an instance of this entity, the property will have that default value and will need to be overwritten by calling its respective setter method.

*Source: another blog

Tuesday, August 26, 2008

Tip: How to convert Oracle BLOB to VARCHAR for temporary viewing - SQL

 

   1: SELECT
   2: utl_raw.cast_to_varchar2
   3: (
   4:     dbms_lob.substr
   5:     (
   6:         BLOB_COLUMN, dbms_lob.getLength(BLOB_COLUMN), 1
   7:     )
   8: ) BLOB_AS_VARCHAR
   9: FROM THE_TABE 

Interesting huh?...

Friday, August 01, 2008

Multi-project with Maven

-- PARENT Application Holder
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=BrokerGateway -DartifactId=BrokerGateway -DpackageName= -Dversion=1.0

# Create a new simple project BrokerGateway inside the workspace with eclipse (From the menu bar, select File >New >Project. Select Simple >Project). Eclipse will create a simple .project-file for your BrokerGateway Project and you should be able to see the pom.xml-file.
# Delete the src-folder and open the pom.xml - file to change the packaging of your parent project to pom

  <packaging>pom</packaging>

GO INSIDE PROJECT FOLDER ON COMMAND LINE AND ISSUE THE FOLLOWING COMMANDS

-- Endpoint Module
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=BrokerGateway.EndpointManager -DartifactId=BrokerGatewayEndpointManager -DpackageName=com.reliable.fbs.brokergateway.endpointmanager -Dversion=1.0

-- Entity Module
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=BrokerGateway.EntityManager -DartifactId=BrokerGatewayEntityManager -DpackageName=com.reliable.fbs.brokergateway.business.entity -Dversion=1.0

-- Util Module
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=BrokerGateway.Utils -DartifactId=BrokerGatewayUtils -DpackageName=com.reliable.fbs.brokergateway.utils -Dversion=1.0

-- Web Module
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=BrokerGateway.Web -DartifactId=BrokerGatewayWeb -DpackageName=com.reliable.fbs.brokergateway.web -Dversion=1.0

===========================================================

# Add the newly created modules to your parent pom.

  <modules>
    <module>guide-ide-eclipse-site</module>
    <module>guide-ide-eclipse-core</module>
    <module>guide-ide-eclipse-module1</module>
  </modules>

# Add the parent to the POMs of the new modules:

  <parent>
  <groupId>BrokerGateway</groupId>
  <artifactId>BrokerGateway</artifactId>
  <version>1.0</version>
  </parent>

# Add dependencies to respective modules from module1 to the core-module:

    <dependency>
      <groupId>BrokerGateway.Utils</groupId>
      <artifactId>BrokerGatewayUtils</artifactId>
      <version>1.0</version>
    </dependency>

===========================================================

mvn install
mvn eclipse:eclipse
mvn site -- to generate the documentation website for the app

===========================================================

Check in your project using the eclipse team support (select from the context menu Team >Share Project). Note: Don not check in the generated eclipse files. If you use CVS you should have a .cvsignore-file with the following entries for each module:

---------------------------------------------
target
.classpath
.project
.wtpmodules
---------------------------------------------

Even the parent project should have this .cvsignore-file. Eclipse will automatically generate a new simple .project-file when you check out the project from the repository.

From now on you have different options to proceed. If you are working on all modules simultaneously and you rather have eclipse project dependencies than binary dependencies, you should set up a new workspace and import all projects form step-by-step/guide-ide-eclipse. Note, you have to delete the .project-file of your parent project before. The result is equals to checking out the whole project from the command line, running mvn eclipse:eclipse and finally importing the projects into your eclipse workspace. In both cases you will be able to synchronize your changes using eclipse.

In case of large projects with many people it can be quite tedious to check out all modules and keep them up to date. Especially if you are only interested in one or two modules. In this case using binary dependencies is much more comfortable. Just check out the modules you want to work on with eclipse and run mvn eclipse:eclipse for each module (see also Maven as an external tool). Of course all referenced artifacts have to be available from your maven repository.

Tip: Formatting Numbers in DisplayTag Columns

Displaytag supports easy display of formatted number columns using the format attribute on <display:column> - however, it’s not really well documented on the Displaytag site. Here’s how to do simple number formatting without requiring a decorator class:

<displaytag:column property="amount" title="$ Amount" format="{0,number,#.##}"/> 

This will display a decimal formatted to a maximum of 2 decimal places!