2014年7月22日 星期二

[Note] 編碼與加密

base64
. 可反解。
. 似明碼 很好辨識和破解。
. 常用於在通常處理文字資料的場合。
. 所以與其作為加密方法 更適合作為傳送特殊符號的編碼處理。

DES (Data Encryption Standard)
. 可反解。
. 對稱式加密。
. 速度較快,適用於加密大量數據時。

AES (Advanced Encryption Standard)
. 可反解。
. 對稱式加密。
. 安全性高、速度快。

RSA
. 可反解。
. 非對稱式加密。
. 以發明者的名字命名 Ron Rivest, AdiShamir 和 Leonard Adleman。

DSA (Digital Signature Algorithm)
. 一種標準的 DSS (Digital Signature Standard) 簽章演算法。
. 非一種加密法。
. 會將數位文件用 Hash Function (ex: MD5, SHA, SHA-1) 產出一份訊息摘要 再用私鑰加密成數位簽章送給對方 對方再用公鑰解密數位簽章與 Hash Function 解密接收到的交易資料 若兩邊的訊息摘要相同 表示內容正確 未遭到竄改

SHA-1
. 不可反解。
. 雜湊編碼方式。
. 基於 MD4 算法的,並且它的設計在很大程度上是模仿 MD4 的。

MD5
. 不可反解。
. 訊息指紋演算法: 目的是要計算出"唯一"能代表該份資料的字串。
. 雜湊編碼方式。


對稱式加密 (Symmetric Algorithm)
. 加解密使用同個 key。

非對稱式加密 (Asymmetric Algorithm)
. 使用公鑰加密 私鑰解密。


* Reference
Base64
. 加密算法
. SSL常见加密算法
. 加密基本知識
. 密碼學的應用
. 下載小常識SHA1、MD5驗證碼

2014年1月27日 星期一

[Javascript] trim() is not working in IE8

Error Message

Object doesn’t support this property or method


Solution

1. replace

str.replace(/^\s+|\s+$/g,"")


2. jQuery:

$.trim("str");



* Reference
. trim()

[Web] meta X-UA-Compatible

<meta http-equiv="X-UA-Compatible" content="....." />

指定 render 的模式

content 內填入要使用的模式 可使用多個用逗號分開


. 標準模式:
IE=9; IE=8; IE=7...

. 模擬模式:
IE=EmulateIE9; IE=EmulateIE8; IE=EmulateIE7....

. 什麼版本 IE 就用什麼版本的標準模式:
IE=edge

. 強制 IE 使用 Google Chrome Frame:
chrome=1



Google Chrome Frame
  • IE plugin,可在 IE 中模擬 Chrome
  • 目前支援 IE 6、IE 7、IE 8、IE9


* Reference
. X-UA-Compatible設置IE兼容模式
. 在老 IE 中使用 Google Chrome Frame 超速新引擎瀏覽網頁(測試心得)
. Google Chrome Frame

2014年1月25日 星期六

解憂雜貨店

即使只是小孩子們的玩笑 浪矢爺爺也仍都是認真看待

只要是正向的想法 總有一天也能是力量的來源吧

也沒有誰不想被認真的對待 就算只是一字一小句

能讓人感受到用心的 哪天想起就是能讓人窩心的能量

量力的適時給別人支援 學習著了解別人的用心



每個人間的溝通 都得靠著自己的想法去理解對方

文字更是 在沒有情緒的一張紙上 任憑你解讀

所以也才能發展出更多可能 看到更多的自己

也可能產生誤會 但終究是自己的反射

不論好的壞的 這些撞擊 都是為了得到最後的想法

我想那更是珍貴的

太多時候 我們找不到看不見心中的答案

因為這樣 迷惘 猶豫 或者是用很多的藉口推砌

所以需要別人的指點 意見 甚至是打破心中的那些不勇敢

前進 面對 才能更接近答案



浪矢爺爺回覆的最後一個問題 一張白紙

甚麼都沒有 沒有想法 沒有方向

但表示的也是 沒有侷限的無限可能



一個人可以被很多原因打散 預設 限制

但只有自己可以構建自己

2014年1月20日 星期一

[JPA] @Inheritance, @DiscriminatorColumn and @DiscriminatorValue

@Entity
@Table(name = "car")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Car { .... }


car table 中的資料可以以 type 區別為不同意義的物件

例如可以建立下列物件:

@Entity
@DiscriminatorValue("bus")
public class Bus extends Car { ... }

