2013年2月26日 星期二

菩萨蛮—甄嬛传


小山重叠金明灭
鬓云欲渡香腮雪
懒起画蛾眉
弄妆梳洗迟
照花前后镜
花面交相映
新帖绣罗襦
双双金鹧鸪
新帖绣罗襦
双双金鹧鸪
懒起画蛾眉
弄妆梳洗迟
照花前后镜
花面交相映
新帖绣罗襦
双双金鹧鸪
新帖绣罗襦
双双金鹧鸪
双双金鹧鸪

紅顏劫—甄嬛傳

斬斷情絲心猶亂 千頭萬緒仍糾纏

拱手讓江山     低眉戀紅顏  禍福輪流轉 是劫還是緣

天機算不盡  交織悲與歡  古今癡男女 誰能過情關

長恨歌—白居易


漢皇重色思傾國,御宇多年求不得。
楊家有女初長成,養在深閨人未識。
天生麗質難自棄,一朝選在君王側。
回眸一笑百媚生,六宮粉黛無顏色。
春寒賜浴華清池,溫泉水滑洗凝脂。
侍兒扶起嬌無力,始是新承恩澤時。
雲鬢花顏金步搖,芙蓉帳暖度春宵。
春宵苦短日高起,從此君王不早朝。
承歡侍宴無閒暇,春從春遊夜專夜。
後宮佳麗三千人,三千寵愛在一身。
金屋妝成嬌侍夜,玉樓宴罷醉和春。
姊妹兄弟皆列土,可憐光彩生門戶。
遂令天下父母心,不重生男重生女。
驪宮高處入青雲,仙樂風飄處處聞。
緩歌慢舞凝絲竹,盡日君王看不足。
漁陽鼙鼓動地來,驚破霓裳羽衣曲。

念奴嬌 - 赤壁懷古


大江東去,浪淘盡,千古風流人物。
故壘西邊,人道是,三國周郎赤壁。

亂石崩雲,驚濤裂岸,捲起千堆雪。

江山如畫,一時多少豪傑。

遙想公瑾當年,小喬初嫁了,雄姿英發。

羽扇綸巾,談笑間,強虜灰飛煙滅。

故國神遊,多情應笑我,早生華髮。

人間如夢,一尊還酹江月。

2013年2月25日 星期一

UML-類別圖

UML-類別圖

[OO 小學堂] UML 類別圖介紹


關聯 : (Association) 

在一個類別和另一個類別之間拉出連接線、並在上方標示關聯詞(動詞)、二端視需求可標示參與數目和介面名稱,表示二個類別之間的關聯。例如,一或多個「數字」「標記」一種「物品」。參與數目(multiplicity or cardinality)可能標示為數字、表示多項的代用符號(*) 、或是像 "0..1" 的數字區段。可以在關聯詞旁邊以實心三角形標示方向。






彙總 : (Aggregation) 『生死有命diamond表示whole,箭頭表示part

在連接線的一端加上空心菱形,表示一項類別(菱形端)對另一項類別有「整體與部份」的關聯。彙總的意思是 has-a ,通常是強調一項類別(菱形端)的包含了另一項類別的集合:例如,Man 有 身分證, 健保卡...等 (IDCard)。





組合 : (Composition) 『同生共死diamond表示whole,箭頭表示part

在連接線的一端加上實心菱形,表示一項類別(菱形端)除了對另一項類別有「整體與部份」的關聯之外、而且是由另一項類別組成。組合的關聯力比彙總強,Composition表示法與Aggregation的差異在於變成實diamond,其他完全一樣,跟Aggregation在意義的差異在於Composition強調『同生共死』, 以下圖來解釋, 當System物件死亡時,Component物件也要跟著死亡,但aggregation是『生死有命』,當System物件死亡時,Component物件並不特別去處理。






一般化/特殊化 : (Generalization/specialization) 

在連接線的一端加上空心三角形,表示一項類別(三角形端)是另一項類別的一般樣式;相對地,另一項類別是該類別的特殊樣式。一般化的意思是 is-a ,也是繼承的關係:例如,Bus 是一種車子。Bus 和 Taxi 是 Car 的子型(subtype),而Car 是母型(supertype) 。
可注意,一般化也可以用在使用案例圖,用來處理案例或參與者的從屬層次。





