Implements equals() for non-primitive types

Posted on January 21st, 2015

這次遇到一個簡單到不行的 Java 小地雷,

實作物件的 equals() 時, 若要比對 non-
primitive
type
Integer, Long, Double 等物件數值,

請記得使用 equals() 不要用 == ,

或者自己將 Integer.intValue()Long.longValue() 拿出來再用 ==

不然 boolean eq = new Integer(1) == new Integer(1) 會永遠給你 false,

除非夠幸運兩個 Object 發生 conflict XD

這是 Integer.equals(obj) 的實作, 也就是為什麼 equals(obj) 才會正確的原因


    private final int value;

    public boolean equals(Object obj) {
         if (obj instanceof Integer) {
             return value == ((Integer)obj).intValue();
         }
         return false;
     }

參考資料

另外參考了 stackoverflow Compare non-
primitive
Long values 127 and 128
後, 發現 -128127 之間的數值 jvm 會先 cache 起來 ,

如下程式碼:


        Long a = Long .valueOf(127);
        Long b = Long .valueOf(127);
        boolean eq = a == b; // 是 true

        Long a = Long .valueOf(128);

        Long b = Long .valueOf(128);

        boolean eq = a == b; // 是 false

        boolean eq = new Long(1) == new Long(1); // 但這一樣永遠是 false !

小結:

  1. == 去比較 new Long(long) 的數值都不會相等,

  2. == 去比較 Long.valueOf(long) 的數值只有在 -128 到 127 之間會相等

  3. 所以若要比較 non-
    primitive
    type
    (ex: Integer, Long, Double) 的數值, 請用 equals(obj)


透過 SQL 查詢樹狀結構某節點下的所有節點

Posted on December 26th, 2014

透過 SQL 查詢 Oracle 樹狀結構某節點下的所有節點

  • ORACLE 限定方法: START WITH ... CONNECT BY PRIOR ...
  • Oracle ex:
SELECT Id
FROM Category START WITH Id=2 CONNECT BY
PRIOR Id=Parentid

透過 SQL 查詢 MSSQL 樹狀結構某節點下的所有節點

  • MSSQL 似乎沒有直接的查詢語法(?), 但有 with
  • MSSQL ex:
WITH n (id, name) AS
    (SELECT id, name
    FROM category
    WHERE id = 2 --父節點 id
    UNION ALL SELECT child.id, child.name
    FROM Category AS child, n
    WHERE n.id = child.parentId)

SELECT * FROM n

如果要透過 n 再查詢, 例如查詢分類樹底下所有 binder
(假設 BinderCategory 透過 Binder_Category 來記錄關聯性)

WITH n (id, name) AS (SELECT id, name FROM category WHERE id = 2 --父節點 id UNION ALL SELECT child.id, child.name FROM Category AS child, n WHERE n.id = child.parentId) SELECT * FROM Binder bb LEFT JOIN Binder_Category bc ON bc.binder_id = bb.id WHERE bc.cat_id IN (SELECT id FROM n)

(感謝 April 大大教學)


Eclipse 一些好用的快捷鍵

Posted on December 8th, 2014

用了這款 IDE 好幾年, 最近才學會的一些好用的快捷鍵, 把它記錄下來:

  • Incremental Find: Ctrl + j, 按完快捷鍵後轉換mode, 直接輸入要搜尋的文字會跳持續到符合的位置, 若有多個符合可以用方向鍵選取
  • Find Next: Ctrl + k, 將文字圈選後, 按下快捷鍵直接搜尋下一個
  • Last Edit Position: Ctrl + Q, 跳到整個 workspace 工作階段最後編輯的位置
  • Goto Line: Ctrl + L, 跳到某一行
  • Find Method/field: Ctrl + o, 搜尋 Class 的函式或屬性
  • Matching Brace: Ctrl + Shift + p, 找到對應的大中小括號
  • Column Edit Mode: Shift + Alt + A, 切換編輯模式

