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 .....