實現 : (Realization) 

用帶箭頭的虛線從一項類別拉到另一項類別、並在上方標示用雙角括號 « » 包含的關聯詞(動詞),表示一項類別實現了另一項類別的行為:例如,工程師與經理等角色都需要吃飯, 走路 etc . 故在 IPeople 的介面定義了所有Human being 都會有的行為。






依賴 : (Dependency) 

從一項類別拉出一條帶箭頭的虛線到另一項類別、不帶標示文字,表示一項類別依賴另一項類別提供的功能。

Spring ROO

01 project creation
02 creation and development of domain objects
03 adding fields of different types to the domain objects
04 creating relationships between domain objects
05 automatic creation of integration tests
06 creating workspace artifacts to import the project into your IDE
07 automatic scaffolding of a Web tier
08 running the application in a Web container
09 controlling and securing access to different views in the application
10 customizing the look and feel of the Web UI for our bussiness domain
11 creating and running Selenium tests
12 deployment and backup of your application


01
roo> project --topLevelPackage org.rooinaction.taskmanager --projectName  taskmanager


02
roo> logging setup --level WARN

03
roo> logging setup --level TRACE --package PROJECT

04
roo> jpa setup --database DERBY_EMBEDDED --provider ECLIPSELINK

05
roo> entity jpa --class ~.model.Task --testAutomatically

06
roo> field string --fieldName description --notNull --sizeMax  40

07
roo> field boolean --fieldName completed --value false

08
roo> web mvc setup

09
roo> web mvc all --package ~.web

10
roo> backup

11
$ mvn package jetty:run
roo>perform eclipse
perform eclipse之後,重更新STS中的專案,才可使用run server的指令

12
properties list --name database.properties --path SPRING_CONFIG_ROOT

13
properties set --name database.properties --path SPRING_CONFIG_ROOT --key password --value foob@r

14
focus --class ~.model.Course

15
























2013年2月18日 星期一

Rails 筆記


01
$ rails new tickets

02
rails generate scaffold ticket name:string seat_id_seq:string address:text
                                  price_paid:decimal email_address:string

03
rake db:migrate

04
$ rails g migration AddPhoneToTickets phone:string
注意:要手動更改頁面模板

05
設定哪些欄位值是可以被更改的,否則會出現Can't mass-assign protected attributes
fix app/models/ticket.rb

    class Ticket < ActiveRecord::Base
      attr_accessible :address, :email_address, :name, :price_paid, :seat_id_seq,:phone
    end


06
半自動,建立MODEL
$ rails g model ad name:string description:text price:decimal seller_id:integer email:string img_url:string

07
半自動,建立CONTROLLER
$ rails g controller ads

08
HTML頁面生成的流程,及路由(route)設定
    1.Controller的功用是產生Html所需的資料
    2.View(Page template)
    ERb將controller及view產生出Html

   但ERb要知道在什麼情況下要找哪一個controller和view配對使用,這透過Route來定義!

   Route典型的指定方式:(config/routes.rb)
                     match      "/ads/   :id"  => "ads   #      show"
                                             URL Pattern  參數           Controller                View
                                                                                     ads_controller.rb      show.html.erb
09
Ruby
表達式<%= ......%>  <%= @ad.name %>
腳本段<%   ......%>
            <% for ad in @ads %>
                 <li><a href="/ads/<%= ad.id %>"><%= ad.name %></a></li>
           <% end %>

10
控制器的例子
 # GET /tickets/1
  # GET /tickets/1.json
  def show
    @ticket = Ticket.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @ticket }
    end
  end

  # GET /tickets/new
  # GET /tickets/new.json
  def new
    @ticket = Ticket.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @ticket }
    end
  end










2013年2月17日 星期日

Rails 安裝

http://rubyonrails.org/download

1

Ruby


2
gem install rails

or gem install rails --no-ri --no-rdoc

3
Install the DevKit

  3.1
  Download Development Kit from here http://rubyinstaller.org/downloads/

  3.2
  Unzip the zip
  open command-line and go to unzipperd folder
  3.3
  ruby dk.rb init
  ruby dk.rb install
  gem install rdiscount --platform=ruby

4
 Install IDE

Aptana RadRails



5
從IDE中新增一個Rails專案來測試一下!
或在CMD中用rails new helloWorld



attr_accessible


從 Github 被 Hack,談 Rails 的安全性  Mass-assignment )


Rails 也不是沒有針對這件事設計出防禦措施,有兩組 model API :attr_accessible 與 attr_protected。其實也就是 白名單、黑名單設計。
把 attr_accessible 加在 model 裡,可以擋掉所有 massive assignement 傳進來的值,只開放你想讓使用填寫的欄位。

1
2
3
class Post < ActiveRecord::Base
  attr_accessible :title, :content
end

而 attr_protected 是完全相反地機制。
……

XSS V.S. Massive Assignment

後來睡醒以後才發現不對,其實這東西應該要被拿來跟 auto escape 相比:XSS 是一般設計 Web Application 最容易中招的攻擊。
XSS 的原因肇因是讓開發者開放讓使用者自行輸入內容,然後無保護的讀出來,Hacker 會利用這種漏洞,寫進有害的 JavaScript 讓使用者中招。正確的方式應該是:內容讀出來之後,都要利用 html_escape 濾掉。
問題是,html_escape 濾不勝濾,沒有開發者能夠那麼神,寫任何一段 code 都會自律的加上 h(content)。最後 Rails core 痛定思痛,在 Rails 3.0 後效法 Django 的設計,在讀出 content 時,一律先 escape。除非有必要,才另行設定不需 escape。
我想這次的 massive assignment 問題應該也要比照辦理才對…






2013年2月4日 星期一

子夜讀信 —洛夫

子夜的燈
是一條未穿衣裳的
小河

你的信像一尾魚游來
讀水的溫暖
讀你額上動人的鱗片
讀江河如讀一面鏡
讀鏡中你的笑
如讀泡沫

2013年2月3日 星期日

成人和兒童的區別

成人和兒童的區別,在於玩具的價格不同……

Exception Handling (四)


Best Practices for Exception Handling

先記錄一下!!

Exception Handling (三)


10 Best Practices With Exceptions




  • 1 Relevancy
  •      keep the exceptions you throw relevant to the interface.
  • 2 Encapsulation
  •     throws only exceptions that are relevant to the interface
  • 3 Reason
  •     The exceptions must always say what caused them=>To create a generic exception and describe what caused it
  • 4 Exception names
  •     My advice is to avoid using thejava.lang.Exception as it is too generic and gives too little information about what went wrong.
  • 5 Balance what you catch
  • } catch (ClassCastException e1) {
       ...
    } catch (FileNotFoundException e) {
       ... 
    } catch (IOException e) {
       ...
    }
  • 6 Scoping
  •     How long should a try block be?
  • 7 Use Finally
  • } finally {
        try {
          output.close();
        } catch (Throwable e) {
          e.printStackTrace();
        }
    }
  • 8 Throw only Exceptions
  • 9 Throw early catch late
  •     you should throw an exception as soon as you can and catch it late, wait until you have all the information to handle it properly.
  • 10 Add validation
  • if ( null == variable || variable.isEmpty()){
       throw new blahException("the variable cannot be null at this point");
    }


Exception Handling (二)

承上篇

Exception Handling (一)


再看一個討論的例子,

Java: Error handling in MVC project. How correct implement?

How do you want to handle the error on DAO layer? Return fake data? Empty collection? if you can't, just let the exception pop-up.


And how about handling on the service layer? You know that the database is not working, so what can you return to the Struts action? An empty result? An error object? Isn't exception an error object?
So the exception appears in the Struts action. Here you have some options. If the exception will actually tell something to the user and your GUI is prepared, you can return different view (and log the exception here).
But what if you catch a NullPointerException in Struts action? Will you handle it separately in every Struts action? No, so pass the exception even further (!)
I think you get the idea - as long as you don't know how to handle the exception (and logging is not handling), let the client clean-up the mess. Otherwise you are only hiding the problem and increasing the damages (e.g. transactions aren't rolled-back, users see incorrect results).
I would advice implementing generic exception handling mechanism which logs the exception and returns HTTP 500 to the user. Gentle error message (without stack trace) should appear and the user should be apologized. You should investigate every error that reaches this layer.
As far as I remember Struts (and virtually every web framework) has some centralized way of handling exceptions. (Struts 2 Exception Handling Docs)


