2012年10月23日 星期二

getResource() function with classpath

1. Why use getResource() function ? 
    Sometimes there is a need for you to get file, but in java the constructor of File class need the absolute path, but it is depends on the machine, it will be a problem when you didn't know the actual parent path in other computer.
    Therefore, you need a method supporting defining the different root staring path, and can get the file. That is the answer of the  getResource function.


2. where is the relevant searching path in getResource()?
  1.  package as the staring path
  2.  classpath as the staring path

3. Example
File file3 = new File(Test.class.getResource("file3.txt").getFile())



Reference:
http://gavin-chen.iteye.com/blog/261151
 

2012年10月12日 星期五

記憶體漏失 Memory Leak

記憶體漏失 Memory Leak


Memory Leak造成的原因是某個被配置(allocated)的記憶體無法在被參照(referenced),也無法被釋放(released);那塊被配置的 記憶體就有如記憶體孤兒般,無法被系統再使用,所以要看一個程式有否Memory Leak,很簡單的方法就是去看作業系統(For standalone application)或是Application Server(如果是Web Application)的實體記憶體使用圖,如果隨著時間增加,記憶體的使用量呈現明顯增加的趨勢,這個程式就極有可能有潛在的Memory Leak問題。
 
Memory Leak在C/C++語言中是很常見的人為過失,因為C/C++並沒有自動Garbage Collector (垃圾收集器)的機制,程式設計師必需在使用完資源後,人為釋放資源。雖然在Java、C#有自動Garbage Collector的機制,Memory Leak的機會大幅下降,但仍然不能完全倖免,只要程式設計師不小心仍然會造成Memory Leak

Reference
 Memory Leak








rhino for print and readln function

1. what is rhino ??


Rhino is a javascript interpreter which use java language to develop.
In rhino you can import java package and call java package.

2. rhino shell

you can enter js, and type help() to view the function.

3. rhino print

can use print("expression") function;

4. rhino input





var readln = (typeof readline === 'function') ? (readline) : (function() {
     importPackage(java.io);
     importPackage(java.lang);
     var stdin = new BufferedReader(new InputStreamReader(System['in']));

     return function() {
         return String(stdin.readLine());  // Read line, 
     };                                    // force to JavaScript String
 }());
 
 
 
Reference:
 input
 output



2012年10月2日 星期二

Newline thransform Linux \n to \r\n



Command:
sed -e 's/$/\r/'



Reference:
http://superuser.com/questions/419865/how-can-i-replace-all-r-n-with-n-using-either-komodo-edit-on-osx-snow-leopard

2012年9月19日 星期三

Program Design and develop

1.  Version control
  • use date to build a version
  • write a version files


2.  Error message
  • use log4j to record different error message


3. Properties files
  • we can set different files such as project or test




log4j


Writing step:

a. write a log4j.properties in your package
{code}
log4j.rootLogger=Info, A1


# A1 is set to be a ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# %d is date and it's format
# %p is output the log priority info DEBUG INFO WARN ERROR FATAL
# %c is class name
# %l is line position
# %m is the message in contents
# %n is new line
# output format
#[12/09/20 09:26:10][DEBUG][HelloWorld-14] Sample debug message
#[12/09/20 09:26:10][INFO][HelloWorld-15] Sample info message
#[12/09/20 09:26:10][WARN][HelloWorld-16] Sample warn message
log4j.appender.A1.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss}][%p][%C-%L] %m%n
{code}

b. write a class call hello word
{code}
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;


public class HelloWorld {
   
    static final Logger logger = Logger.getLogger(HelloWorld.class);

    // Reference http://werdna1222coldcodes.blogspot.tw/2009/11/javalog4j.html
    public static void main(String[] args) {
            // 會自動設定
            //BasicConfigurator.configure();
            System.out.println("Start Log4j test");
            logger.debug("Sample debug message");
            logger.info("Sample info message");
            logger.warn("Sample warn message");
            logger.error("Sample error message");
            logger.fatal("Sample fatal message");
    }
    
}

{code}

c. Reference
http://werdna1222coldcodes.blogspot.tw/2009/11/javalog4j.html

AJP Introduction

AJP is the apache and tomcat community method.


English:
http://www.mulesoft.com/tomcat-connector

Chinese:
http://tc.itkee.com/sysapp/detail-16d3.html
http://puremonkey2010.blogspot.tw/2010/09/linux-apachetomcat-tomcat-worker.html

Office:

http://tomcat.apache.org/tomcat-4.0-doc/config/ajp.html