Total Pageviews

2018/01/13

[Win 10] 如何解決磁碟使用率常 100% 的情況

Problem
當我在使用 Windows 10 的時候,從工作管理員發現,磁碟使用率常滿載,一直 90 ~ 100%,嚴重影響整體效能

How-To
解決方式分成兩方面:


在 C 槽按右鍵,將以下 checkbox uncheck,按下確定即可

到控制台 -> 服務,找到 Windows Search 此服務,將其 Stop 並將啟動類型設為手動

2018/01/12

[JSON] Jackson Annotations for JSON

Assume I have two Java beans:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  package test.albert.jackson;
    
  import lombok.Builder;
  import lombok.Data;
    
  @Data
  @Builder
  public class Employee {
    
      private Integer id;
    
      private String name;
    
      private String email;
    
      private String phone;
        
      private Address address;
   
  }


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  package test.albert.jackson;
    
  import lombok.Builder;
  import lombok.Data;
    
  @Data
  @Builder
  public class Address {
    
      private String streetAddress;
      private String city;
      private String zipCode;
    
  }
    


I am using ObjectMapper to convert object into JSON string, the example code is as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  package test.albert.jackson;
    
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.ObjectMapper;
    
  import lombok.extern.slf4j.Slf4j;
    
  @Slf4j
  public class JacksonTest {
    
      public static void main(String[] args) throws JsonProcessingException {
          Address address = Address.builder().zipCode("110").city("台北市").streetAddress("信義區信義路五段7號").build();
          Employee albert = Employee.builder().id(1).name("Albert").email("albert@gmail.com").phone("0900123456")
                  .address(address).build();
    
          ObjectMapper mapper = new ObjectMapper();
          String json = mapper.writeValueAsString(albert);
          log.debug(json);
      }
    
  }
    

The generated JSON looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
 "id": 1,
 "name": "Albert",
 "email": "albert@gmail.com",
 "phone": "0900123456",
 "address": {
  "streetAddress": "信義區信義路五段7號",
  "city": "台北市",
  "zipCode": "110"
 }
}


If I would like to ignore id, just add @JsonIgnore to id



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  package test.albert.jackson;
  
  import lombok.Builder;
  import lombok.Data;
  
  @Data
  @Builder
  public class Employee {
  
      @JsonIgnore
      private Integer id;
  
      private String name;
  
      private String email;
  
      private String phone;
  
      private Address address;
  
  }

If I would like to get rid of id and phone in Employee, add 
@JsonIgnoreProperties({"id", "phone"}) to Employee class:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  package test.albert.jackson;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  
  import lombok.Builder;
  import lombok.Data;
  
  @Data
  @Builder
  @JsonIgnoreProperties({"id", "phone"})
  public class Employee {
  
      private Integer id;
  
      private String name;
  
      private String email;
  
      private String phone;
  
      private Address address;
  
  }


If I want to remove address from Employee class, just add @JsonIgnoreType to Address class.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  package test.albert.jackson;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreType;
  
  import lombok.Builder;
  import lombok.Data;
  
  @Data
  @Builder
  @JsonIgnoreType
  public class Address {
  
      private String streetAddress;
      private String city;
      private String zipCode;
  
  }

2018/01/11

[Win 10] 如何解決憑證匯入失敗, "權限不足" 的錯誤

Problem
當我要將國泰證券的憑證匯入到新電腦時,IE 瀏覽出現 "權限不足" 的錯誤訊息




How-To
以系統管理員身分執行即可以解決此權限不足的問題




2018/01/10

[Maven] How to use local repository?

Problem
How to use local repository in Maven?

How-To


Approach 1. Add localRepository tag in your settings.xml
1
2
3
4
5
6
7
8
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:SchemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
          
   <localRepository>C:\Users\albert\.m2</localRepository>
   
</settings>


Approach 2. Assign -Dmaven.repo.local in command line
1
mvn -o -Dmaven.repo.local=C:\Users\albert\.m2" clean install -Dmaven.test.skip=true -X



Reference
[1] https://stackoverflow.com/questions/9123004/maven-is-it-possible-to-override-location-of-local-repository-via-the-use-of-co

2018/01/09

[GitLab] How to build jar file automatically

Problem
If I hope GitLab can build a jar file when I commit source code, how to do it?
I am using Maven 3.3.3 and JDK 7 in eclipse, and using mvn clean install -Dmaven.test.skip=true to build WAR file in eclipse.

How-To

Step 1. Add .gitlab-ci.yml to the root directory of my project, the content looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
image: maven:3.3.3-jdk-7
    
build:
  stage: build
  script:
   - "mvn clean install -Dmaven.test.skip=true"
  artifacts:
    expire_in: 1 week
    when: on_success
    paths:
    - target/*.jar

Step 2. Add, commit and push .gitlab-ci.yml to GitLab
Step 3. Seeing the status of your pipeline and jobs 


Reference

[1] https://docs.gitlab.com/ce/ci/quick_start/README.html

2018/01/08

[GitLab] How to build WAR file automatically

Problem
If I hope GitLab can build a WAR file when I commit source code, how to do it?
I am using Maven 3 and JDK 7 in eclipse, and using mvn clean package -U to build WAR file in eclipse.

How-To

Step 1. Add .gitlab-ci.yml to the root directory of my project, the content looks like: 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
image: maven:3-jdk-7

build:
  stage: build
  script: "mvn clean package -U"
  artifacts:
    expire_in: 1 week
    when: always
    paths:
    - target/*.war

Step 2. Add, commit and Push .gitlab-ci.yml to GitLab
Step 3. Seeing the status of your pipeline and jobs 

Reference

[1] https://docs.gitlab.com/ce/ci/quick_start/README.html
[2] https://hub.docker.com/r/library/maven/tags/

2018/01/07

[Maven] Cannot find IBM-related jar files in Maven Central Repository

Problem
If I would like to use some proprietorial jar files (i.e. IBM DB2 jdbc jar file, IBM MessageQueue jar files) in my Maven project, and add them into dependency. 
How to do it?

How-To

Step 1. Download jar files, ex. DB2 jdbc jar file
Step 2. Launch command prompt, change directorty to db2 jar file and execute the following command:
mvn install:install-file -Dfile=db2jcc4-4.22.29.jar -DgroupId="com.ibm.db2.jcc" -DartifactId="db2jcc4" -Dversion="4.22.29" -Dpackaging="jar"

Step 3. Add dependency to pom.xml
    <dependency>
        <groupId>com.ibm.db2.jcc</groupId>
        <artifactId>db2jcc4</artifactId>
        <version>4.22.29</version>
    </dependency>


Step 4. Go to .m2 folder and copy com/ibm/db2/jcc/db2jcc4 to your project, for example:


Step 5. Add repository setting in your pom.xml
    <repositories>
        <repository>
            <id>my-local-repo</id>
            <!-- ${basedir} represents the directory containing pom.xml -->
            <url>file://${basedir}/local-repo</url> 
        </repository>
    </repositories>




2018/01/06

[Git] fatal:LF would be replaced by CRLF

Problem
As I try to use the git add command adds a change in the working directory to the staging area, it show the error:
fatal: LF would be replaced by CRLF


Here has a post to solve this problem: https://albert-kuo.blogspot.tw/2016/12/git-fatal-lf-would-be-replaced-by-crlf.html

But if I have hundreds of file have this problem, how to solve this problem?


Solution

Step 1. download file from http://www.bastet.com/uddu.zip

Step 2. Unzip and copy UNIX2DOS.EXE and UNIX2DOS.C to the root directory of your project.


Step 3. Assign the file extension you would like to do replacement in the command and execute this command:

for /f %f IN ( 'dir /b /s *.xml *.pom *.sha1' ) DO @unix2dos %f

Step 4. Remove  UNIX2DOS.EXE and UNIX2DOS.C from your project.

2018/01/05

[Fortify] Fix Unreleased Resource: Database

Before
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.dbutils.QueryRunner;

public class DaoBase {

    public Connection getConnection() throws SQLException {
        return MyDataSource.getInstance().getDatasource().getConnection();
    }

    public List<Map<String, Object>> find(String sql) throws SQLException {
        List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);

            // ...
        } catch (SQLException e) {
            throw e;
        } finally {
            if(rs != null) {
                rs.close();
            }
            if(stmt != null) {
                stmt.close();
            }
            if(con != null) {
                con.close();
            }
        }

        return datas;
    }
}



After
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.dbutils.QueryRunner;

public class DaoBase {

    public Connection getConnection() throws SQLException {
        return MyDataSource.getInstance().getDatasource().getConnection();
    }

    public List<Map<String, Object>> find(String sql) throws SQLException {
        List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);

            // ignore...
        } catch (SQLException e) {
            throw e;
        } finally {
            close(rs);
            close(stmt);
            close(con);
        }

        return datas;
    }

    public void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
    }

    public void close(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
    }

    public void close(Connection con)  {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }
}




2018/01/04

[Fortify] Fix SQL Injection

Before

code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    public List<AccountDtl> getAcctDetail(List<String> acctNos) throws SQLException {

        List<AccountDtl> result = new ArrayList<AccountDtl>();

        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String accounts = "";
        for (String account : acctNos) {
            accounts = accounts + "'" + account + "',";
        }
        
        String sql = "select DISTINCT ( TRIM(REC_ID))  , NO , ID from " + Table.test.getFullName()
                + " where acct_no in (" + accounts.substring(0, accounts.length() - 1) + ")";
        
        try {
            conn = getConnection();
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            // ignore code snippet regarding get values 
        } catch (SQLException e) {
            throw e;
        } finally {
            close(rs);
            close(pst);
            close(conn);
        }

        return result;
    }


After


Add dependency to your pom.xml
1
2
3
4
5
    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>2.1.0</version>
    </dependency>



Updated code snippet (If you are not using DB2 as your database, please apply other Codec, ex. OracleCodec):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
     import org.owasp.esapi.ESAPI;
     import org.owasp.esapi.codecs.DB2Codec;

     // ignore code snippet
     
     public List<AccountDtl> getAcctDetail(List<String> acctNos) throws SQLException {

        List<AccountDtl> result = new ArrayList<AccountDtl>();

        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String accounts = "";
        for (String account : acctNos) {
            accounts = accounts + "'" + ESAPI.encoder().encodeForSQL(new DB2Codec(), account) + "',";
        }
        
        String sql = "select DISTINCT ( TRIM(REC_ID))  , NO , ID from " + Table.test.getFullName()
                + " where acct_no in (" + accounts.substring(0, accounts.length() - 1) + ")";
        
        try {
            conn = getConnection();
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            // ignore code snippet regarding get values 
        } catch (SQLException e) {
            throw e;
        } finally {
            close(rs);
            close(pst);
            close(conn);
        }

        return result;
    }



2018/01/03

[閱讀筆記] Peak and Valleys (1/2)


  1. 知識不僅僅是擁有它,還要知道怎麼使用它
  2. 真正的發現之旅不在於尋找新風景,而是擁有新眼光
  3. 世界上的每個人、每個地方,在生活上、在工作中,本來就會有高峰和低谷
  4. 人們總是會希望儘速離開低谷;在高峰待久一點;在未來能夠擁有較多的高峰、較少的低谷
  5. 當你在幫助別人的同時,其實你也在幫助你自己
  6. 無論在工作或是人生,你一定會遇到無數的高峰與低谷。順境或逆境,可能持續幾分鐘、幾個月、甚至更久。
  7. 個人的高峰與低谷猶如地球表面上的高峰與低谷,貌似分散,實際上又彼此相連。你可能在工作上感覺是高峰,但生活上感受低谷,這是很自然的事,世界上的每個人都如此
  8. 高峰與低谷不僅僅是發生在你身上的順境或逆境的時刻,還包含你內心的感受以及你如何對外部事件的反應
  9. 你內心深處如何感受,端視於你如何看待你目前的處境。即便是壞事降臨在你身上,你內心還是能保持良好狀態
  10. 高峰與低谷彼此是相連的。你在高峰時期犯的錯誤,會造成明日的低谷。你在低谷時期做的明智的事情,會造就明日的高峰
  11. 太多人無法有效管理他們的高峰時期,沒有注意到他們正在造成未來的低谷。他們浪費太多資源,遠離初衷,忽略什麼是最重要的事
  12. 我們會感到快樂與痛苦,待在低谷(valleys)的時間可能比我們想像得還要長,但這些都免不了,人生就是如此。高峰(peaks)和低谷(valleys)不僅僅代表快樂與痛苦,同時也是自我心境的反應,換句話說,只要能改變自己的心境,就能越快走出低谷。
  13. 感恩與渴求也是一種高峰與低谷的展現,當你每天感恩自己擁有的東西,你就處於高峰;當你渴求自己所缺少的東西,就處於低谷。
  14. 如果你能避免比較,你就會擁有較少的低谷;若你能享受愉快的時光,你就會感覺你在高峰
  15. 你無法控制外部事件,但你可以藉由你想的與你所做的,來控制你個人的高峰與低谷
  16. 要將低谷改變成高峰,你必須要改這兩件事情中的一件:若你可以改變現況,那是最好的狀況;如果你無法改變現況,你可以改變你的想法,選擇較好的想法通常會讓你得到較佳的結果
  17. 當你改變看待事情的角度,離開低谷的路就會出現。當兩位被前公司資遣的面試者前來面試,一位看起來被受不公平對待且不斷在抱怨前雇主如何無底對待的面試者,與另外一位即便是在低谷但仍保持正面態度、想要追求新的機會的面試者,你會選哪一位呢?
  18. 在低谷時,記得將低谷視為一個契機,發掘其中隱藏的、之前未發覺的問題,想辦法改善問題,讓事情變得更好,就會從低谷走向高峰
  19. 高峰與高峰之間所間隔的總是低谷,如何管理與改善你的低谷,決定你多快離開低谷、走向下一個高峰
  20. 如果你無法在低谷中學習到教訓,你會變得易怒、怨天尤人;若你可以從低谷中學習到可貴的事物,你會變得更好,並找到離開低谷的道路
  21. 當遇到棘手問題一直無法解決時,先抽離當下,休息一下、反思一下,經過一夜好眠或是幾天的休息之後,你獲得重新注入的能量,原本的問題就可迎刃而解
  22. 當你心存感激且明智地管理你的 good times,你就可以擁有較少的 bad times
  23. 你的自我(ego)會讓你在高峰時感到自負,在低谷時感到恐懼。這會讓你看不見真實情況,你的自我(ego)會扭曲事實。當你在高峰時,你的自我(ego)會讓你過度樂觀,當你在低谷時,你的自我(ego)會讓你過度悲觀。讓你覺得高峰會永遠持續,讓你恐懼低谷不會結束