補充

Windows8/Windows8.1 的內建新注音輸入法, 預設會啟用 Ctrl 來輸入標點符號,
這會導致無法在 Eclipse 中使用 Block Comment (Ctrl + Shift
+ /)的功能, 會變成輸入全形的問號 ,

  • 關掉的方法: 對右下角工作列的 右鍵->內容->進階->標點符號輸入設為不使用

InstaSearch

另外分享一個程式碼全文搜尋用的 Plug-in: InstaSearch

  • Marketplace 連結: http://marketplace.eclipse.org/content/instasearch
  • 搜尋速度比 Ctrl + h 的全文檢索快上一大截
  • 同樣能針對檔案型態進行搜尋: 例如: *.java, *.xml, *.js ...
  • 但因為模糊比對, 常會出現太多結果而找不到, 所以有好有壞...
    • (第一次在 workspace 使用 InstaSearch 時, 會花上一點時間製作索引)

Eclipse 的啟動參數 data

Posted on November 25th, 2014

在 Windows 上, eclipse 的捷徑可以加上啟動參數 -data ${workspace},

讓啟動時直接指定 Workspace 開啟

如此就不用每次打開會要先選 workspace, 選完還得跑個10秒才出現
Ex: C:\eclips\eclipse.exe -data "C:\workspace\WRA012345"

若搭配 Launchy, 將捷徑名稱設定好, 整個就超方便
例如: 捷徑命名為 eclipse_WRA012345.lnk
只要按下快捷鍵打開 launchy 並打 WRA 3個英文字就可以選到這個專案並開啟
(因為 Launchy 有模糊比對的功能)
Launchy 在 Win8/8.1 上尤其好用!


刪除金山雲U盤 (我的電腦捷徑) (小米WiFi / 金山云U盘)

Posted on November 20th, 2014

移除金山雲U盤方法(2015年1月6日更新)

找到 C:\Program Files (x86)\XiaoMi\MiWiFi\kuaipan 目錄底下的 uninstall.exe

執行它就是對雲U盤的完整移除, 如此就不需要處理我的電腦中的捷徑了


  1. 執行: regedit
  2. 找到: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace\{01249E9F-88FF-45d5-82DB-A1BEE06E123C}
  3. 然後刪掉它 (如果怕有問題可以先對它按右鍵->匯出, 將 reg 備份下來)
  4. 再重新啟動 explorer.exe 或是直接重開機, 應該就成功了

P.S.

  • 我自己嘗試時, 雖然最後一個機碼不相同, 但刪除機碼後捷徑也會消失.
  • 重新啟動後圖示又出現了, 但可對其移除.

REF:

  1. 小米論壇
  2. 小米論壇

Tomcat 弱點 Apache Tomcat Default Error Page Version Detection 與修復方式

Posted on November 7th, 2014

該弱點 Apache Tomcat Default Error Page Version Detection
意思是在 tomcat 的預設錯誤頁會呈現 Tomcat 版本, 可能讓攻擊者得知該版本有什麼弱點並藉此攻擊

這個弱點修復要替換 jar 檔 catalina.jar 中的檔案: org/apache/catalina/util/ServerInfo.properties

原本這行是: server.info=Apache Tomcat/7.042

把版本號拿掉變成: server.info=Apache Tomcat

如附件截圖, 修改後就不會在 Default Error Page 出現 Tomcat 版本



MSSQL Server 對已經存在的 table 增加識別 id 欄位 (從1開始)

Posted on November 7th, 2014

Sql Server add auto increment primary key to existing table

No - you have to do it the other way around: add it right from the get go as INT IDENTITY - it will be filled with identity values when you do this:

ALTER TABLE dbo.YourTable
  ADD ID INT IDENTITY

and then you can make it the primary key:

ALTER TABLE dbo.YourTable
  ADD CONSTRAINT PK_YourTable
  PRIMARY KEY(ID)

