2009年12月31日 星期四

linux 下做兩個資料夾的比對

diff 程式可以對兩個檔案檢查之間的相異點。
但同時也可以檢查兩個資料夾下所有檔案的相異點

diff -ruN folder1/ folder2/

ivy 指定設定檔位置

ivy 是一個和 ant 配合得很好的 lib 管理工具,簡單安裝及設定後,只要在 ant 的 build.xml 裡加上一個 "resolve" target
target 裡使用 ivy 提供的 <ivy:retrieve> ant task 並配合 ivy.xml 條列出所需 lib 的組織、名稱、版本
就可以由 maven repository 取得專案所需的 lib
但是 ivy 初始預設的設定往往不足以應付專案所需,例如需要對 ivy 另加不同的 repository 站點等等…
這些特殊設定必需由 ivysettings.xml 設定檔來完成,在預設的情型下,只要在 build.xml 同目錄下
加入檔名為 ivysettings.xml 的設定檔,ivy 就會使用這個設定檔覆蓋掉 ivy.jar 裡預設提供的 ivysettings.xml
但如果要將 ivysettings.xml 改名或移到別的目錄,就必須增加 <ivy:settings> task 如以下列範例:

<!-- resolve -->
<target name="resolve">
<ivy:settings id="my-ivysettings" file="ivy/myivysettings.xml" />
<ivy:retrieve settingsref="my-ivysettings" />
</target>

2009年12月29日 星期二

透過 ant 和 vbscript 合作建立 windows 捷徑檔

先以 vbscript 寫一段建立捷徑的程式

// 讀取參數
Set args = WScript.Arguments
installedFolder = args.Item(0)
// 取得 windows shell 物件
Set objShell = WScript.CreateObject("WScript.Shell")
// 取得 windows fs 物件
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
// 桌面路徑
strDesktopFolder = objShell.SpecialFolders("Desktop")
// 程式集路徑
strProgramsFolder = objShell.SpecialFolders("Programs")
// 建立資料夾(程式集裡)
objFso.CreateFolder(strProgramsFolder & "\app provider")
// 建立捷徑物件
Set objDesktopShortCut = objShell.CreateShortcut(strDesktopFolder & "\feeler.lnk")
// 設定目標
objDesktopShortCut.TargetPath = chr(34) & installedFolder & "\app.exe" & chr(34)
// 設定圖示
objDesktopShortCut.IconLocation = installedFolder & "\resources\images\app.ico, 0"
// 設定執行工作目錄
objDesktopShortCut.WorkingDirectory = installedFolder & "\"
// 儲存桌面捷徑
objDesktopShortCut.Save
// 依同法再建立程式集捷徑
...

完成 vbscript 後以 ant 的 exec task 執行 (vbscript 程式可透過 cscript 命令執行)
<target name="config">
<exec dir="${install.path.dir}" executable="cscript" os="Windows XP" output="shortcut.log">
<arg value="resources/shortcut.vbs" />
<arg value="${install.path.dir}" />
</exec>
</target>

2009年12月28日 星期一

pythonw.exe

Windows 版本的 python 安裝後除了有 python.exe 直譯器之外,還有多一個 pythonw.exe
一般的 python 主程式如果副檔名為 *.py 則會預設以 python.exe 執行,執行時會先彈出一個 dos command line
以方便進行 輸入/輸出 ,但是一般的 GUI 程式是不需要用 command line 做 輸入/輸出 的,這樣一來
這一個 dos command line 就顯得多餘了,可以簡單的透過將 python 主程式的 副檔名 改為 *.pyw
這樣執行時 就會以 pythonw.exe 來執行 python 程式 ,也就不會彈出 dos command line

2009年12月22日 星期二

lenny + apache2 + mod_jk + tomcat55 設置

先安裝好需要的套件:
sudo aptitude install apache2 libapache2-mod-jk tomcat5.5 tomcat5.5-admin tomcat5.5-webapps

設定 tomcat:
sudo vim /etc/default/tomcat5.5
JAVA_HOME=/usr/lib/jvm/java-6-sun
TOMCAT5_SECURITY=no

若 TOMCAT5_SECURITY=yes
則會啟用 java 的 security manager 並以 /etc/tomcat5.5/catalina.policy 下的規則為依據
避免複雜設定先設定為 no 待有安全問題再進行詳細設定

tomcat webapps 位於 /var/lib/tomcat5.5/webapps/ 將 war 檔放到此目錄下過一會會自動解壓縮

先連線到 http://localhost:8180/webapp/ 看看是否可以正常連線

設定 apache jk module
在 /etc/apache2/mod-available/ 目錄下建立 jk.conf 檔案 內容如下:

JkWorkersFile /etc/apache2/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel info
JkMount /webapp/* worker1

這檔案是設定 jk 以 /etc/apache2/workers.properties 的設定建立 worker (可以多個)
接著建立 /etc/apache2/workers.properties 內容如下:

# Defining a worker named worker1 and of type ajp13
worker.list=worker1

# Set properties for worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

這檔案是建立一個 名稱為 worker1 且 type為 ajp13 的 worker
完成之後就可以 enable jk module

sudo a2enmod jk

完成後可以到 /etc/apache2/mod-enable/ 檢查看看有沒有兩個連結

$ ls -la | grep jk
lrwxrwxrwx 1 root root 25 2009-12-22 16:41 jk.conf -> ../mods-available/jk.conf
lrwxrwxrwx 1 root root 25 2009-12-22 16:41 jk.load -> ../mods-available/jk.load

接著要設定 /etc/apache2/sites-available/default 檔案
vim /etc/apache2/sites-available/default
加入:
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www/
JkMount /webapp/* worker1

</VirtualHost>

最後再重新啟動 apache

sudo /etc/init.d/apache2 restart

這次改以 80 port 連接 webapp

http://localhost/webapp/

順利的話就可以看到和用 8180 port 連接的相同結果

swing Window 元件置頂


無論是 JFrame、JDialog 均可使用下列 method 將 window 置於最上層,


即使切換視窗也不會被 focus 的視窗所蓋到。


public final void setAlwaysOnTop(boolean alwaysOnTop)

2009年12月20日 星期日

debian 下格式化隨身碟為 vfat

必須使用 mkfs.vfat 來進行格式化
而 mkfs.vfat 預設沒有安裝,查詢他所存在的 deb 套件位置

$ apt-file search mkfs.vfat
dosfstools: sbin/mkfs.vfat

可以發現存在於 dosfstools 下
先安裝 dosfstools 再格式化隨身碟為 vfat:

sudo aptitude install dosfstools
sudo mkfs.vfat /dev/sdb1

2009年12月18日 星期五

自定以圖片為背景的 JPanel

繼承 JPanel 並 override paintComponent(Graphics gc) 就可以了:

public class ImageBackgroundJPanel extends JPanel {
private Image backgroundImg = null;

public ImageBackgroundJPanel(String imagePath) {
backgroundImg = new ImageIcon(imagePath).getImage();
}

@Override
protected void paintComponent(Graphics gc) {
gc.drawImage(backgroundImg, 0, 0, this);
}
}

使用 jgoodies 的 look and feel

jgoodies 有許多產品,下載最新版的 jgoodies libraries looks 就可以了,
目前使用 2.2.2 版,將 looks-2.2.2.jar 加到 classpath 下
在執行到 swing 的任何程式之前,先加入以下 code 設定 L&F (look and feel)


// 設定銀色主題,有多種可用,必須在設定 L&F 前設定
// com.jgoodies.looks.plastic.theme - 主題的 package
PlasticLookAndFeel.setPlasticTheme(new Silver());
// 設定 look and feel ,在 linux 下使用 Plastic 系列比較沒問題,三種可用
//com.jgoodies.looks.plastic.PlasticLookAndFeel
//com.jgoodies.looks.plastic.Plastic3DLookAndFeel
//com.jgoodies.looks.plastic.PlasticXPLookAndFeel
UIManager.setLookAndFeel(new PlasticXPLookAndFeel());

python 取得 os 的 path 分隔符號

使用 os moudle 取得有關 os 的資訊
import os

javaLibraryPath = "." + os.pathsep + "bundle" + os.pathsep + "lib"

windows 下 javaLibraryPath = .;bundle;lib
linux 下 javaLibraryPath = .:bundle:lib

help(os) 片斷說明如下
- all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc.
- os.path is one of the modules posixpath, ntpath, or macpath
- os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos'
- os.curdir is a string representing the current directory ('.' or ':')
- os.pardir is a string representing the parent directory ('..' or '::')
- os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
- os.extsep is the extension separator ('.' or '/')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device ('/dev/null', etc.)

2009年12月12日 星期六

2009年12月10日 星期四

JXTA p2p 協定介紹之資料傳送

啟動 JXTA 網路後可以得到一個 net peer group 實例

infrastructurePeerGroup = jxtaNetMan.startNetwork();

而 net peer group 提供了許多 service ,透過其中的 discover service 及 pipe service
即可讓 peer 互相傳遞訊息。

// 取得 discovery service
discoveryService = infrastructurePeerGroup.getDiscoveryService();
// 取得 pipe service
pipeService = infrastructurePeerGroup.getPipeService();

第一個要了解的概念是 PipeAdvertisement ,Pipe 是 JXTA 用來進行 peer 與 peer 溝通
的通道,可以想像成 Socket ,實作上底層應該也是脫離不了 Socket,而傳輸
也是透過 InputStream 與 OutputStream 進行資料傳送。
而 Advertisement 是 JXTA 用來表示資源的定義文件,是一個規範好的 xml 文件
JXTA 為網路中的各式各樣的資源都有定義屬於他的 Advertisement 。
以 PipeAdvertisement 來說,就是用來表示某一個 Pipe 的 Ad 文件。

因此,在資訊傳送前,需先由接收端的 peer 建立一個 PipeAdvertisement ,並且,
綁定一個 InputPipe 到 PipeAdvertisement 上,再由 discovery service 發佈到 JXTA
網路上,而發送端的 peer 再由 discovery service 查詢到 PipeAdvertisement,並綁定
OutputPipe 到同一個 PipeAdvertisement 上,等到綁定完成後,即可開始傳送資料。

建立 PipeAdvertisement
PipeAdvertisement pipeAd = (PipeAdvertisement)
AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
pipeAd.setPipeID(IDFactory.newPipeID(PeerGroupID.defaultNetPeerGroupID));
pipeAd.setType(PipeService.UnicastType);
pipeAd.setName("APIPENAME");

建立 InputPipe bind 到 pipe ad 並設定 listener

pipeService.createInputPipe(pipeAd, pipeMsgListener);

listener 是一個 PipeMsgListener 的介面的實例,當完成對方建立了 OutputPipe 並
開始傳送資料時,PipeMsgListener 的 方法:

public void pipeMsgEvent(PipeMsgEvent event)

而傳進來的 event 物件會收到傳送過來的資料。
隨後即可將 pipeAd 發佈到 discover service 上,並設定 life time 及 expiration time

discoveryService.publish(pipeAd, pipeAdLifetime, pipeAdExpiration);
discoveryService.remotePublish(pipeAd, pipeAdExpiration);

而另一個要發送資料的 peer 則必須先以 discovery service 查詢前一個 peer
發佈的 pipeAd

discoveryService.getRemoteAdvertisements(
null, // 指定單一查詢的 peer id,null 則表示查詢整個網路
DiscoveryService.ADV, // 查詢的 type
"Name", // 指定查詢項目
"APIPENAME", // 查詢項目的值
1, // 一次要找多少個 ad
null); // 指定 discovery listener

這樣即會送出一個查詢到 JXTA 網路,最後一項參數的 listener 也可以使用

discoveryService.addDiscoveryListener(disclistener);

disclistener 是一個 DiscoveryListener 介面的實例,當查詢 ad 有結果後會觸發
listener 的 :

public void discoveryEvent(DiscoveryEvent event)

並可以 event 裡取得 ad 物件。因此發送端 peer 就可以得到 pipeAd 了
得到 pipeAd 後,發送端需要先建立一個 OutPipe 並綁定到 pipeAd 上

pipeService.createOutputPipe(pipeAd, outListener);

outListener 是一個 OutputPipeListener 當 OutPipe 綁定完成後,會觸發 listener 的

public void outputPipeEvent(OutputPipeEvent event)

並由 event 取得 OutputPipe 實例,開始傳送資料。

因此總結來說,傳送端在觸發 OutputPipeEvent 後可以開始傳送資料,
而接收端在觸發了 PipeMsgEvent 後可以開始接收資料,至於建立資料的方法則是,
建立一個 net.jxta.endpoint.Message 實例:

Message msg = new Message();

Message 是資料的包裝,裡面可以包含多筆 net.jxta.endpoint.MessageElement 實例
以最單純的 StringMessageElement 舉例:

StringMessageElement sme = new StringMessageElement(
"elemName", // element 的 key
"elemValue", // element 的 value
null // signature
);

將 element 加入到 Message 中,並以 OutPipe 傳送

msg.addMessageElement("msgNameSpace", sme);
event.getOutputPipe().send(msg);

而接收端則由 PipeMsgEvent 取得 Message,並由 namespace 和 element key 取值

Message msg = event.getMessage();
ElementIterator itMsg = msg.getMessageElements("msgNameSpace", "elemName");
while(itMsg.hasNext()){
MessageElement msgElem = itMsgElem.next();
System.out.println(msgElem.toString()); // 印出 "elemValue"
}

JXTA p2p 協定介紹之連線

JXTA 是 SUN 所提出的 p2p 標準協定,可使用各種語言完成,而由 java 完成的,
又可分為給 JavaSE 使用的 JXSE 及給 JavaME 使用的 JXME,也有其他語言的版本,
如: JXTA-C ,有空可以多加研究比較,而目前使用的版本為 JXSE,簡單介紹如下:

JXTA 是一個 p2p 的協定,規範了一個 p2p 網路上的所有 peer (節點) 的溝通標準,
有別於傳統的 client-server 架構,p2p 網路上有許多的新名詞,如:
peer、peer group、ad、RDV…等,在官方的說明文件中都有詳細說明,在此不多介紹,
JXTA 協定中定義了許多 peer 種類,以 EDGE 最常見,另外還有構成 JXTA 網路架構
的 RDV 節點,以及讓防火牆或 NAT 內的 EDGE 可以向外界RDV構通的 RELAY
節點,架構如下列官方文件裡的圖:









JXSE 下載下來解開有四個 jar:
jxse.jar、bcprov-jdk14.jar、javax.servlet.jar、org.mortbay.jetty.jar
其中似乎只要有 jxse.jar 即可 compile 而執行需要 jxse.jar 加 bcprov-jdk14.jar
javax.servlet.jar、org.mortbay.jetty.jar 目前的用處不明。

JXSE 程式以 net.jxta.platform.NetworkManager 類別來啟動及管理 JXTA 網路
建立 NetworkManager 時需指定 peer 種類及相關資訊:

jxtaNetMan = new NetworkManager(
NetworkManager.ConfigMode.EDGE,
FEELER_ENTITY_PEER_NAME,
new File(new File(JXTA_HOME), FEELER_ENTITY_PEER_NAME).toURI()
);

第一個參數為 peer 種類,第二個參數為 peer 名稱,第三個參數為 peer 資料暫存目錄
建立好 NetworkManager 之後可以進行一些設定:

jxtaNetMan.setConfigPersistent(true); // 啟動儲存設定功能
// 使用官方提供的預設 RDV ,但似乎有問題,無法連線
//會不斷連往 http://rdv.jxtahosts.net/cgi-bin/rendezvous.cgi?3 查詢 RDV
jxtaNetMan.setUseDefaultSeeds(true);
jxtaNetCfg = jxtaNetMan.getConfigurator(); // 取得設定物件,以進行更多設定
// 加入自定的 RDV 位址,範例: "tcp://www.bennu.tw:9701"
jxtaNetCfg.addSeedRendezvous(new URI(NetService.FEELER_RDV_URI));
jxtaNetCfg.save(); // 儲存設定到 peer 資料暫存目錄下的 PlatformConfig 檔案

設定之後,即可以 startNetwork() 啟動這一個 peer 的運作。

// startNetWork() 傳回 net group
infrastructurePeerGroup = jxtaNetMan.startNetwork();

每一個裝置或者jvm應該可以同時擁有多個節點,同時扮演多個角色,如:
可以既是某些 EDGE 的 RDV 又是其他 RDV 的 EDGE ,但是目前測試的結果,
無法在同一個 jvm 下啟動兩次 JXTA network

啟動完成後會回傳一個 peer group 物件,每一個 peer 最少都會加入一個 peer group
這個 peer group 稱之為 net peer group,可以想像成整個 JXTA 網路是一個 peer group
而如果要另外建立 peer group 也一定是此 net peer group 的子集,如下圖:

取得 net peer group 實例後,可以從 net peer group 取得許多種 service ,如:
discovery service、pipe service、rdv service、… 等,JXTA定義了許多 service
可以讓 peer 與 peer 進行資料傳遞,而不需在乎每個 peer 所在的網路環境。

其中需先注意 rdv service ,以 RDV peer 來說,除了在建立 NetworkManager 時,
第一個參數給予 NetworkManager.ConfigMode.RENDEZVOUS 仍然無法讓其他
EDGE peer 連上這一個 rdv peer ,還需在 startNetwork() 後啟動一下 rdv service:

jxtaNetMan.getNetPeerGroup().getRendezVousService().startRendezVous();

如此才可以開始提供 rdv 功能。
而 EDGE peer 要連上 RDV peer 則是使用 :

boolean connected = jxtaNetMan.waitForRendezvousConnection(timeout);

waitForRendezvousConnection 是一個 block 方法,會一直嘗試連線到成功或是
timeout 時間到為止,而回傳 boolean 表示連線成功(true)或失敗(false)。
目前 timeout 必須設定長一點,即使是在 local net 也需要 5 分鐘以上比較好。
可能是由於使用廣播的方式,導致連線效率不好,官網上可以看到已有改進的工作

除了 EDGE 可以連上 RDV,RDV 也可透過同一 method 連上其他 RDV,
以構成 RDV 主幹網路,而 EDGE peer 顧名思議就是散佈在 RDV 主幹網路的邊緣。

2009年12月3日 星期四

ivy 加入 java 的 maven 2 repository

直接在 <ivysettings> <resolvers> 下加入
<!-- maven 2 -->
<ibiblio name="java-net-maven2" root="http://download.java.net/maven/2/" m2compatible="true" />

這一個 ibiblio 節點
並在
<chain name="main" dual="true"> 裡補上
<resolver ref="java-net-maven2"/>

範例:
<?xml version="1.0" encoding="utf-8"?>
<ivysettings>
<resolvers>
<!-- spring EBR -->
<url name="com.springsource.repository.bundles.release">
<ivy pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
<artifact pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<url name="com.springsource.repository.bundles.external">
<ivy pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
<artifact pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<!-- maven 2 -->
<ibiblio name="java-net-maven2" root="http://download.java.net/maven/2/" m2compatible="true" />
<!-- main chain -->
<chain name="main" dual="true">
<resolver ref="shared"/>
<resolver ref="public"/>
<resolver ref="com.springsource.repository.bundles.release"/>
<resolver ref="com.springsource.repository.bundles.external"/>
<resolver ref="java-net-maven2"/>
</chain>
</resolvers>
</ivysettings>

2009年11月28日 星期六

java java.ext.dirs jvm 環境變數設定

在 java 運行時可以透過 -cp 去設定 classpath,
-cp 可以指定多個資料夾做為 *.class 檔的來源目錄,
同時也可以指定多個 jar 檔案做為 *.class 檔的來源,但是,
jar 檔通常很多,如果要一一的指定每一個 jar 非常不方便,
且,一般來說 jar 會被放在一個統一的目錄下:如 lib/ ,
這時可以透過 java -Djava.ext.dirs=lib 來指定 lib 下的所有 jar 檔,
一次就可以把所有的 jar 做為 classpath ,非常方便。

2009年11月19日 星期四

java 透過 PythonInterpreter 執行外部的 py 程式檔

先建立所需要的 java interface

package tw.bennu.feeler.jython.service;
public interface IJyCommand {
public String getName();
}

寫 src/jython/JyCommand.py 去實作 java interface
from tw.bennu.feeler.jython.service import IJyCommand
class JyCommandImpl(IJyCommand):

def __init__(self,cmd):
self.command = cmd

def getName(self):
ret = self.command
print ret
return ret
寫一個 Factory 物件,負責建立PythonInterpreter、讀取*.py、建立及回傳物件實例
public class JyFactory {
// 以 jython 實作的 classes 物件,可用來建立實例
private PyObject jyCommandClass = null;

public JyFactory() {
PyDictionary table = new PyDictionary();
PySystemState state = new PySystemState();
state.setClassLoader(JyFactory.class.getClassLoader());
PythonInterpreter interp = new PythonInterpreter(table, state);
// 讀入 src/jython/*.py
interp.execfile("src/jython/JyCommand.py");
jyCommandClass = interp.get("JyCommandImpl");
}

/**
* 建立一個 IJyCommand 介面的實例 (由JyCommand.py 實作介面內容)
*
* @param name
* @return
*/
public IJyCommand getJyCommand(String name) {
PyObject jyCommandObj = this.jyCommandClass.__call__(new PyString(name));
return (IJyCommand) jyCommandObj.__tojava__(IJyCommand.class);
}
}

JyFactory 解說:先以 interp.get("JyCommandImpl"); 取得 class 物件(只做一次),
再以 class.__call__ 建立實例 obj 物件,最後以 obj.__tojava__轉換回java 物件

public static void main(String[] args){
String name = jythonServImpl.getJyCommand("hello jython").getName();
System.out.println(name);
}

結果會印出 hello jython

jython Embedded 直譯器的 ClassLoader設定

jython 可以 embedded 一個直譯器到 java 的程式裡,叫作PythonInterpreter物件,
透過PythonInterpreter,我們可以寫出python語法的程式碼,同時又使用到java的物件,
PythonInterpreter 在建立時可以指定某個 classloader 做為他在讀取 java class的依據,
在某些Classloader 環境較特殊的情況可以使用,如OSGi的每個 bundle 都有自己的 classloader。

設定方式如下:

PyDictionary table = new PyDictionary();
PySystemState state = new PySystemState();
state.setClassLoader(XXX.class.getClassLoader());
PythonInterpreter interp = new PythonInterpreter(table, state);

其中的 table 是 python 的 key-value namespace 如:
a=1
則 table 裡會加入一筆 key=new PyString("a");value=new PyInteger(1) 的 pair,
傳入 table 可以當做 PythonInterpreter 建立時的預設參數,供 python 程式碼參考使用。

2009年11月17日 星期二

Felix 使用到 rt.jar 裡特殊的 package 設定

在OSGi的環境裡每個bundle都有自己的classloader,因此要使用別的bundle的package
必須透過Import、Export來使用。

Felix是一個java的OSGi實作,jre本身有提供許多官方的package可供使用,因此,
當Felix啟動時,必須啟動一個稱為System Bundle的 bundle,而這個System Bundle即
Export出許多 jre 所提供的 package,然而並非是"整個" jre 的 package 都Export 出來,
如 rt.jar 的一些 com.sun.* 的 package 即有缺少,可以在 Felix 的config.properties 檔裡
加上:
org.osgi.framework.system.packages.extra= \
com.sun.package.a,\
com.sun.package.b,\
....

來將需要的 package export 出來。

2009年11月12日 星期四

java MessageDigest 產生編碼資料

以 java.security.MessageDigest 產生 md5 編碼:
String password = "123";
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(password.getBytes());
String digest = new String(md5.digest());

除了md5也有rsa等各種編碼詳見MessageDigest javadoc

virtual box 官方版本安裝

http://www.virtualbox.org/wiki/Linux_Downloads
轉載自官方wiki說明如下:

Debian-based Linux distributionsAdd
one of the following lines according to your distribution to your /etc/apt/sources.list:
deb http://download.virtualbox.org/virtualbox/debian karmic non-free
deb http://download.virtualbox.org/virtualbox/debian jaunty non-free
deb http://download.virtualbox.org/virtualbox/debian intrepid non-free
deb http://download.virtualbox.org/virtualbox/debian hardy non-free
deb http://download.virtualbox.org/virtualbox/debian gutsy non-free
deb http://download.virtualbox.org/virtualbox/debian dapper non-free
deb http://download.virtualbox.org/virtualbox/debian lenny non-free
deb http://download.virtualbox.org/virtualbox/debian etch non-free
deb http://download.virtualbox.org/virtualbox/debian sarge non-free
deb http://download.virtualbox.org/virtualbox/debian xandros4.0-xn non-free

The Sun public key for apt-secure can be downloaded here.
You can add this key with

sudo apt-key add sun_vbox.asc

or combine downloading and registering:

wget -q http://download.virtualbox.org/virtualbox/debian/sun_vbox.asc -O- | sudo apt-key add -

The key fingerprint is
AF45 1228 01DA D613 29EF 9570 DCF9 F87B 6DFB CBAE Sun Microsystems, Inc.
(xVM VirtualBox archive signing key)
To install VirtualBox, do

apt-get install virtualbox-3.0

2009年11月10日 星期二

Unit Test 使用 System.in 輸入的物件

有時我們會有程式是需要從 System.in 輸入資料來進行運算的,我們會這樣寫:
BufferedReader inputBr = new BufferedReader(new InputStreamReader(System.in));
String input = inputBr.readLine();
但在寫UnitTest時,不可能測試到一半還要去輸入一些資訊到 console,有違自動化的架構。
且在 ant 結合 junit 時,遇到這種狀況會直接測試失敗,這時我們可以透過 mock System.in 的方式,
完成這一個測試,首先待測物件不可以直接使用 System.in 而是在建構子上加上一個,
傳入 InputStream的建構子,而預設建構子則以 System.in 當作該 InputStream,如:

public LcanorusMain() {
this(System.in);
}
public LcanorusMain(InputStream in) {
this.systemIn = in;
}

真正執行時是使用預設建構子,而測試時就是使用有 InputStream 參數的建構子,
並且傳入假造的 mock InputStream 即可:

@Before
public void before() {
lcMain = new LcanorusMain(new SystemInMockInputStream());
}

假造的 mock InputStream 實作重點就是將所要回傳的資料轉成 byte 再一一由read()方法傳回。
且完成所有 byte 後要傳回 -1 表示結束。

private class SystemInMockInputStream extends InputStream {
private byte[] cmdBytes = "fexit\n".getBytes();
private int readIndex = 0;

@Override
public int read() throws IOException {
if (readIndex < cmdBytes.length) {
int ret = cmdBytes[readIndex];
readIndex++;
return ret;
} else {
return -1;
}
}
}

2009年11月5日 星期四

OSGi 的 Bundle-Classpath header

OSGi bundle 可以透過 Import-Package 及 Export-Package 去使用互相的 Class,
但前提是彼此都是 OSGi bundle 格式的 jar 並完成部署在 OSGi 平台上,如Felix、Equinox…,
如果只是單純要使用到非OSGi bundle的第三方 jar,必須要將 jar 整個放到 OSGi bundle 的 bundle space 裡。
所謂的 bundle space 就是 bundle jar 本身,及 bundle 裡的 META-INF/MANIFEST.MF所指定的位置。
MANIFEST.MF 有一個 Bundle-Classpath 的 Header 可以用來指定引用到的第三方 jar。
需注意,指定時要將 "." 目錄也加上去,這裡的 "." 目錄就是指 bundle jar 的根目錄,再
以 "," 分隔,一一加入所需要的第三方 jar,而jar的位置也是以bundle根目錄開始算起。
範例(加入sqlite-jdbc.jar):
Manifest-Version: 1.0
Spring-Context: META-INF/spring/db-context.xml,META-INF/spring/db-osgi-context.xml
Bundle-Name: [feeler] db service
Bundle-Description: provide db service
Bundle-Vendor: muchu@www.bennu.tw
Bundle-Version: 0.0.1
Import-Package: org.osgi.framework
Export-Package: tw.bennu.feeler.db.service
Bundle-ClassPath: .,
META-INF/lib/sqlite-jdbc-3.6.17.jar

因此這一個 bundle jar的裡面還有一個sqlite-jdbc-3.6.17.jar並放在META-INF/lib/ 下。
另外 Bundle-ClassPath 也可以指定含有 *.class 的資料夾做為 classpath。
用法和 java 的 -cp 完全一樣,只是改以"," 分隔。
而且不需再對Bundle-ClassPath的jar做Import-Package的宣告,Import-Package 是偵對
其他 bundle Export 出來的 Package 使用。有宣告反而會因找不到Export-Package而無法啟動 bundle。

2009年11月2日 星期一

easymock 官方範例

mock 是指當用來被 unit 測試對象的物件,需要相依某些其他物件才能正常運作時,
以一個"假冒"的 mock 物件實作被相依的物件之界面,設定給 unit 測試對象物件,
目的是隔離unit 測試對象物件與部署環境,並於測試後直接檢查mock物件的變化,
達到不需要部署測試對象物件到環境,也可以了解測試對象物件對環境的影響是否符合要求。

easymock api 方便我們建立 mock 物件,官方範例如下:
http://www.easymock.org/EasyMock2_5_2_Documentation.html
以一個被測試的 ClassUnderTest 物件,使用到一個 Collaborator 介面為例,
將要為 Collaborator 建立 mock ,並給 ClassUnderTest 做測試。
public class ClassUnderTest {
// ...
public void addListener(Collaborator listener){
// ...
}
public void addDocument(String title, byte[] document) {
// ...
}
public boolean removeDocument(String title) {
// ...
}
public boolean removeDocuments(String[] titles) {
// ...
}
}

public interface Collaborator {
void documentAdded(String title);
void documentChanged(String title);
void documentRemoved(String title);
byte voteForRemoval(String title);
byte[] voteForRemovals(String[] title);
}

測試的 TestCase 實作如下:

import static org.easymock.EasyMock.*;
import junit.framework.TestCase;
public class ExampleTest extends TestCase {
private ClassUnderTest classUnderTest;
private Collaborator mock;

protected void setUp() {
mock = createMock(Collaborator.class); // 1 建立 mock
classUnderTest = new ClassUnderTest();
classUnderTest.addListener(mock);
}

public void testAddDocument() {
mock.documentAdded("New Document"); // 2 設定 mock 物件將replay後該有的行為
replay(mock); // 3 切換 mock 為 replay 準備狀態
classUnderTest.addDocument("New Document", new byte[0]);
}
}

重點在 replay() 之前 mock 物件運作的行為不是 Collaborator 真正的行為,而是,
"預先告知" replay() 之後, mock 應該要有什麼 method 被呼叫,傳進來的參數是什麼…等。
而 replay() 之後, classUnderTest.addDocument("New Document", new byte[0]);
如果沒有觸發 mock 物件"預先告知"的行為,就會產生錯誤使得測試失敗。

ivy 官方範例

ivy 採用和 maven 相同的 lib 管理機置,也使用相同的 repository,
ivy 的 dependency 設定檔叫做 ivy.xml,範例:

<ivy-module version="2.0">
<info organisation="apache" module="hello-ivy"/>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.0"/>
<dependency org="commons-cli" name="commons-cli" rev="1.0"/>
</dependencies>
</ivy-module>

對應於 maven 的 dependency
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.0</version>
</dependency>

groupId 對應到 org
artifactId 對應到 name
version 對應到 rev

完成 ivy.xml後將他放在和 ant build.xml 同目錄下,並在 build.xml 裡加上 resolve target:
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="hello-ivy" default="run">

...

<!-- =================================
target: resolve
================================= -->
<target name="resolve" description="--> retrieve dependencies with ivy">
<ivy:retrieve />
</target>
</project>

執行 ant resolve 時,ivy 會自動到 maven repository 上找到 ivy.xml 裡定義的 library,
並將之下載到 ~/ivy2/cache 下儲存為本地函式庫,再 copy 到 build.xml 同目錄下的 lib 資料夾裡。

spring-DM webcontainer 啟動

spring-DM 以 extender bundle 去偵測install進來的 bundle中
的META-INF/MANIFEST.MF含有:

Spring-Context: xxxxx/xxxx.xml

這個 header 的 bundle 並以這個 xml 為這個 bundle 建立一個屬於他的
ApplicationContext.....

與 war 整合時,則透過類似相同的概念,不過不是用同一個 extender 偵測,
而是要另外再加上 org.springframework.osgi.web.extender 這個 bundle,
他一樣會偵測 install 進來的 war (war 也可以被當做 bundle install),
並看 war 裡面的 META-INF/MANIFEST.MF 的 Spring-Context
但是不為 war 建立 ApplicationContext 而是將 war 丟給指定的 WebContainer,
預設為 tomcat,tomcat當然也是一個 bundle,再由WebContainer去建立ApplicationContext。

tomcat WebContainer 有自己原本的啟勳方式,但現在被當成一個 bundle,
就必須以OSGi的方式啟動,也就是 Activator ,而tomcat bundle本身沒有這一個Activator,
因此,除了tomcat bundle以外,還要再加裝一個Spring-DM提供的catalina.start.osgi-1.0.0.jar
由這個 start bundle 來啟動 tomcat。這裡的 tomcat bundle 和 tomcat start bundle
都可以在 Spring-DM 的 zip 檔裡的 lib 目錄下找到,目前試過只有 Spring-DM 提供的bundle,運作正常。

除了 tomcat WebContainer ,也可以使用 jetty 做為 WebContainer ,只要以 OSGi 的 fragment 機致,
去修改 org.springframework.osgi.web.extender 的設定即可,Spring-DM 文件中有寫:
http://static.springsource.org/osgi/docs/1.2.0/reference/html-single/#web:configuration:changing-deployer
但由於目前使用的 Felix Osgi Framework 未支援 fragment 機致,因此只能先以 tomcat 做為 WebContainer。

jetty 同樣也需要 start bundle,jetty fragment 、 start bundle 在 Spring-DM zip 檔的 lib 裡都可以找到。

2009年10月29日 星期四

ivy 新增 maven repository 以外的 repository

先在 build.xml 所在目錄下加入 ivysettings.xml 覆寫掉 ivy.jar 裡預設的 ivysettings.xml
僅修改 main-chain 部分內容如下:
<?xml version="1.0" encoding="utf-8"?>
<ivysettings>
<settings defaultResolver="default"/>
<include url="${ivy.default.settings.dir}/ivysettings-public.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
<include url="ivysettings-main-chain.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>
${ivy.default.settings.dir}的位置就是 ivy.jar 裡的 org.apache.ivy.core.settings package
修改後改為指到 build.xml 所在目錄下的 ivysettings-main-chain.xml
新增 ivysettings-main-chain.xml 其內容如下:
<?xml version="1.0" encoding="utf-8"?>
<ivysettings>
<resolvers>
<url name="com.springsource.repository.bundles.release">
<ivy pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
<artifact pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<url name="com.springsource.repository.bundles.external">
<ivy pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
<artifact pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<chain name="main" dual="true">
<resolver ref="shared"/>
<resolver ref="public"/>
<resolver ref="com.springsource.repository.bundles.release"/>
<resolver ref="com.springsource.repository.bundles.external"/>
</chain>
</resolvers>
</ivysettings>

這裡新增的public和shared resolver 是原本ivy.jar就預設定義的,而新增的兩個 url resolver是 spring 的 SpringSource Enterprise Bundle Repository

安裝 ivy - 類似 maven 的 java 函式庫管理工具

下載 ivy 後將 ivy-x.x.x.jar 放到 ant 的 lib 目錄下…如:
/usr/share/ant/lib/ivy-2.1.0.jar
即可在 ant 的 build.xml 裡使用 ivy 的 tag
並且編寫 ivy.xml 定義所用到的函式庫,放在 build.xml 檔案的同目錄下。

2009年10月27日 星期二

META-INF/MANIFEST.MF 的最後一行注意事項

要多加一行空白行(多按一次Enter),否則最後一行的參數將無法被 jar 讀到。
如:
========= MANIFEST.MF 開始 ==========
Manifest-Version: 1.0
Bundle-Name: Service listener example
Bundle-Description: A bundle that displays messages at startup and when service events occur
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: tw.bennu.tutorial.example.Activator
Import-Package: org.osgi.framework (<===最後一行參數)

========= MANIFEST.MF 結束 ==========

2009年10月23日 星期五

以 mutt 結合 msmtp 透過遠端 MTA 幫忙發信

mutt 是 linux 下常見預設安裝好的文字模式 mail client
但是,本身預設不但不包含 pop3、smtp 功能,甚至連郵件編輯器也是委拖其他軟體
同樣的我們可以安裝 msmtp ,並讓 mutt 委拖 smtp 功能給 msmtp 就可以寄信了。
安裝 msmtp
sudo aptitude install msmtp

在家目錄下建立 ~/.msmtprc 內容:
# Example for a user configuration file
# Set default values for all following accounts.
defaults
tls on #Gmail TLS有打開
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log

# A freemail service
account gmail
host smtp.gmail.com
from yourid@gmail.com
auth on #Gmail SMTP要認證
user yourid #gmail 帳號
password secret #gmail 密碼,明碼,記得chmod 600本檔案
account default : gmail

在家目錄下建立 ~/.muttrc 內容
set sendmail="/usr/bin/msmtp"
set from=yourid@yourdomain.com #gmail SMTP會覆寫

2009年10月22日 星期四

ssh disable 掉 password 登入

注意!進行此動作前需先確定可以透過 ssh-key 登入,
否則…主機最好別放太遠.....

編輯 /etc/ssh/ssh_config 檔
sudo vim /etc/ssh/ssh_config
設定以下三行為 no
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no
重新 reload ssh 服務
sudo /etc/init.d/ssh reload

測試,先將私錀改名
mv ~/.ssh/id_rsa ~/.ssh/id_rsa.backup
以 ssh 登入如果出現以下訊息表示ok (不再要求輸入密碼):
“Permission denied (publickey).”
成功後再把私錀改回來
mv ~/.ssh/id_rsa.backup ~/.ssh/id_rsa

以 ssh-key 代替密碼登入遠端機器

muchu@glory:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/muchu/.ssh/id_rsa): /home/muchu/id_rsa
Enter passphrase (empty for no passphrase): #直接按 enter
Enter same passphrase again: #直接按 enter
Your identification has been saved in /home/muchu/id_rsa.
Your public key has been saved in /home/muchu/id_rsa.pub.
The key fingerprint is:
21:02:7e:b1:7d:11:85:26:e7:4b:16:ae:39:d5:79:ed muchu@glory
The key's randomart image is:
+--[ RSA 2048]----+
| . . o+. |
| . . +. =. |
| . + o*oo . . |
| . . o*.o . . |
| *S. . . |
| + . E |
| . |
| |
| |
+-----------------+
muchu@glory:~$ ls /home/muchu | grep id_rsa
id_rsa id_rsa.pub

id_rsa 是私錀,id_rsa.pub 是公錀。
將 "公錀的內容" 附加在 "對方" 主機的 ~/.ssh/authorized_keys 下。
並將私錀以 id_rsa 的名稱放在 ~/.ssh/ 下。就可以不用密碼 ssh 登入 "對方" 主機。

在 debian 系統,放公錀到對方主機的動作可以透過以下命令完成,先將公錀放在 ~/.ssh/ 下
ssh-copy-id muchu@www

2009年10月19日 星期一

trac 重新同步 svn

如果在 trac 已經指定了某個 svn repository 的環境下,
重新建立 svn repository 導致 repositoy 版本變動,如:
將 rev 101 僅 dump 出第 101 版本,並放棄第 1~100 版。
重新建立 reposiotry load 101 版為新 repository 的第 1 版。
這時連到原本指定 repository 的 trac 時會看到以下的錯誤訊息:

Can't synchronize with the repository

執行以下命令重新進行 trac 與 svn 的同步即可解決:

trac-admin </path/to/projenv> resync

2009年10月12日 星期一

trac 備份及還原

備份
trac-admin /usr/local/trac/wiki hotcopy ~/backup/wiki
還原
cp 回去即可

apache2 + svn + trac 設置

