2011年8月1日 星期一

在 eclipse 中開發 web service

jdk 6.0
eclipse ganymede ee
tomcat 6.0
上述工具先裝好

一、
下載 Axis2
http://ws.apache.org/axis2/download/1_4_1/download.cgi
安裝 Axis2
Window>Preference>Web Services>Axis2 Preferences

二、
建立 Dynamic Web Project
建立一個 java application 如下
package com.tdl;

public class HelloWorld {

public String echo(){
return "Hellow Web Service.";
}
}

三、
在上述 HelloWorld 上按右鍵>Web Service>Create Web Service
Web Service Runtime 改成 Axis2
Next>Next>Start Server>Finish

四、
看一下 WebContent 下是不是多了一些檔案
WebContent>axis2-web>*.jsp

五、
在 index.jsp 上按右鍵>Run As>Run on Server>Finish
看到畫面的話就表示已成功配置 web service 了
再點選 Services>HelloWorld
應該會帶出如下的 url 和一份 wsdl 文件檔
http://localhost:8080/bonus/services/HelloWorld?wsdl

六、
再來要建立 Client 端
另外建一個 Dynamic Web Project 在專案名稱上按右鍵
File>New>Other>Web Services>Web Service Client
service definition 填入上述的
http://localhost:8080/bonus/services/HelloWorld?wsdl

Web Service Runtime 改成 Axis2
Next>Finish

七、
再建一個 java application 當作執行檔,範例如下:

package com.tdl;
import com.tdl.HelloWorldStub.Echo;
import com.tdl.HelloWorldStub.EchoResponse;

public class TestClient {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
HelloWorldStub stub = new HelloWorldStub();
HelloWorldStub.Echo stubEcho = new HelloWorldStub.Echo();
EchoResponse returnValue = stub.echo( stubEcho ) ;
System.out.println( "return value = "+ returnValue.get_return() );
}
}

完成後 Run As > Java Application

即可看到結果

實用的 AspectJ

最近用了 「AJDT」 和 「Spring + AspectJ」這兩種 AOP 的方式。

AJDT 用於 Eclipse 內開發,需要撰寫 .aj 檔。

Spring + AspectJ 限用於以 Spring 管理的 bean。
範例下載 https://github.com/luyingzhang/aspectj-tutorial/blob/master/README 

2011年7月5日 星期二

Human Sort

Human Sort (Luxury Sort)

http://www.sorting-algorithms.com/

因為在多數的演算法討論中(可以參考上面網站),沒有看過我將要提到的這種實作方式。所以,我就自行實作,並且和其它的演算法比較,發現可以大幅減少執行時間。所以,在此和大家分享。

我們都知道演算法重視兩個基本要素: 空間和時間,隨著PC記憶體的容量加大,價格更便宜的趨勢之下,我們可以充份的利用空間,以提升執行效能。Human Sort(以下稱此演算法)就是基於此想法下推演出來的。

排序的過程如下:
1.配置一塊連續的陣列。
2.將要排序的數字,置入對應的陣列索引。例如: 數字5置入ordered[5]。
3.移除未配置數字的陣列索引。
4.排序完成。


以下是此演算法的Python實作:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def sort(number):
    gap = len(number)
    ordered = [None] * 100
    for k in range(gap):
        ordered[number[k]]  = number[k] ;
 
    return filter(None, ordered)
# EOF.

以下是此演算法的Scala實作:

/**
 * Human Sort Implement
 * @author Boris Lv
 * @since 2011/06/11
 */
object HumanSort {
def sort(number: List[Int]): Array[Int] = {
 val maxSize = 100
 val gap = number.length
 val all = new Array[Int](maxSize)
 val ordered = Nil
 for(i <- 0 until gap){
   all(number(i)) = number(i)
 }
 all.filter(s => s != 0)
}

    def main(args: Array[String]) {
   val ar = List(6, 2, 8, 5, 1)
   sort(ar).foreach(println)
    }
}

import org.junit._
import Assert._

class Tests {

  @Test def sample() {
    val result = HumanSort.sort( List(6, 2, 8, 5, 1) )
    result.foreach(println)
    assertArrayEquals(Array(1, 2, 5, 6, 8), result )
  }
}


此演算法具備的特點和先決條件有:
1.知道最大數,或者必須假設一個最大數。例如: 1,6,96此三個數排序,因為最大數為96,所以,我們會預先配置0-96的陣列空間。若完全不知道將要排序的數字(也許是動態產生的),則必須假設一個最大數,例如所有數都定在3位數以下,則必須配置0-999的陣列空間。
2.數字不可重複,因為一個陣列放置一個對應的數,但可以加上一些前製或後製作業來改善此點。
3.記憶體要夠大,雖然使用完就可以回收(GC),但排序的瞬間必須有足夠的空間。
4.大量數字排序時效果最顯著。
5.僅適用於正數和零。負數也要另外處理。

但這些先決條件都很容易實作,也不會影響效能太多。


我用 log 的方式,簡單的計算了執行的時間,發現用這種方式可以比所有教科書中的演算法,節省2倍以上的時間。


2011年7月4日 星期一

2011年3月8日 星期二

在 Vim 多行同時插入相同文字

引用 http://stackoverflow.com/questions/253380/how-do-i-insert-text-at-beginning-of-a-multi-line-selection-in-vi-vim
  • Use Ctrl+v to select the first column of text in the lines you want to comment.
  • Then hit 'I' and type the text you want to insert.
  • Then hit 'Esc', wait 1 second and the inserted text will appear on every line.
這個功能在MadEdit中使用更容易,按Alt+2就可以了。但這都是要在連續的幾列之間才能用,更厲害的是Sublime Text 2,可以跳著選,然後真正的多行修改。

2011年3月5日 星期六

在 Vim 使用 MONACO 字型

引用 http://plog.longwin.com.tw/programming/2007/08/14/programmer_best_font_monaco_2007
1 先下載MONACO.TTF到/usr/share/fonts/truetype/
2 執行 fc-cache -f -v
3 .vimrc中加上:
set guifont=Monaco\ 10

Python檔案內容的編碼宣告

# -*- coding: UTF-8 -*-

#encoding=utf-8

在 Vim 下去掉 ^M

引用 http://graphics.csie.ntu.edu.tw/~jonathan/tpd/2008/06/get-rid-of-carrot-m/


:%s/^M$//g

Vim指令01: 取代字串

引用 http://vimregex.com/#substitute

:range s[ubstitute]/pattern/string/cgiI
For each line in the range replace a match of the pattern with the string where:
c
Confirm each substitution
g
Replace all occurrences in the line (without - only first).
i
Ignore case for the pattern.
I
Don't ignore case for the pattern.

:1,5s/java/scala/g
選取第1行到第5行,並將其中所有的java取代成scala

:%s/java/scala/g
選取全檔,並將其中所有的java取代成scala

:%s/[Rr]uby/Python/g
選取全檔,並將其中的Ruby和ruby都取代成Python