REF: http://stackoverflow.com/questions/4862385/sql-server-add-auto-increment-primary-key-to-existing-table


SQL Server:匯入資料遭遇“文字已截斷”錯誤

Posted on November 7th, 2014

匯入大檔, 錯誤大概都會長這樣...

原文告訴大家改用一般檔案來源, 且匯入時對應的欄位型態要修改!

以下整理自原文的建議:

  1. 資料來源選擇: 一般檔案來源

  2. 選好欲匯入的檔案,左側點選 進階

  3. 將文字欄位的 DataType 改成 文字資料流[DT_TEXT]

  4. 將整數欄位的 DataType 改成 二位元組不帶正負號的整數[DT_UI2]

圖片來源: http://lanfar.pixnet.net

REF: SQL Server:匯入資料遭遇“文字已截斷"錯誤 from 蘭花❉心得報告


在 Tomcat 關閉已被證實不安全的 SSLv3 protocol, 並改用 TLS

Posted on October 23rd, 2014

  1. 修改檔案 Tomcat7\conf\server.xml

  2. connector 增加 attribute: sslEnabledProtocols="TLSv1,TLSv1.1,TLSv1.2" , 並拿掉 cipher 中非 TLS 的項

修改前


               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
                  keystoreFile="tomcat.keystore" keystorePass="intumit"
                  ciphers="SSL_RSA_WITH_RC4_128_SHA,
                  TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
                  TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA,
                  SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA" />

修改後


               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
                  keystoreFile="tomcat.keystore" keystorePass="intumit"
                  sslEnabledProtocols="TLSv1,TLSv1.1,TLSv1.2"
                  ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,
                  TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
                  TLS_DHE_DSS_WITH_AES_128_CBC_SHA" />

