Spring EBR:
http://www.springsource.com/repository/app/
maven repository:
http://mvnrepository.com/
maven browser:
http://www.mvnbrowser.com/index.html
2010年1月29日 星期五
java System.getProperty() 參數紀錄
java.version – Java Runtime Environment version
java.vendor – Java Runtime Environment vendor
java.vendor.url – Java vendor URL
java.home – Java installation directory
java.vm.specification.version – Java Virtual Machine specification version
java.vm.specification.vendor – Java Virtual Machine specification vendor
java.vm.specification.name – Java Virtual Machine specification name
java.vm.version – Java Virtual Machine implementation version
java.vm.vendor – Java Virtual Machine implementation vendor
java.vm.name – Java Virtual Machine implementation name
java.specification.version – Java Runtime Environment specification version
java.specification.vendor – Java Runtime Environment specification vendor
java.specification.name – Java Runtime Environment specification name
java.class.version - Java class format version number
java.class.path – Java class path
java.library.path – List of paths to search when loading libraries
java.io.tmpdir – Default temp file path
java.compiler – Name of JIT compiler to use
java.ext.dirs – Path of extension directory or directories
os.name – Operating system name
os.arch – Operating system architecture
os.version – Operating system version
file.separator – File separator ("/" on UNIX)
path.separator – Path separator (":" on UNIX)
line.separator – Line separator ("\n" on UNIX)
user.name – User's account name
user.home – User's home directory
user.dir – User's current working directory
java.vendor – Java Runtime Environment vendor
java.vendor.url – Java vendor URL
java.home – Java installation directory
java.vm.specification.version – Java Virtual Machine specification version
java.vm.specification.vendor – Java Virtual Machine specification vendor
java.vm.specification.name – Java Virtual Machine specification name
java.vm.version – Java Virtual Machine implementation version
java.vm.vendor – Java Virtual Machine implementation vendor
java.vm.name – Java Virtual Machine implementation name
java.specification.version – Java Runtime Environment specification version
java.specification.vendor – Java Runtime Environment specification vendor
java.specification.name – Java Runtime Environment specification name
java.class.version - Java class format version number
java.class.path – Java class path
java.library.path – List of paths to search when loading libraries
java.io.tmpdir – Default temp file path
java.compiler – Name of JIT compiler to use
java.ext.dirs – Path of extension directory or directories
os.name – Operating system name
os.arch – Operating system architecture
os.version – Operating system version
file.separator – File separator ("/" on UNIX)
path.separator – Path separator (":" on UNIX)
line.separator – Line separator ("\n" on UNIX)
user.name – User's account name
user.home – User's home directory
user.dir – User's current working directory
2010年1月15日 星期五
cx_freeze 將 python 程式轉換成 exe
安裝 cx_freeze 前需先安裝好 python
再根據安裝的 python 版本下載對應的 cx_freeze 安裝程式
下載網站
http://cx-freeze.sourceforge.net/
安裝完成後會在原本的 python 安裝目錄下的 Scripts 目錄出現 cxfreeze cxfreeze.bat 兩個檔案
將要轉換的 python 程式 及 圖示檔 copy 到 Scripts 目錄下,並開啟 dos command line , cd 到 Scripts 下
執行:
C:\Python26\Scripts\>cxfreeze --base-name=Win32GUI --icon=app.ico app.py
--base-name=Win32GUI 是用來使用在執行 gui 程式時不必顯示 console 視窗,和將 副檔名 改為 app.pyw 意思一樣。
--icon 就是用來指定 產生的執行檔 的 icon 圖檔。
完成後,會在 Scripts 下建立 dist 資料夾,並產生幾個檔案,當然也包含 app.exe
dist 下的所有檔案都要有才能執行 app.exe (正常執行)
再根據安裝的 python 版本下載對應的 cx_freeze 安裝程式
下載網站
http://cx-freeze.sourceforge.net/
安裝完成後會在原本的 python 安裝目錄下的 Scripts 目錄出現 cxfreeze cxfreeze.bat 兩個檔案
將要轉換的 python 程式 及 圖示檔 copy 到 Scripts 目錄下,並開啟 dos command line , cd 到 Scripts 下
執行:
C:\Python26\Scripts\>cxfreeze --base-name=Win32GUI --icon=app.ico app.py
--base-name=Win32GUI 是用來使用在執行 gui 程式時不必顯示 console 視窗,和將 副檔名 改為 app.pyw 意思一樣。
--icon 就是用來指定 產生的執行檔 的 icon 圖檔。
完成後,會在 Scripts 下建立 dist 資料夾,並產生幾個檔案,當然也包含 app.exe
dist 下的所有檔案都要有才能執行 app.exe (正常執行)
2009年12月31日 星期四
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>
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>
// 讀取參數
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
一般的 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 連接的相同結果
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
而 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 實例
而 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 的子集,如下圖:
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 程式碼參考使用。
訂閱:
文章 (Atom)