一、apache2 + svn
#安裝 svn apache2 及 apache2-svn 模組
apt-get install apache2 subversion libapache2-svn
#建立 svn 目錄
mkdir -p /usr/local/svn
#建立 repos svn倉庫
svnadmin /usr/local/svn/repos
#改變 repos 存取權限
chown -R www-data /usr/local/svn/repos
#設定 apache dav_svn 模組
vim /etc/apache2/mods-available/dav_svn.conf
#修改為
<Location /svn>
DAV svn
SVNPath /usr/local/svn/repos
AuthType Basic
AuthName "Subversion repos"
AuthUserFile /etc/apache2/dav_svn.passwd
AuthzSVNAccessFile /etc/apache2/dav_svn.authz
#<LimitExcept GET PROPFIND OPTIONS REPORT>
Require valid-user
#</LimitExcept>
</Location>
#建立 dav_svn.authz 設定帳號讀寫權限
vim /etc/apache2/dav_svn.authz
#修改為 :設定 "/" = /repos 下的資料除了 muchu 帳號可以讀寫,其他帳號均不可讀寫。
[/]
* =
muchu = rw
#建立 dav_svn.passwd 設定帳號密碼 (如果檔案已存在就不需要 -c)
/usr/bin/htpasswd -c /etc/apache2/dav_svn.passwd muchu
#重新啟動 apache2
/etc/init.d/apache2 restart
#以 http co svn
svn co http://www.bennu.tw/svn

二、trac
#安裝 trac 及 mod_python 模組
apt-get install trac libapache2-mod-python
#建立 trac 根目錄
mkdir -p /usr/local/trac
#初始化 trac
trac-admin /usr/local/trac/wiki initenv
#Project Name 指定為
wiki
#Path to Repository 指定為
/usr/local/svn/repos
#設定 trac.ini
vim /usr/local/trac/wiki/conf/trac.ini
#修改 default_charset 編碼
default_charset = utf-8
#修改存取權限
chown www-data.www-data -R /usr/local/trac/wiki
# standalone 執行測試
sudo -u www-data tracd --port 8000 /usr/local/trac/wiki
存取 http://127.0.0.1:8000/ 檢查是否正常運作
#建立 apache2 Virtual Host
vim /etc/apache2/sites-available/trac
#修改為:
<VirtualHost *:80>
ServerName trac.bennu.tw
ServerAdmin muchu@trac.bennu.tw

DocumentRoot /usr/local/trac

ErrorLog /var/log/apache2/trac-error.log
CustomLog /var/log/apache2/trac-access.log combined

SetHandler mod_python
PythonHandler trac.web.modpython_frontend
PythonOption TracEnv "/usr/local/trac/wiki"
PythonOption TracUriRoot "/trac/wiki"
<LocationMatch "/trac/[^/]+/login">
AuthType Basic
AuthName "trac"
AuthUserFile /etc/apache2/dav_svn.passwd
Require valid-user
</LocationMatch>
</VirtualHost>
#啟動 trac Virtual Site
sudo a2ensite trac
#重啟 apache2
/etc/init.d/apache2 restart
#新增 trac.bennu.tw domain
trac CNAME www.bennu.tw.
#存取 http://trac.bennu.tw/trac/wiki
並可以 svn 使用的 muchu 帳號登入

2009年10月2日 星期五

python 的第三方模組安裝 (windows)

直接將第三方的模組:如 eazygui.py 放到 C:\Python26\Lib\site-packages 下即可。

2009年10月1日 星期四

python 學習資料小集

Python 教學文件
http://www.python.tw/ebook/Guido%20van%20Rossum/tut.html
Python 學習手冊
http://www.oreilly.com.tw/product2_c.php?id=a240
易記學
http://ez2learn.com/index.php/python-tutorials
pydoing
http://pydoing.blogspot.com/
官方 module index
http://docs.python.org/modindex.html

2009年8月28日 星期五

java JVM 增大記憶體配置參數 -X 與 -XX

java OOM 分為兩種:

java.lang.OutOfMemoryError: PermGen space
表示 stack 空間不夠,可能是 static 變數太多,或 class 太多。

java.lang.OutOfMemoryError: Java heap space
表示 heap 空間不夠,可能是建立的物件太多,或是處理資料檔案太大。

-Xms<size> : 初始的記憶體大小 (heap)
-Xmx<size> : 最大的記憶體大小 (heap)
-XX:MaxNewSize=<size> : 初始的記憶體大小 (stack)
-XX:MaxPermSize=<size> :最大的記憶體大小 (stack)
-Xincgc = 讓gc在背景持續執行

例:
java -Xms512M -Xmx1024M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Xincgc -version

2009年8月21日 星期五

spring 的 annotation @Value 介紹

通常在 bean.xml 裡我們常以 value 注入單純的資料,如 int,string、
也會注入 properties 檔裡的變數資料,如:
x-setting.properties -
com.transtep.xdna.doc.projplan.docDirLocation=tpma_doc

x-bean.xml -
<property name="docDirLocation">
<value>${com.transtep.xdna.doc.projplan.docDirLocation}</value>
</property>

但spring 3.0 使用 annotation 的時候,x-bean.xml 有些 bean 就不需要再宣告了,如 @Controller
而是自動讓 spring 去 scan 某 package 找到的,如此一來就無法注入 value
在 spring 3.0.M3 時加入了 @Value 這一個方便的 annotation 讓我們可以重拾 value 注入。
用法:

@Value("${com.transtep.xdna.doc.projplan.docDirLocation}")
private String docDirLocation = "";

2009年8月9日 星期日

svn merge 合併 用法理解

svn merge 是用來合併 "其他" repos目錄的某段版本之間的 diff 到你指定的目錄(一般為 . )
假設 有 trunk/stable 和 branches/maintain 兩個目錄都是同一個專案的程式碼,

直接以範例來解釋:
一開始,在trunk下已經有一個穩定版本,但是在 ver 27 時發現一個 bug,
於是將 stable 以 svn copy 到 branches/maintain 並 commit 準備進行修正,此時,
ver 28 時 trunk/stable 和 branches/maintain 均為同一版本的程式碼。

maintain 開始修正 bug ...
在 bug 修正的過程中,一共 commit 了10個版本,並將 bug修正完成。
同時 stable 也有做一些新功能開發,但和 maintain 正在修正的 bug 無太大關聯。

此時 ver為 38,但 maintain 的程式碼和 stable 的程式碼,
已經有 28->38 之間 10個版本的落差,

為了將 maintain 做的修正 "補回" 到 trunk 中,我們要做 merge,首先 cd 到 stable 目錄下
執行 svn merge -r 28:38 ../../branches/maintain/ .
結果即是將 maintain 在版本 28->38 之間做的變更(diff) ,合併到目前的 stable 目錄下。

此階段也許會產生衝突,因為trunk也有可能和 maintain 在同一段程式碼有做修改。
解決衝突後,stable的版本就會是 maintain bug 修正 加 stable 新功能開發版本,
但是 "尚未 commit"。
再進行 stable 的 commit 後,stable 即是最新的版本。

2009年7月31日 星期五

java ee servlet response

HttpServletResponse 物件是用來傳回結果到 browser,有些方法常用的方法:

設定 mime type:以下為 open office 的 odt 文件的 mime type
res.setContentType("application/vnd.oasis.opendocument.text");
設定編碼:
res.setCharacterEncoding("utf-8");
設定元件檔名:
res.addHeader("Content-Disposition", "attachment; filename=project-plan-book.odt");

2009年7月22日 星期三

hql 結合 distinct 過瀘重復的查詢結果

有些情況下,會有查詢到重復物件的問題,
舉一個例子直接說明:
A -> B :1 -> 多
from A as a left join fetch a.b as b
由於A與B是一對多的關係,即許多個B可能以外鍵指向同一個A
以上的hql查詢結果會返回許多重覆的A物件,
假設某A有3個B指向他,則會返回3個某A物件。
這時可以透過distinct將重復的A物件去除,如下:
select distinct a from A as a left join fetch a.b as b

2009年7月6日 星期一

debian 簡易檔案加密解密 ccrypt

安裝:
sudo aptitude install ccrypt

加密:
ccencrypt target-file
Enter encryption key:
Enter encryption key: (repeat)
target-file -> 變成 target-file.cpt 檔案

解密:
ccdecrypt target-file.cpt
Enter decryption key:
target-file.cpt -> 變成 target-file 檔案

顯示加密檔的內容但不解密:
ccat target-file.cpt
Enter decryption key:
.....秘密的資料內容....#@$
target-file.cpt 維持不變。

2009年7月2日 星期四

使用JFreeChart畫甘特圖(gantt)範例

JFreeChart主要分為 JFreeChart 物件表示圖表 及 Dataset 物件表示資料
不同的圖表物由 ChartFactory 不同的 static 方法建立,而不同的圖表物件也需要不同的 Dataset
來儲存資料,如甘特圖的 Dataset 為 IntervalCategoryDataset :

/**
*主程式進入點
*/
public static void main(String[] args){

final IntervalCategoryDataset dataset = createFakeDataset();
final JFreeChart chart = createChart(dataset);
// 輸出圖表成png檔(如果是web servlet可以輸出到response.getOutputStream())
ChartUtilities.writeChartAsPNG(target.getOutputStream(), outputChart, 2000, 600);
}

/**
* 建立甘特圖資料
*
* @return The dataset.
*/
public IntervalCategoryDataset createDataset() {

final TaskSeries s1 = new TaskSeries("計劃");
s1.add(new Task("許功蓋中文測試", new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
s1.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
s1.add(new Task("Requirements Analysis", new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001))));
s1.add(new Task("Design Phase", new SimpleTimePeriod(date(6, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001))));
s1.add(new Task("Design Signoff", new SimpleTimePeriod(date(2, Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001))));
s1.add(new Task("Alpha Implementation", new SimpleTimePeriod(date(3, Calendar.JUNE, 2001), date(31, Calendar.JULY, 2001))));
s1.add(new Task("Design Review", new SimpleTimePeriod(date(1, Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2001))));
s1.add(new Task("Revised Design Signoff", new SimpleTimePeriod(date(10, Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001))));
s1.add(new Task("Beta Implementation", new SimpleTimePeriod(date(12, Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001))));
s1.add(new Task("Testing", new SimpleTimePeriod(date(13, Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2001))));
s1.add(new Task("Final Implementation", new SimpleTimePeriod(date(1, Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001))));
s1.add(new Task("Signoff", new SimpleTimePeriod(date(28, Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2001))));

final TaskSeries s2 = new TaskSeries("實際");
s2.add(new Task("許功蓋中文測試", new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
s2.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
s2.add(new Task("Requirements Analysis", new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), date(15, Calendar.MAY, 2001))));
s2.add(new Task("Design Phase", new SimpleTimePeriod(date(15, Calendar.MAY, 2001), date(17, Calendar.JUNE, 2001))));
s2.add(new Task("Design Signoff", new SimpleTimePeriod(date(30, Calendar.JUNE, 2001), date(30, Calendar.JUNE, 2001))));
s2.add(new Task("Alpha Implementation", new SimpleTimePeriod(date(1, Calendar.JULY, 2001), date(12, Calendar.SEPTEMBER, 2001))));
s2.add(new Task("Design Review", new SimpleTimePeriod(date(12, Calendar.SEPTEMBER, 2001), date(22, Calendar.SEPTEMBER, 2001))));
s2.add(new Task("Revised Design Signoff", new SimpleTimePeriod(date(25, Calendar.SEPTEMBER, 2001), date(27, Calendar.SEPTEMBER, 2001))));
s2.add(new Task("Beta Implementation", new SimpleTimePeriod(date(27, Calendar.SEPTEMBER, 2001), date(30, Calendar.OCTOBER, 2001))));
s2.add(new Task("Testing", new SimpleTimePeriod(date(31, Calendar.OCTOBER, 2001), date(17, Calendar.NOVEMBER, 2001))));
s2.add(new Task("Final Implementation", new SimpleTimePeriod(date(18, Calendar.NOVEMBER, 2001), date(5, Calendar.DECEMBER, 2001))));
s2.add(new Task("Signoff", new SimpleTimePeriod(date(10, Calendar.DECEMBER, 2001), date(11, Calendar.DECEMBER, 2001))));

final TaskSeriesCollection collection = new TaskSeriesCollection();
collection.add(s1);
collection.add(s2);

return collection;
}

/**
* 方便建立日期
*
* @param day
* the date.
* @param month
* the month.
* @param year
* the year.
*
* @return a date.
*/
public Date date(final int day, final int month, final int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Date result = calendar.getTime();
return result;
}

/**
* 產生甘特圖物件
*
* @param dataset
* the dataset.
*
* @return The chart.
*/
public JFreeChart createChart(final IntervalCategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createGanttChart("測試甘特圖 Demo", // chart title
"事件", // domain axis label
"日期", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
//設定各處的字型(需設定為支援中文的字型,才不會造成中文亂碼)
Font font = new Font("標楷體", Font.BOLD, 16);
chart.getLegend().setItemFont(font);
chart.getTitle().setFont(font);
chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
return chart;
}

結果如下(web)

2009年6月26日 星期五

hibernate order by

假設 Account 的主鍵是 username VARCHAR,如果想要列出所有帳號資料,並依主鍵排序的話。
可以使用 id 進行排序即可,不一定要指定 username,如:

from Account as acc order by acc.id

這樣的寫法等同於

from Account as acc order by acc.username

但由於以 id 排序,不需要知道特定表格的主鍵 column 的名稱,
在某些無法得知 column 名稱的場合,可以派得上用場

2009年6月19日 星期五

hibernate 關聯 hbm.xml 設定 範例

many-to-one範例 -
<many-to-one name="targetGroup" column="group_id" class="com.transtep.green.servlet.framework.hibernate.GreenGroup" insert="false" update="false" cascade="save-update" outer-join="true" />

one-to-many範例 -
<set name="relAccExtSet" cascade="all" inverse="true" lazy="true">
<key>
<column name="account_id" />
</key>
<one-to-many class="com.transtep.green.servlet.framework.hibernate.GreenAccountExt" />
</set>

one-to-one範例 -
<one-to-one name="relSauth" class="com.transtep.green.servlet.framework.hibernate.GreenSimpleAuth" cascade="all" property-ref="relAccount" />

<many-to-one name="relAccount" column="account_id" class="com.transtep.green.servlet.framework.hibernate.GreenAccount" insert="false" update="false" cascade="save-update" outer-join="true" unique="true"/>

2009年6月8日 星期一

Spring PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer 的作用是可以將 spring 的 bean-config.xml 裡的某些資料
抽離出來,放到另外一個 key-value 的 property file 裡,方便統一設定。
先在bean-config.xml裡加入:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:green-setting.properties</value>
</property>
</bean>

再建立對應的 green-setting.properties 檔:
com.transtep.green.db.jdbcurl=jdbc:postgresql://db.transtep.com:5432/MARK_GREEN?charSet=utf8
com.transtep.green.db.user=postgres
com.transtep.green.db.password=admin

如此一來就可以在bean-config.xml裡使用${key}變數來設定資料,如:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>org.postgresql.Driver</value>
</property>
<property name="jdbcUrl">
<value>${com.transtep.green.db.jdbcurl}</value>
</property>
<property name="user">
<value>${com.transtep.green.db.user}</value>
</property>
<property name="password">
<value>${com.transtep.green.db.password}</value>
</property>
略....

2009年6月7日 星期日

debian 下的啟動服務設定 script update-rc.d

在 /etc/init.d 中建立一個叫作 zope 的 script , 然後

update-rc.d zope defaults

就會產生以下連結::

Adding system startup for /etc/init.d/zope ...
/etc/rc0.d/K20zope -> ../init.d/zope
/etc/rc1.d/K20zope -> ../init.d/zope
/etc/rc6.d/K20zope -> ../init.d/zope
/etc/rc2.d/S20zope -> ../init.d/zope
/etc/rc3.d/S20zope -> ../init.d/zope
/etc/rc4.d/S20zope -> ../init.d/zope
/etc/rc5.d/S20zope -> ../init.d/zope

2009年6月6日 星期六

hql 的left outter join轉換成sql的結果

hql:
from TpmaAccount as a
left join a.relTpmaStaff as staff with staff.projectId = 1

sql:
select
tpmaaccoun0_.username as username0_0_,
tpmastaff1_.staff_id as staff1_10_1_,
tpmaaccoun0_.password as password0_0_,
tpmaaccoun0_.role as role0_0_,
tpmaaccoun0_.enabled as enabled0_0_,
tpmaaccoun0_.valid_bdate as valid5_0_0_,
tpmaaccoun0_.valid_edate as valid6_0_0_,
tpmaaccoun0_.staff_id as staff7_0_0_,
tpmaaccoun0_.login_date as login8_0_0_,
tpmaaccoun0_.login_cnt as login9_0_0_,
tpmaaccoun0_.login_ip as login10_0_0_,
tpmastaff1_.project_id as project2_10_1_,
tpmastaff1_.first_name as first3_10_1_,
tpmastaff1_.last_name as last4_10_1_,
tpmastaff1_.sex as sex10_1_,
tpmastaff1_.org as org10_1_,
tpmastaff1_.cellphone as cellphone10_1_,
tpmastaff1_.telephone as telephone10_1_,
tpmastaff1_.address as address10_1_,
tpmastaff1_.skype as skype10_1_,
tpmastaff1_.msn as msn10_1_,
tpmastaff1_.email as email10_1_,
tpmastaff1_.remark as remark10_1_
from
public.tpma_account tpmaaccoun0_
left outer join
public.tpma_staff tpmastaff1_
on tpmaaccoun0_.staff_id=tpmastaff1_.staff_id
and (
tpmastaff1_.project_id=1
)

2009年5月15日 星期五

regex網頁版測試工具

regexpal
http://regexpal.com/

在下方貼上要測試的文字
在上方寫RegEx語法

2009年5月7日 星期四

apache commons httpclient 送出 multipart post 寫法

public byte[] uploadMessage(File fileToUpload, UPLOAD_FILE_TYPE type, int[] messageIds) {
byte[] response = null;
try {
// new method
PostMethod postHttpMethod = new PostMethod(webappUrl + uploadFileUrl);
// prepare
// key-value part
Part fileNamePart = new StringPart("fileName", fileToUpload.getName(), "UTF-8");
Part typePart = new StringPart("type", type.toString(), "UTF-8");
StringBuffer strbuf = new StringBuffer();
for (int i = 0; i < messageIds.length; i++) {
strbuf.append(messageIds[i]);
if (i != messageIds.length) {
strbuf.append(",");
}
}
Part messageIdsPart = new StringPart("messageIds", strbuf.toString(), "UTF-8");
// file part
Part filePart = new FilePart("content", fileToUpload);
// multipart
Part[] parts = new Part[] { fileNamePart, typePart, messageIdsPart, filePart };
MultipartRequestEntity multipartEntity = new MultipartRequestEntity(parts, postHttpMethod.getParams());
postHttpMethod.setRequestEntity(multipartEntity);
// execute
try {
httpclient.executeMethod(httpMethod);
response = httpMethod.getResponseBody();
} catch (HttpException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
httpMethod.releaseConnection();
}
return response;
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
}
return response;
}

JFrame的最小化與還原

使用JFrame的setState方法:
public void setState(int state)
state 的值可以是 Frame.NORMAL(還原)Frame.ICONIFIED(最小化)

另外:
JFrame還有 toFront() 及 toBack() 方法可以調整前後,
但必須在 Frame.NORMAL 狀態下才有效。

toFront() 讓 JFrame 到最前面不被其他視窗擋住。

2009年5月4日 星期一

以vim進行檔案編碼轉換

首先必須確定vim有支援multi_byte
開啟vim後輸入
:echo has('multi_byte')
如果結果為1則表示有支援

在vim下要以指定的encoding開啟某一檔案
:e ++enc=

在vim下要以指定的encoding儲存某一檔案
:w ++enc=

例:以vim開啟一個ucs-2le編碼的檔案,並轉存成utf-8編碼。
:e ++enc=ucs-2le /tmp/file_ucs2le.csv
:w ++enc=utf-8 /tmp/file_utf8.csv

ps.ucs-2le為windows下用的unicode編碼

2009年4月22日 星期三

java 的 odt 轉換工具 JODConverter

官方網頁
http://www.artofsolving.com/opensource/jodconverter

首先啟動openoffice的服務模式,最簡單的方式是,到openoffice安裝目錄下的program,如:
C:\Program Files\OpenOffice.org 3\program執行
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

可以使用 netstat -a 檢查 8100 port 是否有在 LISTENING
下載 jodconverter-2.2.2.zip 並將裡面的 lib 下的 jar放到classpath下

轉換的程式碼:
public static void main(String[] args) throws Exception {
File inputFile = new File("test.odt");
File outputFile = new File("test.doc");

// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();

// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);

// close the connection
connection.disconnect();
}

jodconverter是透過輸入的副檔名來做為轉換的依據的。

2009年4月16日 星期四

Junit 4.5 的 TestAll寫法

Junit 4.5 的 TestAll寫法

@RunWith(Suite.class)
@SuiteClasses( { TestAllDao.class, TestAllController.class })
public class TestAllXdna {

}

2009年4月11日 星期六

hinet 中華電信 DNS 列表

DNS Server 中華電信的全區 168.95.1.1
中華電信分區如下
北區 DNS 139.175.55.244(主) 139.175.252.16(次) 適用地區範圍:台北, 桃園, 新竹, 宜蘭, 花蓮, 苗栗
中區 DNS 139.175.150.20(主) 139.175.55.244(次) 適用地區範圍:台中, 彰化, 南投, 雲林
南區 DNS 139.175.10.20(主) 139.175.55.244(次) 適用地區範圍:高雄, 台南, 嘉義, 屏東, 台東

2009年4月8日 星期三

spring 3.0.0.M2 整合 Apache Tiles 2 模版

一、
tiles相關的jar加到WEB-INF/lib,以3.0.0.M2的情型,必需下載tiles-2.0.7的版本,
tiles-2.1.x以上的版本,尚有問題,tiles本身也相依commons-beanutils 及 commons-digester,
都可以在Tiles-2.0.7-bin.tar.gz下載得到。固與Tiles相關的jar有:
commons-beanutils-1.7.0.jar
commons-digester-1.8.jar
commons-logging-1.1.1.jar
tiles-api-2.0.7.jar
tiles-core-2.0.7.jar
tiles-jsp-2.0.7.jar

二、
在spring的bean設定檔稍做修改,
a在viewResolver將InternalResourceViewResolver改用UrlBasedViewResolver
b並加入viewClass 設定為 org.springframework.web.servlet.view.tiles2.TilesView
c加入 TilesConfigurer
如下:
<!-- test jsp view -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<!-- Tiles 模版 -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/templates.xml</value>
</list>
</property>
</bean>

三、
在上面的TilesConfigurer bean中指定了/WEB-INF/defs/templates.xml這個模版定義檔
建立templates.xml內容為:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="/WEB-INF/jsp/test.jsp" template="/WEB-INF/jsp/templates/layout.jsp">
<put-attribute name="title" value="TPMA Tiles tutorial homepage" />
<put-attribute name="header" value="/WEB-INF/jsp/templates/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/jsp/templates/menu.jsp" />
<put-attribute name="body" value="/WEB-INF/jsp/body.jsp" />
<put-attribute name="footer" value="/WEB-INF/jsp/templates/footer.jsp" />
</definition>
</tiles-definitions>

四、
上面的定義了當Controller的return
return new ModelAndView("test", "test", model);
的時後,test會被UrlBasedViewResolver組合成/WEB-INF/jsp/test.jsp,而TilesView則會到,
templates.xml找到/WEB-INF/jsp/test.jsp的定義,並根據layout.jsp頁面組合title、header、
menu、body、footer等頁面,layout.jsp的內容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<table border="0" width="100%" cellspacing="5">
<tr>
<td colspan="2"><tiles:insertAttribute name="header" /></td>
</tr>
<tr>
<td width="140" valign="top"><tiles:insertAttribute name="menu" /></td>
<td valign="top" align="left"><tiles:insertAttribute name="body" /></td>
</tr>
<tr>
<td colspan="2"><tiles:insertAttribute name="footer" /></td>
</tr>
</table>
</body>
</html>

以tiles:getAsString插入templates.xml定義的value為文字
以tiles:insertAttribute插入templates.xml定義的value的網頁

五、
準備templates.xml中定義的其他jsp頁面。

六、
每個網頁一樣可以使用EL取得model的值
${test.name } ${test.passwd }

2009年3月26日 星期四

wine 下好用的 winetricks script

在linux上裝wine可以模擬 windows 環境,並執行 *.exe程式。
然而windows上許多api/dll,官方的wine並不完全包含,必須另外安裝,如:
常用的DirectX、.net等,有一隻script可以幫忙快速安裝這些軟體,叫作winetricks:

winetricks主站:
http://wiki.winehq.org/winetricks

下載:
wget http://www.kegel.com/wine/winetricks

winetricks必須用到cabextract套件來解開*.CAB檔,所以必須安裝cabextract套件:
aptitude install cabextract

執行:
sh winetricks dotnet11 directx9 ie6 firefox3

即安裝 .net-1.1 directx9 ie6 firefox3

使用 apache mina 框架建立網路程式

server:
public class FlavorServer {

private int PORT = 6275;

public void startServer() {
try {
ByteBuffer.setUseDirectBuffers(false);
ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
IoAcceptor acceptor = new SocketAcceptor();
SocketAcceptorConfig cfg = new SocketAcceptorConfig();
cfg.getFilterChain().addLast("logger", new LoggingFilter());
cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

acceptor.bind(new InetSocketAddress(PORT), new FlavorServerHandler(), cfg);
System.out.println("MINA Flavor server started.");
} catch (IOException e) {
e.printStackTrace();
}
}
}

client:
public class FlavorClient {
private int PORT = 6275;

public void startClient() {
ByteBuffer.setUseDirectBuffers(false);
ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
IoConnector connector = new SocketConnector();
SocketConnectorConfig cfg = new SocketConnectorConfig();
cfg.getFilterChain().addLast("logger", new LoggingFilter());
cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
connector.connect(new InetSocketAddress(PORT), new FlavorClientHandler(), cfg);
}
}

無論是 server 或 client 用到的 Handler 都是繼承 IoHandlerAdapter 物件的子類別:
public class FlavorServerHandler extends IoHandlerAdapter
public class FlavorClientHandler extends IoHandlerAdapter
並override IoHandlerAdapter所提供的方法,就可以進行網路的溝通,如:

@Override
public void messageReceived(IoSession session, Object message) throws Exception {
String msgStr = (String) message;
if (msgStr.trim().equalsIgnoreCase("bye")) {
session.close();
return;
}
if (count == 1000) {
session.write("quit");
} else {
session.write(count);
}
count++;
System.out.println(msgStr);

}

@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("client session created");
if (session.getTransportType() == TransportType.SOCKET) {
((SocketSessionConfig) session.getConfig()).setReceiveBufferSize(2048);
}

session.setIdleTime(IdleStatus.BOTH_IDLE, 10);
}