修改前 sslscan 掃描

                   _
           ___ ___| |___  ___ __ _ _ __
          / __/ __| / __|/ __/ _` | '_ \
          \__ \__ \ \__ \ (_| (_| | | | |
          |___/___/_|___/\___\__,_|_| |_|

                  Version 1.8.2-win
             http://www.titania.co.uk
        Copyright Ian Ventura-Whiting 2009
    Compiled against OpenSSL 0.9.8m 25 Feb 2010

Testing SSL server 10.254.1.233 on port 443

  Supported Server Cipher(s):
    Rejected  SSLv2  168 bits  DES-CBC3-MD5
    Rejected  SSLv2   56 bits  DES-CBC-MD5
    Rejected  SSLv2  128 bits  IDEA-CBC-MD5
    Rejected  SSLv2   40 bits  EXP-RC2-CBC-MD5
    Rejected  SSLv2  128 bits  RC2-CBC-MD5
    Rejected  SSLv2   40 bits  EXP-RC4-MD5
    Rejected  SSLv2  128 bits  RC4-MD5
    Rejected  SSLv3  256 bits  ADH-AES256-SHA
    Rejected  SSLv3  256 bits  DHE-RSA-AES256-SHA
    Rejected  SSLv3  256 bits  DHE-DSS-AES256-SHA
    Rejected  SSLv3  256 bits  AES256-SHA
    Rejected  SSLv3  128 bits  ADH-AES128-SHA
    Accepted  SSLv3  128 bits  DHE-RSA-AES128-SHA
    Rejected  SSLv3  128 bits  DHE-DSS-AES128-SHA
    Accepted  SSLv3  128 bits  AES128-SHA
    Rejected  SSLv3  168 bits  ADH-DES-CBC3-SHA
    Rejected  SSLv3   56 bits  ADH-DES-CBC-SHA
    Rejected  SSLv3   40 bits  EXP-ADH-DES-CBC-SHA
    Rejected  SSLv3  128 bits  ADH-RC4-MD5
    Rejected  SSLv3   40 bits  EXP-ADH-RC4-MD5
    Accepted  SSLv3  168 bits  EDH-RSA-DES-CBC3-SHA
    Rejected  SSLv3   56 bits  EDH-RSA-DES-CBC-SHA
    Rejected  SSLv3   40 bits  EXP-EDH-RSA-DES-CBC-SHA
    Rejected  SSLv3  168 bits  EDH-DSS-DES-CBC3-SHA
    Rejected  SSLv3   56 bits  EDH-DSS-DES-CBC-SHA
    Rejected  SSLv3   40 bits  EXP-EDH-DSS-DES-CBC-SHA
    Accepted  SSLv3  168 bits  DES-CBC3-SHA
    Rejected  SSLv3   56 bits  DES-CBC-SHA
    Rejected  SSLv3   40 bits  EXP-DES-CBC-SHA
    Rejected  SSLv3  128 bits  IDEA-CBC-SHA
    Rejected  SSLv3   40 bits  EXP-RC2-CBC-MD5
    Accepted  SSLv3  128 bits  RC4-SHA
    Rejected  SSLv3  128 bits  RC4-MD5
    Rejected  SSLv3   40 bits  EXP-RC4-MD5
    Rejected  SSLv3    0 bits  NULL-SHA
    Rejected  SSLv3    0 bits  NULL-MD5
    Rejected  TLSv1  256 bits  ADH-AES256-SHA
    Rejected  TLSv1  256 bits  DHE-RSA-AES256-SHA
    Rejected  TLSv1  256 bits  DHE-DSS-AES256-SHA
    Rejected  TLSv1  256 bits  AES256-SHA
    Rejected  TLSv1  128 bits  ADH-AES128-SHA
    Accepted  TLSv1  128 bits  DHE-RSA-AES128-SHA
    Rejected  TLSv1  128 bits  DHE-DSS-AES128-SHA
    Accepted  TLSv1  128 bits  AES128-SHA
    Rejected  TLSv1  168 bits  ADH-DES-CBC3-SHA
    Rejected  TLSv1   56 bits  ADH-DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-ADH-DES-CBC-SHA
    Rejected  TLSv1  128 bits  ADH-RC4-MD5
    Rejected  TLSv1   40 bits  EXP-ADH-RC4-MD5
    Accepted  TLSv1  168 bits  EDH-RSA-DES-CBC3-SHA
    Rejected  TLSv1   56 bits  EDH-RSA-DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-EDH-RSA-DES-CBC-SHA
    Rejected  TLSv1  168 bits  EDH-DSS-DES-CBC3-SHA
    Rejected  TLSv1   56 bits  EDH-DSS-DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-EDH-DSS-DES-CBC-SHA
    Accepted  TLSv1  168 bits  DES-CBC3-SHA
    Rejected  TLSv1   56 bits  DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-DES-CBC-SHA
    Rejected  TLSv1  128 bits  IDEA-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-RC2-CBC-MD5
    Accepted  TLSv1  128 bits  RC4-SHA
    Rejected  TLSv1  128 bits  RC4-MD5
    Rejected  TLSv1   40 bits  EXP-RC4-MD5
    Rejected  TLSv1    0 bits  NULL-SHA
    Rejected  TLSv1    0 bits  NULL-MD5

  Prefered Server Cipher(s):
    SSLv3  128 bits  DHE-RSA-AES128-SHA
    TLSv1  128 bits  DHE-RSA-AES128-SHA

  SSL Certificate:
    Version: 2
    Serial Number: -4294967295
    Signature Algorithm: sha1WithRSAEncryption
    Issuer: 馬賽克
    Not valid before: Feb 19 07:35:50 2014 GMT
    Not valid after: Feb 19 07:35:50 2016 GMT
    Subject: 馬賽克
    Public Key Algorithm: rsaEncryption
    RSA Public Key: (1024 bit)
      Modulus (1024 bit):
          00:ad:e3:69:8a:78:d3:f3:fa:0d:60:33:ca:fe:25:
          94:9b:1e:04:85:36:10:ac:76:63:82:62:61:f8:f5:
          eb:0d:31:69:8c:80:54:91:3e:b5:a3:25:05:0a:22:
          5e:5e:b1:c9:fa:9c:a6:08:71:3c:09:dc:22:7a:ee:
          56:96:2c:b3:57:88:02:b0:9f:7b:7d:72:e7:79:bf:
          1f:d6:a0:85:3f:b0:10:c3:dc:46:86:1c:e4:51:c8:
          7c:d4:c2:fb:42:f7:6e:b3:63:f1:6c:2a:38:7a:29:
          35:16:5a:ee:ff:a2:eb:31:0b:40:7e:96:66:8f:f2:
          ac:5b:29:8b:33:74:cd:84:4b
      Exponent: 65537 (0x10001)
    X509v3 Extensions:
      X509v3 Subject Key Identifier:
        8B:E6:57:28:9C:DE:99:FE:A6:95:15:89:10:5C:85:AC:6F:3D:CB:90
      X509v3 Authority Key Identifier:
        keyid:0C:CE:36:BA:EB:3D:14:35:D7:C5:C3:CA:59:8B:27:81:8F:59:CD:6F

      X509v3 CRL Distribution Points:
        URI:馬賽克
        URI:馬賽克

      Authority Information Access:
        CA Issuers - URI:馬賽克
        CA Issuers - URI:馬賽克

      1.3.6.1.4.1.311.20.2:
        ...W.e.b.S.e.r.v.e.r
      X509v3 Basic Constraints: critical
        CA:FALSE
      X509v3 Key Usage:
        Digital Signature, Key Encipherment
      X509v3 Extended Key Usage:
        TLS Web Server Authentication
  Verify Certificate:
    self signed certificate in certificate chain

修改後 sslscan 掃描

                   _
           ___ ___| |___  ___ __ _ _ __
          / __/ __| / __|/ __/ _` | '_ \
          \__ \__ \ \__ \ (_| (_| | | | |
          |___/___/_|___/\___\__,_|_| |_|

                  Version 1.8.2-win
             http://www.titania.co.uk
        Copyright Ian Ventura-Whiting 2009
    Compiled against OpenSSL 0.9.8m 25 Feb 2010