type = bus 的物件


@Entity
@DiscriminatorValue("truk")
public class Truk extends Car { ... }

type = truk 的物件


@DiscriminatorValue("xxxx" )
會自動存入 type = xxxx


[Spring] Directory pattern

/* 
match only one directory
ex: /a

/** 
match multiple directories 表示該目錄下且包含子目錄
ex: /a/b/c


[Java] Entity, POJO and DAO

Entity
對應 DB 的 object

POJO plain ordinary java object
不具備任何用途 用於不同地方 就是該角色

DAO data access object
訪問數據庫方法的 interface


2014年1月7日 星期二

[Spring] @Component, @Scope

  • 標注了 @Component,@Service,@Controller,@Repository 的 class,定義為 bean 也就由 Spring 管理 class lifecycle (creation/destruction)。
  • 作用和在 XML 文件中使用 <bean> 一樣。


@Service
  • 表示是邏輯層物件。
@Controller
  • 表示是控制層物件, 如 struts 中的 action。
@Repository
  • DAO 物件。
@Component
  • 泛指沒有明確歸類的元件。


@Component is a generic stereotype for any Spring-managed component.
@Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.


@Scope
  • Spring container 要回傳哪個 bean instance。
  • singleton(default), prototype, request, session, globalSession。
    • singleton: container 中只存在一個 instance。
    • prototype: 每個取得都是新的 instance



* Reference
- 4.4 Bean scopes
- Spring Bean Scopes Example
- What's the difference between @Component, @Repository & @Service annotations in Spring?

[JPA] PersistentObjectException: detached entity passed to persist: xxx.xxx

Error Message

PersistentObjectException: detached entity passed to persist: xxx.xxx


Solution

拿掉 @OneToOne 的 cascade attribute


[J2EE] 2PC

There are two types of datasources suppported by JBoss

1. xa-datasource
  • XADataSource is a feature of the JDBC 2.0 Optional Package (formerly Standard Extension API).
  • If you're clustering your application servers or wanting to use distributed transactions among multiple application servers, then you should use
  • XA gets involved when you want to work with multiple resources, ex:
    • 2 or more databases
    • a database and a JMS connection
    • all of those plus maybe a JCA resource - all in a single transaction. (?)
2. tx-datasource
  • Most stuff in the world is non-XA, ex:
    • a Servlet or EJB or plain old JDBC in a Java application talking to a single database. 

Two Phase Commit (2PC)
  • 分散式資料庫為保持各個資料庫數據一致的協定。
    • 對於分散式資料庫系統來說,因為資料庫的資料是分散儲存在各地的資料庫伺服器,當交易需要執行交付時,如果馬上執行,若任何一個資料庫伺服器沒有完成交付,就有可能造成整個分散式資料庫的資料不一致。
  • 也支援 individual resource。
  • 概念為一個失敗則全部失敗。
    • 將交付分為兩個階段來執行。

Phases

1. 協調者(Coordinator)送出SECURE訊息給各資料庫伺服器,此時各資料庫伺服器交付分散儲存各處的資料庫伺服器可以執行交付,否則將放棄此交易。


2. 當各資料庫伺服器都回覆可執行交付後,協調者才送出交付訊息,讓各資料庫伺服器執行交付交易,並且回覆是否成功,如果有任何一個資料庫伺服器回覆錯誤,協調者就會送出復原訊息取消交易。


* Reference
- DataSource configuration
- 二階段交付

[J2EE] JCA and cglib

J2EE Connector Architecture (JCA)
A resource manager integration API whose goal is to standardize access to non-relational resources in the same way the JDBC API standardized access to relational data.

cglib (Code Generation Library)
Used to extend JAVA classes and implements interfaces at runtime.


* Reference
- Chapter 7. Connectors on JBoss
- cglib

[Linux] shell

shell (殼程式)
  • 只要能夠操作應用程式的介面都能夠稱為殼程式。
  • 狹義的殼程式指的是指令列方面的軟體,ex: bash 等。 
  • 廣義的殼程式則包括圖形介面的軟體!因為圖形介面其實也能夠操作各種應用程式來呼叫核心工作。


shell 依據發展者的不同有許多的版本:
  • Bourne shell (sh)
    • 第一個流行的 shell 是由 Steven Bourne 發展出來的,所以稱為 Bourne shell (sh)。
  • C SHell (csh)
    • 預設在 Sun 裡頭的
    • 是由柏克萊大學的 Bill Joy 設計依附於 BSD 版的 Unix 系統中的 shell ,這個 shell 的語法有點類似 C 語言,所以才得名。
    • 由於在學術界 Sun 主機勢力相當的龐大,而 Sun 主要是 BSD 的分支之一,所以 C shell 也是另一個很重要而且流傳很廣的 shell 之一 。
  • Linux 使用的這一種版本就稱為『 Bourne Again SHell (簡稱 bash) 』
    • Bourne Shell 的增強版本。
    • 基準於 GNU 的架構下發展出來的。
    • 另外還有商業上常用的 K SHell、TCSH 等等。


shell script (程式化腳本)
  • 是利用 shell 的功能所寫的一個『程式 (program)』。
  • 這個程式是使用純文字檔,將一些 shell 的語法與指令(含外部指令)寫在裡面, 搭配正規表示法、管線命令與資料流重導向等功能,以達到我們所想要的處理目的。


#!/bin/bash
  • 在宣告這個 script 使用的 shell 名稱
    • 其他還如: 
      • #!/bin/sh
      • #!/bin/ksh
      • #!/usr/bin/php
  • 宣告這個檔案內的語法使用 bash 的語法!那麼當這個程式被執行時,他就能夠載入 bash 的相關環境設定檔 (一般來說就是 non-login shell 的 ~/.bashrc)


shell script 的預設變數 $0, $1, $2...
ex:
/path/to/scriptname opt1 opt2 opt3...
             $0               $1    $2    $3

$# :代表後接的參數『個數』,以上表為例這裡顯示為『 4 』;
$@ :代表『 "$1" "$2" "$3" "$4" 』之意,每個變數是獨立的(用雙引號括起來);
$* :代表『 "$1c$2c$3c$4" 』,其中 c 為分隔字元,預設為空白鍵, 所以本例中代表『 "$1 $2 $3 $4" 』之意。


* Reference
- 第十三章、學習 Shell Scripts

2014年1月6日 星期一

[J2EE] web.xml

/WEB-INF/web.xml

The deployment descriptor file describes how to deploy a web application in a servlet container such as Tomcat..


What are web.xml doing:
  • To determine how URLs map to servlets.
  • Which URLs require authentication.
  • Describes the classes, resources and configuration of the application and how the web server.


* Reference
- Web.xml
- The Deployment Descriptor: web.xml
- web.xml - DTD and XSD

hostname and domain

hostname 主機名稱 and domain name 領域名稱

Fully Qualified Domain Name, FQDN 兩者組成的完整主機名稱

format: hostname.domain.com

ex: 以區域號碼來區分相同的電話號碼

(02)2600-1234

區碼 (02) => domain name
電話號碼 (26001234) => hostname

=> 26001234.02

ex: www.ksu.edu.tw

domain name     hostname
.tw                    edu               -> .edu.tw tw 下有其他更小網域的主機 ex: com, gov, edu...
.edu.tw             ksu                -> .ksu.edu.tw
.ksu.edu.tw       www              -> www.ksu.edu.tw

hostname: 主機名稱(位置)
domain name: 指定是哪個(區域的) hostname



* Reference
- Difference between host name and domain name

2014年1月3日 星期五

[Spring] 使用 Properties

有以下兩種做法:

1. web.xml 宣告



    




在程式中可以這樣使用

. Autowired Properties

@Autowired private Properties applicationProperties;
String fileRootPath = applicationProperties.getProperty(PROPERTY_FTP_UPLOAD_PATH);


2. context:property-placeholder + @Value

. 以前的寫法

    
        
            WEB-INF/classes/config/properties/database.properties
            classpath:config/properties/database.properties
        
    
    



    
    
    
    



. 用 PropertyPlaceholderConfigurer 可以簡化成一行:






...
    



程式中引用

@Value("${url}")
private String url


* Reference
- Spring Util:Properties Injection via Annotations into a bean

2014年1月2日 星期四

[Web] attribute 命名中包含句點

model.put("aaa.bbb", "xxx");

在 jsp 中用 ${aaa.bbb} 不會顯示任何東西 會被視為 bbb 是 class aaa 中的 attribute

必須要寫成

${requestScope['aaa.bbb']}


* Reference
- Dots in attribute names

[Spring] @Async


// 此 method 會再另一個 thread 中被執行.
@Async
public void execute() { // TODO }


在 applicationContext.xml 中要宣告:


    





* Reference
- Asynchronous method invocation in Spring 3.0