2015年7月13日 星期一

連線SSL URL出現錯誤 unable to find valid certification path to requested target

參考:SunCertPathBuilderException: unable to find valid certification path to requested target
InstallCert.java

這狀況挺奇怪的,運作已經半年以上的突然出了以下Exception:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExce
ption: unable to find valid certification path to requested target
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.
java:1478)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.jav
a:212)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:957)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:892)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.
java:1363)

出問題的地方在於我要連結一個https的路徑取回資料,參考解法步驟如下:

  1. 將上述連結InstallCert.java 的檔案拿回來存成InstallCert.java
  2. 切換到存放InstallCert.java的路徑,使用javac編譯成.class
  3. cmd:java InstallCert [your target SSL domain]
    執行第一次的時候會有錯誤。再執行一次就可以(總共執行兩次)
  4. 將產生出來的jssecacerts檔案複製到 $JAVA_HOME/jre/lib/secirity
  5. restart your service(ex:tomcat)

2015年4月2日 星期四

再一次Mybatis出現 java.io.IOException: Could not find resource...使用Gradle

參考:
MyBatis loading XML : java.io.IOException: Could not find resource


又再一次出現了惱人的錯誤:
 java.io.IOException: Could not find resource...


這次的情況很詭異,使用eclipse  build的時候一切成功,但是一使用gradle build就出錯了,跟以前不同的是這次build tool改使用了gradle(之前使用ANT或根本沒用)

找到了以上的參考文章,以下的回答:


