Monday, August 29, 2011

Simple Spring

  1. Download spring framework from (http://www.springsource.org/download) and commons-logging-1.1.1 from (http://commons.apache.org/logging/download_logging.cgi)
  2. Unzip the spring-framework-3.1.0.M2-with-docs.zip and commons-logging-1.1.1-src.zip
  3. Import all jar files contained in dist folder and commons-logging-1.1.1.jar into your project.
  4. create Hellobean.java
  • public class HelloBean {
        private String helloWord;

        public void setHelloWord(String helloWord) {
            this.helloWord = helloWord;
        }

        public String getHelloWord() {
            return helloWord;
        }
    }
   5.  create applicationContext.xml
  • <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
        <bean id="helloBean" class="com.cht.paas.springtest.HelloBean">
            <property name="helloWord">
                <value>Hello Hello Testing</value>
            </property>
        </bean>
    </beans>
   6.  create demo java:

  • import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.Resource;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;

    public class SpringDemo {
        public static void main(String[] args) {
            Resource rs = new FileSystemResource("applicationConfig.xml");
            BeanFactory factory = new XmlBeanFactory(rs);
            HelloBean hello = (HelloBean) factory.getBean("helloBean");
            hello.setHelloWord("This is setting hello world");
            System.out.println(hello.getHelloWord());
           
        }
    }
References:
  1. http://caterpillar.onlyfun.net/Gossip/SpringGossip/SpringGossip.html
  2. http://www.springsource.org/download
  3. http://www.ibm.com/developerworks/cn/java/wa-spring1/

Sunday, August 28, 2011

Simple Jersey

  1. Download the jersey.zip file from (http://download.java.net/maven/2/com/sun/jersey/jersey-archive/1.8/jersey-archive-1.8.zip)
  2. nzip jersey-archive-1.8.zip and import all the jar files contained lib folder.
  3. Create Hello.java: 
  • import javax.ws.rs.GET;
  • import javax.ws.rs.Path;
  • import javax.ws.rs.Produces;
  • import javax.ws.rs.core.MediaType;
  •  
  • @Path("/hello")
  • public class Hello {
  •     // This method is called if TEXT_PLAIN is request
  •     @GET
  •     @Produces(MediaType.TEXT_PLAIN)
  •     public String sayPlainTextHello() {
  •         return "Hello World";
  •     }
  •     // This method is called if XML is request
  •     @GET
  •     @Produces(MediaType.TEXT_XML)
  •     public String sayXMLHello() {
  •         return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  •     }
  •     // This method is called if HTML is request
  •     @GET
  •     @Produces(MediaType.TEXT_HTML)
  •     public String sayHtmlHello() {
  •         return "<html> " + "<title>" + "Hello World" + "</title>"
  •                 + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  •     }
  • }
4. In web.xml, add the following context:
  • <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <display-name>JerseyTest</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>

        <display-name>Jersey Service</display-name>

        <servlet>
            <servlet-name>Jersey REST Service</servlet-name>
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>com.cht.paas.jersey</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Jersey REST Service</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>

    </web-app>
5. start your tomcat server and enter the url:  http://localhost:8080/JerseyTest/rest/hello

References:
  1. http://www.vogella.de/articles/REST/article.html#first_project
  2. http://jersey.java.net/nonav/documentation/latest/user-guide.html#chapter_deps

Friday, August 26, 2011

Easy to monitor CPU, memory, disk, and network states using SIGAR

  1. Firstly, download the SIGAR API (e.g., hyperic-sigar-1.6.4.zip) from http://support.hyperic.com/display/SIGAR/Home
  2. After unzipping hyperic-sigar-1.6.4, you can find two required folders (i.e., include and lib).
  3. You have to put these folders into your workspace, and import these jar files. 
    • the lib folder and include folder should be put in the same level (e.g., workspace/projectName/). 
  4. Use the following simple code, you can get system performance: 
import org.hyperic.sigar.Cpu;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.NetStat;
import org.hyperic.sigar.Sigar;

public class PerformanceMonitor {

    private static Sigar sigar = new Sigar();

    public PerformanceMonitor() {
    }
    public static void main(String[] args) throws Exception {

        // For Disk
        System.out.println("-----Disk Info-----");
        FileSystem fileSystemList[] = sigar.getFileSystemList();
        for (int f = 0; f < fileSystemList.length; f++) {
            String devName = fileSystemList[f].getDevName();
            int devType = fileSystemList[f].getType();
            //Only get the physical disk
            if (devType == FileSystem.TYPE_LOCAL_DISK) {
                System.out.println("Disk " + devName);
                System.out.println(sigar.getDirUsage(devName));
                System.out.println(sigar.getDiskUsage(devName));
                System.out.println(sigar.getFileSystemUsage(devName));
            }
        }

        // For CPU
        System.out.println("-----CPU Info-----");
        System.out.println(sigar.getCpuPerc());
        System.out.println(sigar.getCpu());
        CpuInfo cpuInfoList[] = sigar.getCpuInfoList();
        Cpu cpuList[] = sigar.getCpuList();
        for (int c = 0; c < cpuInfoList.length; c++) {
            System.out.println("CPU " + c);
            System.out.println(cpuInfoList[c].toString());
            System.out.println(cpuList[c].toString());
        }

        // For Memory
        System.out.println("-----Memory Info-----");
        System.out.println(sigar.getMem());

        // For Network
        System.out.println("-----Network Info-----");
        System.out.println(sigar.getNetInfo());
        NetStat thisNetState = sigar.getNetStat();
        System.out.println("in-bound " + thisNetState.getAllInboundTotal()
                + " out-bound " + thisNetState.getAllOutboundTotal());
    }
}


Thursday, August 25, 2011

How to get project name under Tomcat server

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetPathTest {
    private String webApps    = "webapps";
    private String webINF    = "WEB-INF";

    public String getClassPath(){
        return this.getClass().getClassLoader().getResource(File.separator).getPath();
    }
   
    public String getProjectName(){
        String classPath = getClassPath();
        Pattern pattern = Pattern.compile(webApps+".*"+webINF);
        Matcher matcher = pattern.matcher(classPath);
        String projectName = null ;
        while (matcher.find()) {
            projectName = matcher.group();
            projectName = projectName.substring(webApps.length()+1,projectName.length()-(webINF.length()+1));
        }
        if (projectName==null) return "null";
        return projectName;
    }
}