Testing SSL server blahblah.com on port 443

  Supported Server Cipher(s):
    Rejected  SSLv2  168 bits  DES-CBC3-MD5
    Rejected  SSLv2   56 bits  DES-CBC-MD5
    Rejected  SSLv2  128 bits  IDEA-CBC-MD5
    Rejected  SSLv2   40 bits  EXP-RC2-CBC-MD5
    Rejected  SSLv2  128 bits  RC2-CBC-MD5
    Rejected  SSLv2   40 bits  EXP-RC4-MD5
    Rejected  SSLv2  128 bits  RC4-MD5
    Failed    SSLv3  256 bits  ADH-AES256-SHA
    Failed    SSLv3  256 bits  DHE-RSA-AES256-SHA
    Failed    SSLv3  256 bits  DHE-DSS-AES256-SHA
    Failed    SSLv3  256 bits  AES256-SHA
    Failed    SSLv3  128 bits  ADH-AES128-SHA
    Failed    SSLv3  128 bits  DHE-RSA-AES128-SHA
    Failed    SSLv3  128 bits  DHE-DSS-AES128-SHA
    Failed    SSLv3  128 bits  AES128-SHA
    Failed    SSLv3  168 bits  ADH-DES-CBC3-SHA
    Failed    SSLv3   56 bits  ADH-DES-CBC-SHA
    Failed    SSLv3   40 bits  EXP-ADH-DES-CBC-SHA
    Failed    SSLv3  128 bits  ADH-RC4-MD5
    Failed    SSLv3   40 bits  EXP-ADH-RC4-MD5
    Failed    SSLv3  168 bits  EDH-RSA-DES-CBC3-SHA
    Failed    SSLv3   56 bits  EDH-RSA-DES-CBC-SHA
    Failed    SSLv3   40 bits  EXP-EDH-RSA-DES-CBC-SHA
    Failed    SSLv3  168 bits  EDH-DSS-DES-CBC3-SHA
    Failed    SSLv3   56 bits  EDH-DSS-DES-CBC-SHA
    Failed    SSLv3   40 bits  EXP-EDH-DSS-DES-CBC-SHA
    Failed    SSLv3  168 bits  DES-CBC3-SHA
    Failed    SSLv3   56 bits  DES-CBC-SHA
    Failed    SSLv3   40 bits  EXP-DES-CBC-SHA
    Failed    SSLv3  128 bits  IDEA-CBC-SHA
    Failed    SSLv3   40 bits  EXP-RC2-CBC-MD5
    Failed    SSLv3  128 bits  RC4-SHA
    Failed    SSLv3  128 bits  RC4-MD5
    Failed    SSLv3   40 bits  EXP-RC4-MD5
    Failed    SSLv3    0 bits  NULL-SHA
    Failed    SSLv3    0 bits  NULL-MD5
    Rejected  TLSv1  256 bits  ADH-AES256-SHA
    Rejected  TLSv1  256 bits  DHE-RSA-AES256-SHA
    Rejected  TLSv1  256 bits  DHE-DSS-AES256-SHA
    Rejected  TLSv1  256 bits  AES256-SHA
    Rejected  TLSv1  128 bits  ADH-AES128-SHA
    Accepted  TLSv1  128 bits  DHE-RSA-AES128-SHA
    Rejected  TLSv1  128 bits  DHE-DSS-AES128-SHA
    Accepted  TLSv1  128 bits  AES128-SHA
    Rejected  TLSv1  168 bits  ADH-DES-CBC3-SHA
    Rejected  TLSv1   56 bits  ADH-DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-ADH-DES-CBC-SHA
    Rejected  TLSv1  128 bits  ADH-RC4-MD5
    Rejected  TLSv1   40 bits  EXP-ADH-RC4-MD5
    Rejected  TLSv1  168 bits  EDH-RSA-DES-CBC3-SHA
    Rejected  TLSv1   56 bits  EDH-RSA-DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-EDH-RSA-DES-CBC-SHA
    Rejected  TLSv1  168 bits  EDH-DSS-DES-CBC3-SHA
    Rejected  TLSv1   56 bits  EDH-DSS-DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-EDH-DSS-DES-CBC-SHA
    Rejected  TLSv1  168 bits  DES-CBC3-SHA
    Rejected  TLSv1   56 bits  DES-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-DES-CBC-SHA
    Rejected  TLSv1  128 bits  IDEA-CBC-SHA
    Rejected  TLSv1   40 bits  EXP-RC2-CBC-MD5
    Rejected  TLSv1  128 bits  RC4-SHA
    Rejected  TLSv1  128 bits  RC4-MD5
    Rejected  TLSv1   40 bits  EXP-RC4-MD5
    Rejected  TLSv1    0 bits  NULL-SHA
    Rejected  TLSv1    0 bits  NULL-MD5

  Prefered Server Cipher(s):
    TLSv1  128 bits  DHE-RSA-AES128-SHA

SSL/TLS 設定參考資料

  1. http://en.wikipedia.org/wiki/Transport_Layer_Security#Cipher
  2. http://security.stackexchange.com/questions/19096/how-to-determine-if-a-browser-is-using-an-ssl-or-tls-connection
  3. http://askubuntu.com/questions/537196/how-do-i-patch-workaround-sslv3-poodle-vulnerability-cve-2014-3566
  4. https://blog.eveoh.nl/2014/02/tls-ssl-ciphers-pfs-tomcat/
  5. https://wiki.mozilla.org/Security/Server_Side_TLS

SQL Server Drop All The Tables

Posted on September 17th, 2014

這就是 Stored Procedures, 不過這似乎要有權限才能完成!

EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

Liullen

Notes from my experience.