Exception Handling (一)

在寫ANDROID上的RESTFUL Client程式時,又有了錯誤處理機制很重要的體認。

什麼才是有效又簡單的方法來處理程式可能會出現的錯誤情況?
目前的想法是分成兩部份,
一、處理
二、反映無法處理,通知接手的類別去處理。例如UI顯示網路連線有問題!
UI必需接受任何拋出的例外,處理並通知使用者!!

參考以下網址並節錄內容:

Java Essence: 要抓還是要丟?

1 、由 於操作檔案讀取的過程中,有許多方法可能丟出受檢例外(Checked Exception),你可能如上使用try..catch加以處理,在catch中以主控台方式輸出錯誤訊息,但問題在於,你並不知道你的程式庫會用在 什麼環境,是文字模式?視窗模式?或是Web應用程式?直接在catch中寫死處理處理例外或輸出錯誤訊息的方式,是一件不符合需求的方式。

2、如果你的方法設計流程中,發生例外時,當時的上下文環境並不知道該如何處理(例如你並不知道程式庫會用在什麼環境下時),那麼你可以丟出例外,讓呼叫你的方法的客戶端來處理。例如:
import java.io.*;
public class FileUtil {
    public static String readText(String file) throws FileNotFoundException {
        String text = null;
        FileReader reader = new FileReader(file)
        ...
        return text;
    }
    ....
}

3、當例外發生時,你可以使用try..catch處理當時環境下所可以作的錯誤處理,對於當時環境下無法決定如何處理的部份,可以丟出去給屆時的客戶端處理。如果你想先處理部份事項再丟出,則一個例子如下:
import java.io.*;
public class FileUtil {
    public static String readText(String file) {
        String text = null;
        FileReader reader = null;
        try {
            reader = new FileReader(file);
            ...
        }
        catch(FileNotFoundException ex) {
            // Logging 或 ex.printstackTrace()
            // 其它處理
            throw ex;
        }
        finally {
            if(reader != null) {
                try {
                    reader.close();
                }
                catch(IOException ex) {
                    // Logging 或 ex.printstackTrace()                    // 其它處理
                    throw ex;
                }
            }
        }

        return text;
    }
    ....
}
在進行完部份錯誤處理之後,你可以使用throw將例外再丟出,當你在流程中丟出例外,就直接跳離原有的流程。如果流程正常執行,最後一定要執行的動作可以放在finally區塊中。

4、一個有趣的問題是,如果程式撰寫的流程中先return了,而你也有寫finally區塊,那finally區塊還會執行嗎?答案是肯定的,finally區塊會先執行完後,再執行return。例如下面這個程式會先顯示「finally...」再顯示「1」:
public class Main {    
    public static int test(boolean flag) {
        try {
            if(flag) {
                return 1;
            }
        }
        finally {
            System.out.println("finally...");
        }
        return 0;
    }
    
    public static void main(String[] args) {
        System.out.println(test(true));
    }
}






聞一多《死水》

聞一多《死水》
這是一溝絕望的死水, 
清風吹不起半點漪淪。 
不如多扔些破銅爛鐵, 
爽性潑你的賸菜殘羹。 

也許銅的要綠成翡翠, 
鐵罐上銹出幾瓣桃花, 
再讓油膩織一層羅綺, 
黴菌給他蒸出些雲霞。 

讓死水酵成一溝綠酒, 
飄滿了珍珠似的白沫; 
小珠們笑聲變成大珠, 
又被偷酒的花蚊咬破。 

那麼一溝絕望的死水, 
也就誇得上幾分鮮明。 
如果青蛙耐不住寂寞, 
又算死水叫出了歌聲。 

這是一溝絕望的死水, 
這裏斷不是美的所在, 
不如讓給醜惡來開墾, 
看他造出個什麼世界。