apache2 建立新的 Virtual Host site

#aptitude install apache2
安裝好apache2後,預設只有/var/www/做為default site,
可以在 /etc/apache2/site-available/default 進行設定,
如果想增加一個 site 並綁定到不同的 根目錄 位置及 Virtual Host domain,
首先必須先去擁有的domain設定 CNAME 一個要用 domain 如:
storage CNAME www.bennu.tw.
也就是增加一個 storage.bennu.tw 但storage 的ip和 www.bennu.tw 其實是一樣的。

再到/etc/apache2/site-available/ 建立一個storage.bennu.tw檔案
vim storage.bennu.tw 內容為:

<VirtualHost *:80>
ServerName storage.bennu.tw
ServerAdmin muchu1983@storage.bennu.tw
DocumentRoot /storage/
ErrorLog /var/log/apache2/storageError.log
CustomLog /var/log/apache2/storageAccess.log common
</VirtualHost>

再將這個檔案建立一個連結在 /etc/apache2/site-enabled/ 下
ln -s /etc/apache2/site-availiable/storage.bennu.tw /etc/apache2/site-enabled/storage.bennu.tw

再來要記得用a2ensite enable 這個新的 site
a2ensite storage.bennu.tw

最後重新啟動 apache2 服務即可
/etc/init.d/apache2 restart

