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!

Friday, May 18, 2012

Git+Dropbox always dirty

Solve this issue by just simply set the
filemode = false
for the config of git.

If you are using eclipse+EGit

Go
->GitRepositoryView->Find your Git project->Working Directory->.git->config
You won't miss it.

Wednesday, April 25, 2012

Add basic authendication for Jetty

1. In web.xml, it needs to add this
 
 
  
   api
   /*
  
  
   api
  
 
This config indicates that all resources need ROLE api to access.

2. In the jetty folder, find start.ini and make sure the line like this is not commented. Of course to make this realm config right.
etc/jetty-example.xml
3. Go into your etc/jetty-example.xml to make the xml similar like this

    
      
        
          hw xxx Realm
          /etc/realm.properties
          0
        
      
    

4. Go into your etc/realm.properties file to set up realm configuration.

Also these two links are very helpful.
http://docs.codehaus.org/display/JETTY/Realms
http://docs.codehaus.org/display/JETTY/JAAS Recently I found another problem with realm with jetty 8.1.7. When setup as above, the browser does not popup the authentication window but keep reporting error "This web page has a redirect loop" by Chrome. After a struggling by comparing the jetty xml files I found the issue was caused by this section in contenxts/test.xml

      
        true
      

Just comment out this section, the issue was solved.

Quit ssh with out killing running process

Since I need to keep start.jar of jetty running on EC2. I need to quit the ssh without killing the current running jetty process. There is a tool called "screen" can do this easily.

Follow the steps below
1.
screen
2.
sudo java -jar start.jar
3.
ctrl+a and then ctrl+d
4.quit ssh.

Tuesday, March 27, 2012

Sudo xxx complaining command not found

Problem:
Just installed doctrine. I found if I use command "doctrine" straight away it works but not working with sudo. When I typed in
sudo doctrine
it complains
sudo: doctrine: Command not found
Then I tried to use full path of doctrine with sudo which worked. So I think this should be caused by that sudo command does not use the personal path environment.

Using full qualified path every time is annoying.

Solved:
Create a file at
~/.bash_aliases 
with content
alias sudo='sudo env PATH=$PATH $@'
Logout and login again.

Sunday, March 25, 2012

phpunit with Netbeans on Ubuntu

As a newbie to PHP I got lots of headache on setting up phpunit. It is a little tricky to setup phpunit on Ubuntu since the documentation about this is not accurate. Make some notes here for the future references.

To install phpunit, the only way is to use PEAR. However I got annoying errors during the process.

1. apt-get does not install latest Pear, it only install the PEAR up to verision 1.9.2 which will be complained then when you are installing phpunit. Use this,
$ wget http://pear.php.net/go-pear.phar
$ php go-pear.phar
2. Then install phpunit
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
3. Setup netbeans for phpunit
The right path of phpunit is /home/xxx/pear/bin/phpunit not /usr/lib/php/PHPUnit as documented on neatbeans official website.

Thursday, March 8, 2012

Remote access MySQL

Recently I come cross MySQL which I have not used much before. To connect it on localhost is without any issue till I want to move the MySQL to my EC2 instance.

To make the magic happen, 2 things need to check.

1. mysql only allows localhost to access by default.
If this step is missing the error below will happen.
ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx'
Fixing:

open mysql config file by    sudo nano /etc/mysql/my.cnf comment out the line    bind-address=127.0.0.1 and
skip-networking if existing
Or we can specify a ip say 10.0.0.2 for bind-address as a result only this ip 10.0.0.2 can access mysql. And only one ip can be bound!


2. Root user can not be used to log in remotely by default.
If this step is missing the error below will happen.
ERROR 1130 (HY000): Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server
Fixing:

Make sure to modify root user's privileges to allow remote user login mysql as root user. Don't recommend this way for security reasons. A better is to create another user and enable it to access mysql remotely.

Monday, March 5, 2012

Apache Virtual Hosts - permissions

See this article
https://articles.slicehost.com/2007/9/18/apache-virtual-hosts-permissions

HOWTO: Install xdebug for PHP5

Here is a mini-howto in how to install the xdebug extension with PHP5 in Ubuntu 7.04

This will install xdebug 2.0 (or whatever is latest version in PEAR repository when you try this).

It is assumed you have Apache2 + PHP5 working already.

sudo apt-get install php5-dev php-pear

Now install xdebug thru PECL.

sudo pecl install xdebug

Now we need to find where xdebug.so went (the compiled module)

martin@martin-dev:/$ find / -name 'xdebug.so' /dev/null /usr/lib/php5/20060613/xdebug.so

Then edit php.ini:

sudo gedit /etc/php/apache2/php.ini

Add the following line:

zend_extension="/usr/lib/php5/20060613/xdebug.so"

Then restart apache for changes to take effect

sudo /etc/init.d/apache2 restart

Check phpinfo() to make sure the extension is loaded correctly. The following line should have been appended to the copyright lines:

with Xdebug v2.0.0, Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, by Derick Rethans

For more information about xdebug configuration and usage, please read the official documentation, found here: http://xdebug.org/docs/

check http://ubuntuforums.org/showthread.php?t=525257