Saturday, October 20, 2012

Find PID listening specific port

To find which process is listening a specific port, the 3 options below can be used.

netstat -alnp | grep ::80
or
fuser -v 80/tcp
or
lsof -nPi tcp:80

Friday, October 19, 2012

Custom templates for reverse engineering of hibernate tools

Recently I got a problem that when jersey converts java object to json it encountered this exception.

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[46]->jobprep.domain.educationfacility.Director_$$_javassist_2["handler"])


The solution is here. After read this thread, I think it's better to add @JsonAutoDetect(value = JsonMethod.NONE) to each java entity class and @JsonProperty to each property. However I am using hiberate tools to generate java entity automatically so I need to find out how to add these annotations via the reverse engineering.

I found I can use hibernate tool reverse engineering custom templates to achieve this. But I found after I created a folder and copied all .ftl files from the POJO folder in hibernate-tools-x.x.x.jar, it still didn't work. After googling a while I got the final solution.

I need to also copy the DAO folder to the same level. Then it works.



Here is the content PojoTypeDeclaration.ftl which adds @JsonAutoDetect to class

/**
${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
 */
<#include "Ejb3TypeDeclaration.ftl"/>
@${pojo.importType("org.codehaus.jackson.annotate.JsonAutoDetect")}(value = ${pojo.importType("org.codehaus.jackson.annotate.JsonMethod")}.NONE)
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} ${pojo.getExtendsDeclaration()} ${pojo.getImplementsDeclaration()}

And PojoPropertyAccessors.ftl which adds JsonProperty

<#-- // Property accessors -->
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
 <#if pojo.hasFieldJavaDoc(property)>    
    /**       
     * ${pojo.getFieldJavaDoc(property, 4)}
     */
</#if>
    <#include "GetPropertyAnnotation.ftl"/>
    @${pojo.importType("org.codehaus.jackson.annotate.JsonProperty")}
    ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
        return this.${property.name};
    }
    
    ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
        this.${property.name} = ${property.name};
    }
</#if>
</#foreach>




Monday, October 15, 2012

Android R file can't generate

I engaged a wired problem that the R file can't be generated by eclipse even I clean the project a few time. What happened was I broke the project and also the git file so I clone the project from my bitbucket. When I added the git repos and imported the projects, I found the tricky problem.

After googling a while, I hadn't found right solution. After all, I tried my luck by

switching the android SDK version for the project - because for unknown reason the current sdk version is not working for the project.

and it works. I switched it from 4.0.3 to 4.0.0 and right after the eclipse started building the R file. And then I switched it back to 4.0.3 and it seemed back to normal but once I cleaned the project it failed to generate R file again. So I had to stick with 4.0.0. Strange!