重新啟動後,用網址連storage.bennu.tw所看到的網站的根目錄,
就是/storage/* 下的資料。

spring app context 的 event 通知

spring 的 ApplicationContext 或 WebApplicationContext 在啟動/關閉時,會發出許多事件,
這些事件都是org.springframework.context.ApplicationEvent的子類別

ContextClosedEvent -
在ApplicationContext關閉時發佈事件。
ContextRefreshedEvent -
在ApplicationContext初始或Refresh時發佈事件。
RequestHandledEvent -
在Web應用程式中,當請求被處理時,ApplicationContext會發佈此事件。

如果需要對這些事件做處理,必須實作org.springframework.context.ApplicationListener介面,
並在bean-config.xml定義實作ApplicationListener的bean,一但有事件發生,ApplicationContext
即會通知bean-config.xml中的所有ApplicationListener做處理。

2009年3月16日 星期一

lxde autostart 自動啟動設定

到/usr/share/applications/下找到需要的啟動檔案,
如pidgin的啟動檔案為pidgin.desktop
將pidgin.desktop copy 到 ~/config/autostart/pidgin.desktop 即可
如果沒有autostart資料夾就自行建立。

2009年3月10日 星期二

spring framework mvc 處理編碼的 filter

只要在web.xml加入下列的filter
就可以統一將get/post的url,textfield的內容都轉換為utf-8編碼。

<!-- encoding -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>

2009年3月8日 星期日

善用/etc/hosts及ipconfig 綁定多個ip

如果沒有經過任何設定,當使用 domain name 存取一個對方的主機時,等於是以外部ip傳遞封包
封包一定是經由網域的外部出去internet再到對方的主機,即使對方的主機是和自已主機同網域。
例如:
同一個hub的兩個孔的主機,各指定自己的domain name為ftp.bennu.tw和www.bennu.tw
ftp到www封包路徑 ftp -> hub -> internet -> hub -> www,速度會變得很慢。

這個情況下可以將ftp和www的網卡,假設都是eth0,各綁定第二個內部ip
ftp:
#ifconfig eth0:0 192.168.1.100
www:
#ifconfig eth0:0 192.168.1.101

然後再將各自的/etc/hosts加入:
ftp:
192.168.1.101 www.bennu.tw www
www:
192.168.1.100 ftp.bennu.tw ftp

這樣一來即使使用domain name傳遞封包,路徑就不會再繞到internet,
而是僅僅在內部的網域內傳遞,速度快得多了:
ftp -> hub -> www

2009年3月7日 星期六

python的中文註解

必須在#!/usr/bin/env python下加入:
#-*- coding: utf-8 -*-
否則當python直譯器遇到中文字時會抱怨非ANSI字元,顯示以下訊息:
SyntaxError: Non-ASCII character .....

2009年2月23日 星期一

掃描 XSANE-HP Deskjet

安裝 xsane
sudo aptitude install xsane
並加入 scanner group
sudo usermod -G scanner -a muchu1983
之後在X的功能選單下就可以使用XSane進行掃描。

2009年2月16日 星期一

使用 springframework 3.0.0 建立 RESTful 網路應用程式 Controller

RESTful是一種新的Webapp設計概念,可以減少很多傳統Webapp程式的code,
傳統Webapp以一個 url mapping 到一個 service ,如 java servlet 或 spring controller,
以操作資料庫的 CRUD 四個步驟來說,一張資料表至少就需要4個 service 命令來完成實作。
也就是要分別對四個 url 進行操作。

RESTful 則將資料當成一種資源,利用 http 原本就有的 Get、Post、Delete、Put…
等七個 method ,對同一個 url 進行操作,如下圖。
















spring 在 3.0版之後的 spring MVC 框架就有提供 RESTful 相關的支援,並使用 java 5.0 的
annotation 語法,用起非常簡單方便。

spring 3.0.0 相依的 jar有:
antlr-3.0.1.jar
asm-3.1.jar
asm-common-3.1.jar
commons-logging.jar
log4j-1.2.13.jar

簡單的 RESTful 範例 /rest/{i}/{n} : i、n 均為變數


web.xml 加入 DispatcherServlet 的servlet定義,並mapping到 /rest/*

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring-restful-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

需注意:
DispatcherServlet 因為被 mapping 到 /rest/* 所以在 DispatcherServlet 帶起來的WebApplicationContext 之下 做mapping時, "/"符號就等於 "/rest/"這一層 ,
也就是說 html form action 的 "/rest/1/2/3/4" mapping 要寫成 "/1/2/3/4"。

在 web.xml 指定了 /WEB-INF/conf/spring-restful-config.xml 這個檔為 spring bean config
內容如下:

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

<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- test jsp view -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<bean id="testController"
class="com.transtep.restful.server.framework.spring.controller.TestController">
</bean>
</beans>

spring-restful-config.xml 檔裡有 2 個 bean 負責 spring 2.5 開始有的 annotation MVC功能,
DefaultAnnotationHandlerMapping -> 處理 class level 的 annotation。
AnnotationMethodHandlerAdapter -> 處理 method level 的 annotation。
底下再定義 Controller 的 bean class,這些 bean 就可以用 annotation 的語法來定義了。
如這裡的 TestController :

@Controller
public class TestController {

@RequestMapping(value = "/1/{ii}/{nn}", method = RequestMethod.GET)
public ModelAndView getTest(@PathVariable(value = "ii") String id, @PathVariable(value = "nn") String number) {
System.out.println("run 1");
Map model = new HashMap();
model.put("message", "id " + id + "\n" + "number " + number);
return new ModelAndView("test", model);
}

@RequestMapping(value = "/1/{ii}/{nn}", method = RequestMethod.POST)
public ModelAndView postTest(@PathVariable(value = "ii") String id, @PathVariable(value = "nn") String number) {
Map model = new HashMap();
model.put("message", "id " + id + "\n" + "number " + number);
return new ModelAndView("test", model);
}
}

首先在 class 宣告上以 @Controller 定義這是一個 Controller (不需要再implements Controller)
再以
@RequestMapping 定義各別的 method 的 URI 、 method mapping,特別是 URI 可以{}
定義成變數的格式,再配合 @PathVariable 將變數的實際值綁到 method的參數上,同時根據
方法的參數型別不同,自動轉換成正確的型別。

上面的例子以 get /rest/1/123/456 則會執行 getTest() 方法,id=123,number=456
而如果以 post /rest/1/456/789 則會執行 postTest() 方法,id=456,number=789

2009年2月10日 星期二

在 xorg.conf 檔調整顯示器的水平垂直更新率

每個場商的每個螢幕的設定範圍都不同,即使設定不正確螢幕也不一定就沒畫面,
有可能是在執行某些需要特殊畫面更新率的軟體,例如640x480解析度的遊戲時,
會因為更新率不支援(其實是沒設定好),而造成畫面一片黑,出現 out of range 等等。

先查詢螢幕的正確水平,垂直更新率。
編輯 /etc/X11/xorg.conf
找到 Section "Monitor" 裡面有 HorizSync VertRefresh 兩項,
各自填入查到的 水平(HorizSync)、垂直(VertRefresh) 更新率,
有時可能填的範圍要比查詢到的還小一點,50.0 - 80.0 可以只填 30.0 - 70.0就好。
更新後 startx 進入X,切換一下解析度,看看能不能正常顯示。

2009年2月8日 星期日

lenny中使用wine設定執行魔獸爭霸3

首先安裝wine就不多說了,我使用的是1.0.1-1

安裝好wine後,先執行winecfg設定一下
1.windows版本的地方選windows 2000
2.函式庫->已有的函式庫頂替 維持空的
3.顯示 -> 模擬一個虛擬桌面 打勾 800x600
4.儲存槽 -> 按一下自動偵測 讓wine對應linux的路徑到磁碟機代號

開始安裝Warcraft3,如果已經有安裝好的則直接copy過來linux下,跳過這一步
mount主程式的光碟,並在光碟的掛載點下執行wine install.exe
mount資料片的光碟,並在光碟的掛載點下執行wine install.exe

更新patch
下載 http://todian.myweb.hinet.net/ 提供的傻瓜包來更新
更新完成後也會變成免光碟版,一樣以 wine 執行更新程式,
wine warcraft_patch_upgrade_122a_v3.exe
選擇Warcraft III的安裝(或copy)資料夾,由於傻包完成後會自動執行遊戲,
但wine的Directx3D還不夠完整,現在執行畫面可能一片黑,先想辦法關掉遊戲
大絕招是ctrl-alt-←關掉整個X再startx

透過安裝步驟 wine的register表也會自動加入Warcraft資料,
但如果是從別的地方copy的,就要手動去新增wine register裡的資料
執行 regedit
找到下列的key,如果沒有就按右鍵新增key(一個key就是一層資料夾)
HKEY_CURRENT_USER\Software\Blizzard Entertainment\Warcraft III
在右側建立名為Gfx OpenGL的DWORD值,Value data設置為1
(這個regedit主要設定War3以opengl執行遊戲)

現在回到 Warcraft III 資料夾下,執行
wine War3.exe -opengl或wine War3.exe -x11
順利的話會在wine虛擬桌面下看到開場動畫,等動畫播完,遊戲畫面會一片黑
滑鼠點一下黑色的遊戲畫面,則遊戲界面就會出來。先離開遊戲

再次執行 winecfg
將 顯示 -> 模擬一個虛擬桌面 打勾取消掉
重新回到Warcraft III 資料夾下,執行
wine War3.exe -opengl或wine War3.exe -x11
就可以以全螢幕進行遊戲了。
(因為開場動畫沒有用虛擬桌面的話就不能播)

2009年2月7日 星期六

debian下apache2安裝mod_python模組

首先安裝好apache2
sudo aptitude install apache2
安裝好後預設的web context根目錄會在/var/www下

安裝mod_python模組
sudo aptitude install libapache2-mod-python

設定網站支援mod_python
vim /etc/apache2/sites-available/default
在Directory /var/www的項目中
把內容改為:
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
存檔

重新啟動apache
/etc/init.d/apache2 restart

這樣就完成mod_python的安裝與設定了
現在可以建立python程式

vi /var/www/test.py
內容為:
#!/usr/bin/env python
def index(req):
return "Test successful";

以browser連到 http://localhost/test.py
就可以看到 Test successful 的輸出結果。

2009年1月23日 星期五

svnadmin dump 參數說明

在命令列下輸入
$svnadmin help dump 會出現以下訊息:

dump: 用法: svnadmin dump REPOS_PATH [-r LOWER[:UPPER]] [--incremental]

將檔案系統的內容, 以一種可攜式 '傾印檔' 格式輸出到標準輸出, 並將訊息
回報輸出到標準錯誤. 將 LOWER 與 UPPER 之間修訂版內容傾印出來. 如果沒
有指定修訂版的話, 傾印所有的修訂版樹. 如果只有指定 LOWER 的話, 只傾印
一個修訂版樹. 如果使用了 --incremental 選項, 那麼第一個傾印的修訂版會
是與前一個修訂版的差異, 而非平常的全文輸出.

有效選項:
-r [--revision] ARG : 指定修訂版編號 ARG (或 X:Y 範圍)
--incremental : 以差異增量進行傾印
--deltas : 於傾印檔輸出中使用檔案差異
-q [--quiet] : 不顯示進度 (僅錯誤) 至標準錯誤輸出


僅備份部分版本需使用 -r 參數,如:
repos檔案庫的最新版本號為100
svnadmin dump repos/ -r 90:100
即是完整備份90到最新的100的資料,但下次使用svnadmin load指令回來時
版本會從1開始算,也就是之前的版本90變成版本1

--incremental是進行差異備份,影響的是第一個版本的內容,如:
svnadmin dump repos/ -r 90:100 --incremental
一樣是備份 90-100 之間的資料,但版本90的資料卻不包含版本89之前的資料
也就是備份出來的資料必須建立在已經有版本89的檔案庫為基礎的檔案庫
如果 svnadmin load 時沒有版本 89的檔案庫為基礎,則會出現錯誤。