Note Since you have placed batisConfig.xml under src/main/java. By default maven configures to include only */*.java file( only java files) if need to include all the files you need to configure accordingly. But I would recommend to move the xml file to src/main/resources directory( which is again a classpath)

雖然我並非使用maven,但是其實是一樣的問題,原來預設於src/main/java下都只有include *.java,因此我只要將我的 mapper.xml 移動到 src/main/resources 就可以囉!

2015年3月1日 星期日

使用Android Sudio出現Empty region錯誤

今天第一次嘗試使用Android Studio, 沒多久就出現了詭異的錯誤:
 Empty region 

查了半天才知道原來是AndroidManifest.xml中的
   android:icon=@drawable/xxx 
其中的Resource xxx若是對應的為.ico則會出現此錯誤。 

但是很奇怪的是,雖然出現這錯誤,卻能正確的delpoy到手機上耶! !


p.s:好久沒發文...

2014年12月9日 星期二

mybatis - 如何設定dynamic sql動態因應不同的database環境?

參考:Mybatis databaseIdProvider  Multi-db vendor support
  1. 首先於mybatis-config新增<databaseIdProvider>設定
  2. 
     
      
     
         
     
      
     
    
    
      
      
      
      
    
    
     
      
     
     
    
    
    
  3. 接者在欲使用的dynamic sql部分如下使用:
  4. 
    
    
      
        
          select seq_users.nextval from dual
        
        
          select nextval for seq_users from sysibm.sysdummy1"
        
      
      insert into users values (#{id}, #{name})
    
    
    

2014年11月15日 星期六

AngularJS Scope:use ng-include

此篇文章因為一開始使用mac pages製作,後來再轉成html輸出至此,格式很亂(因為我還不太會用pages),因此code的部分皆使用純文字貼上,請見諒

Scope:use ng-include
此篇所有測試sourceuse hg-include

angularJsScope繼承概念在一般情形下是很直觀的,然而在2-way-data binding的primitive情況下有些必須注意,以下來做些ng-include的小測試:

首先我們準備檔案index.html 
<!DOCTYPE html>
<html ng-app="myApp">
<!-- class="show-scope-demo"-->
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>


<body ng-controller="scopeCtrl as ctrl">
  <h1>Hello!Scope</h1>

  use myPrimitive: <input ng-model="myPrimitive">
  use myObject.aNumber: <input ng-model="myObject.aNumber">

  <p>myPrimitive:{{myPrimitive}}</p>
  <p>myObject.aNumber:{{myObject.aNumber}}</p>

</body>


再來準備script.js 

var app = angular.module('myApp',[]);
app.controller('scopeCtrl' ,['$scope', function($scope){
  $scope.myPrimitive = 50;
  $scope.myObject    = {aNumber: 11};
  
  
  $scope.setMyPrimitive = function(value) {
      $scope.myPrimitive = value;
  }

}]);
執行測試:

  1. 當一開始載入後,一切如我們所想,兩個input輸入框與下方顯示都很完美的反應我們設定的初始值:
  2. 接下來我們試驗者改變兩個輸入框,將它們各自改變為2266,一切如我們預期般執行 

此時內部的假想scope如下:
 




接下來我們試驗使用ng-include方式,首先新增tpl1.html 

myPrimitive:
<input ng-model="myPrimitive">

<br>

再新增tpl2.html 


myObject.aNumber:<input ng-model="myObject.aNumber">

變更原先index.htmlbody部分): 


<body ng-controller="scopeCtrl as ctrl">
  <h1>Use ng-include</h1>

  <div ng-include src="'tpl1.html'"></div>
  <div ng-include src="'tpl2.html'"></div>

  <p>myPrimitive:{{myPrimitive}}</p>
  <p>myObject.aNumber:{{myObject.aNumber}}</p>

</body>

準備完畢,再一次執行測試:

  1. 當一開始載入後,一切如我們所想,兩個input輸入框與下方顯示都很完美的反應我們設定的初始值:
  2. 然而,這次我們一樣改變兩個輸入框,將它們各自改變為2266,卻發生了如下的情況
     

第一個myPrimitive沒有如果們預想的變更成功,這是怎麼一回事?
首先,我們知道使用ng-include會增加新的scope,因此一開始(初始載入後)的內部結構如下:



此時除了原先的RootScopeParentScope以外,又多了兩個ChildScope,分別為ng-include=‘tpl1.html’ng-include=‘tpl12.html’所產生。

而當我們變更輸入框的數值的時候,輸入框2(myObject.aNumber)還是維持與之前一樣的方式,參考至$parent.myObject.aNumber並且更改了他;然而輸入框1(myPrimitive)就不是這麼一回事了,他首先確認所屬的ChildScope1是否擁有myPrimitive這個變數,發現沒有,此時並不會如我們預想般自動去尋找$parent.myPrimitive,而是直接在ChildScope1下建立了myPrimitive,並且assign新值:22,因此目前內部結構變更如下: 

以上的問題全部都不是AngularJS才有的,屬於AngularJS的只有使用ng-include,建立了新scope;而不如我們預想般尋找$parent.myPrimitive並且變更而是產生新的myPrimitive則是JavascriptPrototypal Inheritance的概念(請參考連結文章,有很詳盡的說明)。

那麼如何去變更$parent.myPrimitive呢?有以下兩種方式:
  1. 一定使用關鍵字$parent
  2. 使用function()
以下我們繼續變更code測試上面兩個方式,首先測試第一種方式:使用$parent

變更tpl1.html

myPrimitive:
<input ng-model=“$parent.myPrimitive">

<br>

測試,變更輸入框為2266

執行成功!接下來測試第二種方式:使用function()
變更tp1.html


myPrimitive:
<input ng-model="myPrimitive">
<button ng-click="setMyPrimitive(myPrimitive)">$scope.setMyPrimitive()</button>

<br>

變更script.js


var app = angular.module('myApp',[]);
app.controller('scopeCtrl' ,['$scope', function($scope){
  $scope.myPrimitive = 50;
  $scope.myObject    = {aNumber: 11};
  
  $scope.setMyPrimitive = function(value) {
      $scope.myPrimitive = value;

  }

注意在這邊tpl1.html中的<input ng-model=“myPrimitive”>不是使用$parent.myPrimitive
開始測試!首先變更為2266



因為沒有使用方法1$parent.myPrimitive 因此myPrimitive如上所說一樣維持50 
這時我們按下<button>,呼叫$scope.setMyPrimitive(),確實變更成我們要的22了! 


2014年10月21日 星期二

JAVA - 基本properties的運用


說起來真是慚愧,用java這麼久,一直都沒有真正使用過properties file,今天就來練習一下:(其實是因為專案需要多國語系的關係啦,不得不用...)


假設我們設計一個網站 or app 需要支援多國語系,但不可能有人願意把每個跟語系有關的文字harcode在程式裡,畢竟那太愚蠢了,變成一個專案多個版本,卻只是為了那些不同的顯示文字,使用properties可以很好地解決這個問題:

今天我需要依照不同的語系,輸出不同的招呼語(中文=你好,英文=hello,日文=こんにちは),我該怎麼做呢:

首先是專案架構:
很簡單src只有一個檔案,而resource下的3個properties就是今天的主角囉,properties.sh只是方便執行測試的sh檔案,先看唯一的code Main.java:

package com.example.shihanne.properties;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

public class Main {

 public static void main(String[] args) throws IOException {
  Locale locale = Locale.getDefault();
  String key    = "hello";
  if(args.length == 1)
   key = args[0];
  if(args.length == 2){
   key = args[0];
   locale = Locale.forLanguageTag(args[1]);
  }
  System.out.println(new Main().getMessageByBundle(key,locale));
 }
    
    public String getMessageByBundle(String key,Locale local) throws UnsupportedEncodingException{
     ResourceBundle rs = ResourceBundle.getBundle("config",local); 
     return new String(rs.getString(key).getBytes("ISO-8859-1"),"UTF-8");
    }
}


以下是我的properties file:

config_en.properties:
hello=hello

config_zh_TW.properties:
hello=你好

config_ja.properties:
hello=こんにちは

很簡單,都各只有一行,=左邊的hello代表這個propertie的參數名稱(key),=右邊的也就是我們要輸出的各國語系value啦~

必須要注意的是properties的命名規則:[filename]_[locale].properties
在這裡我們使用的檔案名稱為config,繁體中文的locale=zh_TW,日文locale=ja,英文=en

程式很簡單,Main先偵測是否有指定參數:
第一個參數為欲讀取的properties key(此例為hello),第二個參數為語系(若沒有,則使用本機的預設語系(當然是zh_TW囉) )
之後我們再使用ResourceBundle.getBundle(basename,locale) 讀取properties file,最後再使用ResourceBundle.getString(key)讀出我們需要的value

properties.sh如下:

java -cp ../bin:../resource com.example.shihanne.properties.Main $1 $2

結果畫面:

2014年10月20日 星期一

Android GCM 某些裝置無法成功接收訊息

參考:
http://developer.android.com/google/gcm/client.html#manifest
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/


將近一年沒有再碰過android,這兩天為了火燒屁股的專案不得已再撿回來碰,
這次測試的是使用Android GCM機制來發送Notification,但奇怪的是五台測試裝置,分別為Android:4.1.1,4.1.2,4.2.2,4.0.4,與2.3.3

但是,怎麼發送訊息,4.0.4與2.3.3的就是無法成功接收訊息,試驗了老半天,最後在上述參考網站2發現問題點,原來是AndroidManifest.xml設定的問題,我原先設定如下:



    
    
    
    
    

    
    

    
        
            
                
                
            
        
        
    



原因就出在其中的




因為我是直接偷懶貼google教學(如上參考網址1),結果沒有把其中的com.example.gcm替換為我的pkg:com.xxx
因此,只要將這兩行的pkg也替換掉為我的pkg,就可以順利運作了。

不過這種錯誤實在是太難找了..因為4.1.1,4.1.2,4.2.2的都運作正常啊...難道是因為版本比較高所以自動錯誤修正嗎....?