2010年5月24日 星期一

Singleton notes

Singleton是近代有名的Design Pattern,以下介紹各種實做的approach
-Enum approach
Java SE 1.5後多了enum的型態,enum沒有可以access的constructor,不能被extend,根據"Effective Java"指出 single-element enum是最佳實作singleton的方式

public enum Singleton{
INSTANCE;
}

-static initializer

public final class Singleton {

private static final Singleton instance = new Singleton();
//一開始即做靜態的初始
private Singleton() {
//初始化的欄位
}

public static Singleton getInstance() {
return instance;
}
}

-Synchonized approach

public final class Singleton {

private static final Singleton instance = null; //宣告為null

private Singleton() {
//初始化的欄位
}

public static synchronized Singleton getInstance() {
return instance;//確保getinstance()有synchronized避免race condition
}
}

x Boken Singleton-Double checked locking
此問題詳細介紹如下:
http://en.wikipedia.org/wiki/Double-checked_locking

public class Singleton {

private static final Singleton instance = null; //宣告為null

public static Singleton getInstance() {

if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

-Lazy initialization
1.initialize-on-demand holder class idiom

public final class Singleton {

private Singleton() {
}

private static class SingletonHolder {

private static final Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}

2.Double-check idiom with volatile
Java SE 5後新的memory model讓volatile此關鍵字確保所有thread所取得的欄位值是最新被寫入的

private volatile Helper helper = null;

public Helper getHelper() {
if (helper == null) {
sychonized(this)
{
if (helper == null) {
helper = new Helper();
}
}
}
}

2010年5月18日 星期二

Signing Jar File

jarsigner -keystore myKeys.jks(指定keystore檔名) -sigfile SIGNATURE -signedjar target.jar(指定sign完的jar檔名稱) jPlurk.jar myKeys(keystore的alias)

jar tvf target.jar觀看其內容,其中會產生一signature檔案.SF與.DSA

MANIFEST.MF內容如下:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
X-COMMENT: Main-Class will be added automatically by build
Class-Path: lib/httpclient-4.0.jar lib/httpmime-4.0.jar lib/httpcore-4
.0.1.jar lib/commons-logging-1.1.1.jar lib/json_simple-1.1.jar lib/ro
me-1.0.jar lib/jdom.jar lib/commons-codec-1.4.jar lib/commons-lang-2.
4.jar lib/commons-pool.jar lib/commons-beanutils.jar lib/commons-coll
ections-3.2.jar lib/commons-dbcp.jar
Created-By: 14.0-b16 (Sun Microsystems Inc.)
Main-Class: plurkbot.PlurkBot

Name: util/rsa/Enc_RSA.class
SHA1-Digest: ijuZMiZBZsFI2uzaEPS21BEAlUA=

Name: plurkbot/PlurkBot.class
SHA1-Digest: gWtRJzsWc6y4tpyojSLZayItgqE=

Name: plurkbot/RSSReader.class
SHA1-Digest: RuN4Yplm+osh1Gl2SQUSNc9ayhQ=

Name: plurkbot/KeyFinder.class
SHA1-Digest: DOlsuXgpTd5+WqjEF43THJwTCDU=

Name: util/rsa/Dec_RSA.class
SHA1-Digest: Xr+XbYnikxc5nb4NSuHrhNK0JQ0=

Name: util/rsa/Skey_RSA.class
SHA1-Digest: qSpAs93F+pAn03E43+/MidO2/Ig=

.SF檔案內容如下:

Signature-Version: 1.0
SHA1-Digest-Manifest-Main-Attributes: m0AqR3Yv7Ue3Ktu7XcBNksOBR9o=
Created-By: 1.6.0_14 (Sun Microsystems Inc.)
SHA1-Digest-Manifest: RowSgzBOLnT02SP9Rp22mDeWZZ8=

Name: util/rsa/Enc_RSA.class
SHA1-Digest: zBTBXQToj5H/0IFeao2C+oklyAM=

Name: plurkbot/PlurkBot.class
SHA1-Digest: Ei29dS5puQcnJUjrUvGiUtNXAEU=

Name: plurkbot/RSSReader.class
SHA1-Digest: z20rQN0mplLb05oHMUuBO9mAsCY=

Name: plurkbot/KeyFinder.class
SHA1-Digest: ywdvBXjuycDOjEhLOZpFezEx6mQ=

Name: util/rsa/Dec_RSA.class
SHA1-Digest: lLcZO+xugO/S5hxLhWHn+aGGJZQ=

Name: util/rsa/Skey_RSA.class
SHA1-Digest: eqrG/z+pwfT6dxodDvNzbikRRFw=


而欲檢驗jar檔則使用
jarsigner -verify -verbose target.jar (-verbose會列出詳細資訊)
驗證步驟如下:
1.以public key驗證digital signature block file(.DSA),同時確保為該簽章使用該對應private key,此時亦會使用(.SF檔)來驗證
2.檢驗.SF檔案裡面的hash vaule是否與MANIFEST.MF檔案的hash值相同
3.檢驗.SF檔案中與MANIFEST.MF檔案中各個檔案的hash值是否相同

D:\jar>jarsigner -verify -verbose target.jar

976 Wed May 19 13:39:20 CST 2010 META-INF/MANIFEST.MF
648 Wed May 19 13:39:20 CST 2010 META-INF/SIGNATUR.SF
1007 Wed May 19 13:39:20 CST 2010 META-INF/SIGNATUR.DSA
0 Tue Dec 15 10:50:00 CST 2009 META-INF/
0 Tue Dec 15 10:50:00 CST 2009 plurkbot/
0 Tue Dec 15 10:50:00 CST 2009 util/
0 Tue Dec 15 10:50:00 CST 2009 util/rsa/
sm 1993 Tue Dec 15 10:50:00 CST 2009 plurkbot/KeyFinder.class
sm 7898 Tue Dec 15 10:50:00 CST 2009 plurkbot/PlurkBot.class
sm 3519 Tue Dec 15 10:50:00 CST 2009 plurkbot/RSSReader.class
sm 2812 Tue Dec 15 10:50:00 CST 2009 util/rsa/Dec_RSA.class
sm 1922 Tue Dec 15 10:50:00 CST 2009 util/rsa/Enc_RSA.class
sm 1433 Tue Dec 15 10:50:00 CST 2009 util/rsa/Skey_RSA.class

s = signature was verified
m = entry is listed in manifest
k = at least one certificate was found in keystore
i = at least one certificate was found in identity scope

jar verified.

驗證失敗的話則產生Security Exception
如果要結合Signed jar與policy file
則可參考下列指令:
.Export public certificate
keytool -export -keystore myKeys.jks -alias myKeys -file myPublicKey.cer
*查看憑證資訊
-keytool printcert -file myPublicKey.cer
.套用public certificate
keytool -import -alias myPublicKey file myPublicKey.cer -keystore myPublicKey.jks
.將keystore加入policy檔案
keystore "file:filepath/myPublicKey.jks"
.將signedBy加入grant statement
grant signedBy "myPublicKey", codeBase
"file:/filepath/target.jar" {permission java.io.FilePermission "/directory/*", "write"}

2010年5月16日 星期日

Android 二三事-Android開發工具

目前因要著手開發Android上的Application,整理了點Survey的資訊
官方Android Developer
裡面也有頗豐富的相關介紹,但在與IDE整合上都為Eclipse,但因為工作上比較常用Netbeans IDE,所以找了與Netbeans整合的介紹:
Android SDK與NetBeans IDE整合
步驟簡介如下:
1. 至Netbeans->tools->plugins->settings新增更新位置
http://kenai.com/downloads/nbandroid/updates.xml
後 就可於search中找到android的plugin
接著至tools->Java platform選取android sdk目錄位置加入Android平台

此外仍須建立AVD(Android Virtual Devices)模擬器,指令參考如下:
[查 看target列表]
android list target
[查看模擬器列表]
android list avd
[建 立模擬器]
android create avd --target 1 --name emu --sdcard 500M
(target id為android的版本, 2.1的id為7)
而於Netbeans上可選擇此App所要使用的AVD版本

開發完的application可直接執行該app即可,模擬器畫面如下:

同事整理的相關心得http://cooking-java.blogspot.com/search/label/Android

XDA Forumhttp://forum.xda-developers.com/index.php
Gasolin的Android開發筆記 http://code.google.com/p/androidbmi/wiki/DiveIntoAndroid
To be Continuted...

2010年5月12日 星期三

Hello Android, Hello Nexus One

經過一番坡折後,我的Google Nexus One入手了

My Google Nexus OneMy personal android icon

接著就是安排我的Andriod project了
擷取 Android 手機畫面:http://wiki.cheyingwu.tw/Android/Get-screenshot-on-andrioid
http://wiki.cheyingwu.tw/Android/Get-screenshot-on-andrioid
實際擷取畫面如下: