Thursday, July 19, 2012

Maven java project archetype


If you use maven, you like to use archetype to get all benefits of maven.
To create maven java project, you can use below command below.

It's an example to create sample archetype.

mvn archetype:generate -DgroupId=com.example -DartifactId=test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false




Then you can simply go under test directory and run below command
mvn eclipse:eclipse


After that, simply import this project into eclipse



Monday, June 18, 2012

how to find byte code class file JDK compiled java version

you can find out the byte code class version.

On Linux, Mac OS X or Windows with Cygwin installed, the file(1) command knows the class version. Extract a class from a jar and use file to identify it:

$ jar xf log4j-1.2.15.jar
$ file ./org/apache/log4j/Appender.class
./org/apache/log4j/Appender.class: compiled Java class data, version 45.3

A different class version, for example:

root@mypc:/merchant-sample/classes/com/paypal/core$ file SSLUtil.class 
SSLUtil.class: compiled Java class data, version 50.0 (Java 1.6)


The class version major number corresponds to the following Java JDK versions:

46 = Java 1.2
47 = Java 1.3
48 = Java 1.4
49 = Java 5
50 = Java 6
51 = Java 7

Alternatively, you can make a simple test class that can identify class version.
import java.io.*;
 
public class Test {
    public static void main(String[] args) throws IOException {
        checkClassVersion("Class path you want to know");
    }                      
 
    private static void checkClassVersion(String filename)
        throws IOException
    {
        DataInputStream in = new DataInputStream
         (new FileInputStream(filename));
 
        int magic = in.readInt();
        if(magic != 0xcafebabe) {
          System.out.println(filename + " is not a valid class!");;
        }
        int minor = in.readUnsignedShort();
        int major = in.readUnsignedShort();
        System.out.println(filename + ": " + major + " . " + minor);
        in.close();
    }
}


Thursday, June 14, 2012

Configure Proxy server bypass or pass

When you work in enterprise environment, you will most likely need to use proxy server either you like or not.

I have suffered for long time because proxy setting and I had to have customized proxy bypass script due to development.

I would like to share my snippet. I hope it helps.

Ubuntu apt-get Proxy configuration. For more detail




Maven test proxy setting



Eclipse proxy set up for JRE/JDK



Eclipse Network Proxy configuration for installing new sofware or update plugins



Eclipse Tomcat proxy set up
add below text
 -Dhttp.proxyHost="106.101.91.4" -Dhttp.proxyPort="8080" -Dhttps.proxyHost="106.101.91.4" -Dhttps.proxyPort="8080"



Internet Explorer Proxy configuration



Eclipse Java application Proxy configuration



Ubuntu Proxy for chrome browser



Firefox Proxy configuration



Proxy Automate Script(PAC) sample

/*************************
Configuration Example path

For IE on Windows
file://d:\proxy.pac

For FF on Windows
file:///d://proxy.pac

For FF on Ubuntu
file:///home/proxy.pac
*************************/
var PROXY1 = "PROXY 123.123.123.1:8080";
var PROXYY2 = "PROXY 123.123.123.2:8080";

var DIRECT = "DIRECT";
var regexpr_facebook = /^[.a-zA-Z0-9-]*.facebook.com/;
var myip = myIpAddress();

//url = http://www.yahoo.com/dkdkd/allld.jpg
//host = www.yahoo.com
function FindProxyForURL(url, host){

 //For Home AP access ( If Source IP starts with 192.168.x.x) bypass Proxy
 if (isInNet(myip, "192.168.0.0", "255.255.0.0")) {
  return DIRECT;
 //your local computer bypass Proxy
 }else if (isInNet(host, "127.0.0.1", "255.255.255.0") || host=="localhost") {
  return DIRECT;
 //C class subnet mask by pass Proxy(it means all IPs starts with 111.111.111 will bypass
 }else if (isInNet(host, "111.111.111.xxx", "255.255.255.0") ) {
  return DIRECT;
 //When you need to access from host file definition IPs
 }else if (host=="devsite"|| host=="mytest") {
  return DIRECT;
 //if you need proxy for specific ip ranges ( Domain will be resolved by DNS ang get IP)
 }else if (isInNet(myip, "106.101.7.93", "255.255.255.0") ) {
  return PROXY1;
 //if you need proxy for specific domain address
 }else if (regexpr_facebook.test(host)){
  return PROXYY2;
 }else{
  return PROXY1;
 }
}

Wednesday, May 30, 2012

Set System property on Maven pom.xml for test

Hi, I made maven test class that makes internet connection like Soap, http connection so on that verifies all test is successful. but, my local internet connection requires Proxy server configuration. If I run it on my computer, it won't run because I get connection refused error. If I set system property on my java source, it is not pretty and I might need to change source code when I deploy on production level.

There is the solution :
I need to add maven-surefire-plugin for setting system property on pom.xml file. That is only applies for test classes system property. You can set skip test if you change value of skipTests element true/false below

<project ...>
 <dependencies>
 ...
 </dependencies>

 <build>
    <plugins>
       <plugin> ....      </plugin>

       <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.12</version>
  <configuration>
    <skipTests>true</skipTests>
    <systemPropertyVariables>
      <http.proxyHost>106.101.5.61</http.proxyHost>
      <http.proxyPort>8080</http.proxyPort>
    </systemPropertyVariables>
  </configuration>
       </plugin>
     </plugins>  
 </build>
</project>


For more detail please refer maven surefire plugin document

Friday, May 4, 2012

Spring 3 Task Execution and Scheduling


I have downloaded Spring3 and Quartz and Spring batch Framework.
After running a simple example, It was not easy, not working well with autowired annotation and many things to learn before use giant functions. currently, Spring batch depend on spring 2.5 that I am not sure, it will work well with Spring 3.1
It is also causing to make big packaging jar/war file

I just wanted to make a very simple batch program that I can use based on SpringFramwork which can autowire beans and DB resources.
I decide to use Spring Task. It just need Spring framework, nothing else
It is extreamly simple and works perfect as I expected.

Just type 3 more line on you spring xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="monitor"/>

<mvc:annotation-driven />

<!--  Omit all the WEB MVC Handler and View Resolver Configuration -->


<!--  Scheduler define start -->
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

<task:executor id="myExecutor" pool-size="5"/>

<task:scheduler id="myScheduler" pool-size="10"/>
<!--  Scheduler define End -->

</beans>

Make one Class that is Component annotated. The Class must be in base-package or sub package.
and make method that is Scheduled annotated with Cron Expression.
That's it. that it will run every 5 minutes from 2 min of the hour.
You can autowire beasn which you like.

package monitor.batch;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class HarvestHttpStatusJob {

 private final Log logger = LogFactory.getLog(this.getClass());

 //To-do  @Autowired beans as usual you need.

 @Scheduled(cron = "0 2/5 * * * *")
 public void harvestStatus() {

  logger.info(this.getClass().getName() + " Start");

  //To do Implement your code

  logger.info(this.getClass().getName() + " Finish");
 }

}

For more Detail Information. Please refer below document from SpringFramwork.
http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/html/scheduling.html

If you are interested in Spring batch and quartz, Please refer below link
Spring batch and quartz



Use of Apache commons configuration in Spring framework

I can get benefit if I use Apache commons configuration.
The reason is

I can choose plain text property file or xml based property file. but, mostly better to use xml based property
I can use UTF-8 Encoding, so that I can type most of language characters.
I can set same key name to get list or array or collection so on. So, I can simply get it and run looping clause.
I can get property value based on hierarchy.

I wanted to use on Spring framework. I just set one Spring bean
※ it worked in Spring3, I guess it will work lower version of Spring as well.

 <bean id="xmlConfig" class="org.apache.commons.configuration.XMLConfiguration">
  <constructor-arg type="java.lang.String">
   <value>commons-config.xml</value>
  </constructor-arg>
 </bean>


commons-config.xml file, you can change root element "configuration" as you like

<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
 <countries>
  <country>
   <code>gbr</code>
  </country>
  <country>
   <code>ger</code>
  </country>
  <country>
   <code>ita</code>
  </country>
  <country>
   <code>aut</code>
  </country>
  <country>
   <code>che</code>
  </country>
  <country>
   <code>chfr</code>
  </country>
  <country>
   <code>swe</code>
  </country>
  <country>
   <code>esp</code>
  </country>  
 </countries>
</configuration>

Make a Spring controller and getStringArray from property
package monitor.controller;

import java.util.List;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

 private final Log logger = LogFactory.getLog(this.getClass());

 @Autowired private XMLConfiguration config;

 @RequestMapping(value = { "/", "/index" })
 public ModelAndView index() {

  ModelAndView mnv = new ModelAndView();
  String[] countries = config.getStringArray("countries.country.code");
  mnv.addObject("countries", countries);

  mnv.setViewName("index");

  return mnv;
 }

}

Thursday, May 3, 2012

How to configure apt-get command to let work behind proxy on Ubuntu 11

apt-get doesn't send packet via system proxy which you might have set in system setting, you need to put it manually.It require to create one file on "/etc/apt/apt.conf" You just create file and type 3 lines and save it, that's it.
/etc/apt$ sudo vi apt.conf

1.type in below
Acquire::http::proxy "http://[[user][:pass]@]host[:port]/";
Acquire::ftp::proxy "ftp://[[user][:pass]@]host[:port]/";
Acquire::https::proxy "https://[[user][:pass]@]host[:port]/";

Actual example from my configuration
Acquire::http::proxy "http://192.168.0.1:8080/";
Acquire::ftp::proxy "ftp://192.168.0.1:8080/";  
Acquire::https::proxy "https://192.168.0.1:8080/";

2.save it, if you don't feel comfortable to use vi, you can use any text editor.
:wq!

※More detail information
man apt.conf