View Javadoc

1   /*
2    * Copyright 2007-2008 Naoki NOSE.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package jp.liq.container.util;
17  
18  import java.text.MessageFormat;
19  import java.util.Map;
20  import java.util.ResourceBundle;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  /**
24   * 例外のベースクラスです。
25   * エラーコードに対応するエラーメッセージをリソースバンドルから取得します。
26   * @author nose
27   */
28  public abstract class AbstractException extends RuntimeException {
29      private static final long serialVersionUID = 5718032353881037202L;
30      protected static final String LS = System.getProperty("line.separator");
31  
32      private static final Map<String, String> messageCache = new ConcurrentHashMap<String, String>();
33  
34      private final Enum<?> errorCode;
35      private final Object[] args;
36      
37      /**
38       * この例外のインスタンスを構築します。
39       * @param errCode エラーコード
40       * @param args エラーメッセージに埋め込むパラメータ
41       */
42      public AbstractException(Enum<?> errCode, Object... args) {
43          super();
44          this.errorCode = errCode;
45          this.args = args;
46      }
47  
48      /**
49       * この例外のインスタンスを構築します。
50       * @param cause 起因例外
51       * @param errCode エラーコード
52       * @param args エラーメッセージに埋め込むパラメータ
53       */
54      public AbstractException(Throwable cause, Enum<?> errCode, Object... args) {
55          super(cause);
56          this.errorCode = errCode;
57          this.args = args;
58      }
59  
60      /**
61       * この例外のインスタンスを構築します
62       * @param cause 起因例外
63       */
64      public AbstractException(Throwable cause) {
65          super(cause);
66          this.errorCode = null;
67          this.args = null;
68      }
69  
70      @Override
71      public String getLocalizedMessage() {
72          if(errorCode == null) {
73              return null;
74          }
75          ResourceBundle bundle = ResourceBundle.getBundle(this.getClass().getName());
76          
77          if(!messageCache.containsKey(errorCode.name())) {
78              messageCache.put(errorCode.name(), replaceLineSeparator(bundle.getString(errorCode.name())));
79          }
80          String msg = messageCache.get(errorCode.name());
81          return MessageFormat.format(msg, args);
82      }
83  
84      private static String replaceLineSeparator(String srcStr) {
85          StringBuffer sb = new StringBuffer(srcStr);
86  
87          while(true) {
88              int pos = 0;
89              pos = sb.indexOf("{LS}", pos);
90              if(pos == -1) {
91                  break;
92              } else {
93                  sb.replace(pos, pos + 4, LS);
94                  pos += LS.length();
95              }
96          }
97          return sb.toString();
98      }
99  
100     /**
101      * 例外のエラーコードを取得します。
102      */
103     public Enum<?> getErrorCode() {
104         return errorCode;
105     }
106 }