Coverage Report - jp.co.y_net.amm.common.ResourceReader
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceReader
0%
0/55
0%
0/24
0
ResourceReader$1
0%
0/3
N/A
0
ResourceReader$QuickeProperty
0%
0/1
N/A
0
 
 1  0
 package jp.co.y_net.amm.common;
 2  
 
 3  
 import java.io.FileNotFoundException;
 4  
 import java.io.IOException;
 5  
 import java.io.InputStream;
 6  
 import java.io.InputStreamReader;
 7  
 import java.nio.charset.Charset;
 8  
 import java.util.ArrayList;
 9  
 import java.util.Enumeration;
 10  
 import java.util.List;
 11  
 import java.util.Locale;
 12  
 import java.util.ResourceBundle;
 13  
 
 14  0
 public class ResourceReader {
 15  
     
 16  
     private static final String DEFAULT_PROP = "App";
 17  
 
 18  
     public static Integer getIntegerQuick(String key) {
 19  0
         String str = getStringQuick(key);
 20  
         try {
 21  0
             return Integer.parseInt(str);
 22  0
         } catch (NumberFormatException e) {
 23  0
             return -1;
 24  
         }
 25  
     }
 26  
     public static boolean getBooleanQuick(String key) {
 27  0
         String str = getStringQuick(key).toLowerCase();
 28  0
         return str.equals("yes")
 29  0
                 || str.equals("y")
 30  0
                 || str.equals("true")
 31  0
                 || str.equals("t")
 32  0
                 || str.equals("1");
 33  
     }
 34  
     
 35  
     /**
 36  
      * @see #getString(String, String)
 37  
      * @param def 値が取得できなかった場合のデフォルト値
 38  
      */
 39  
     public static final String getStringQuick(String key, String def) {
 40  0
         String value = getStringQuick(key);
 41  0
         if(value == null) {
 42  0
             return def;
 43  
         } else {
 44  0
             return value;
 45  
         }
 46  
     }
 47  
     /**
 48  
      * 値の変更でサーバーの再起動を必要としない、プロパティファイルの読み取り機能
 49  
      * ・読み取り対象は jp.co.y_net.amm.WicketApplication.properties.properties とする。
 50  
      * ・キャッシュの持続時間は 60 秒とする。
 51  
      * @param key 
 52  
      * @return
 53  
      */
 54  
     public static final String getStringQuick(String key) {
 55  0
         String value = null;
 56  
         try {
 57  0
             ResourceBundle bundle = ResourceBundle.getBundle(DEFAULT_PROP, RESOURCE_BUNDLE_CONTROL);
 58  0
             value = bundle.getString(key);
 59  
             
 60  0
         } catch (Exception e) {
 61  0
             AppLogger.info(DEFAULT_PROP + ".properties から[" + key + "]が取得できませんでした。");
 62  
         }
 63  0
         return value;
 64  
     }
 65  0
     private static final ResourceBundle.Control RESOURCE_BUNDLE_CONTROL = new ResourceBundle.Control() {
 66  
         public long getTimeToLive(String baseName, Locale locale) {
 67  0
             return 60000L;
 68  
         }
 69  0
     };
 70  
     
 71  
     
 72  
     /**
 73  
      * プロパティファイルの全行をリストで取得
 74  
      * @param resourceName 
 75  
      * @return
 76  
      */
 77  
     public static List<String> getLines(String resourceName) {
 78  
         Enumeration<String> keys;
 79  
         try {
 80  0
             ResourceBundle bundle = ResourceBundle.getBundle(resourceName, RESOURCE_BUNDLE_CONTROL);
 81  0
             keys = bundle.getKeys();
 82  
             
 83  0
         } catch (Exception e) {
 84  0
             AppLogger.info(DEFAULT_PROP + ".properties が参照できませんでした。");
 85  0
             return new ArrayList<String>();
 86  
         }
 87  0
         List<String> lines = new ArrayList<String>();
 88  0
         while(keys.hasMoreElements()) {
 89  0
             lines.add(keys.nextElement());
 90  
         }
 91  0
         return lines;
 92  
     }
 93  
     
 94  
 //    /**
 95  
 //     * getString はアプリケーションが稼働しているスレッドからでないと使用できない。
 96  
 //     * 単体テストやアプリケーションの配下でないところからプロパティファイルを参照する際に使用する。
 97  
 //     * @param key 
 98  
 //     * @return 
 99  
 //     */
 100  
 //    public static final String getStringWithoutApp(String key) {
 101  
 //        String value = null;
 102  
 //        try {
 103  
 //            ResourceBundle bundle = ResourceBundle.getBundle(DEFAULT_PROP);
 104  
 //            value = bundle.getString(key);
 105  
 //        } catch (Exception e) {
 106  
 //            AppLogger.info(DEFAULT_PROP + "から[" + key + "]が取得できませんでした。");
 107  
 //        }
 108  
 //        return value;
 109  
 //    }
 110  
 
 111  
     
 112  
     /**
 113  
      * クラスパス上のリソースからテキストファイルとして読み込む
 114  
      * @param name 相対パス指定は不可
 115  
      * @return
 116  
      */
 117  
     public static String getText(String name) {
 118  0
         StringBuilder sb = new StringBuilder();
 119  0
         InputStream is = null;
 120  0
         InputStreamReader isr = null;
 121  
         try {
 122  
             /* mainメソッドからの呼び出しを想定 */
 123  0
             is =  ClassLoader.getSystemResourceAsStream(name);
 124  0
             if(is==null) {
 125  
                 /* webアプリケーションからの呼び出しを想定 */
 126  0
                 is =  ResourceReader.class.getResourceAsStream("/" + name);
 127  
             }
 128  0
             if(is==null) {
 129  0
                 AppLogger.info(name + "が取得できませんでした。");
 130  
             }
 131  0
             isr = new InputStreamReader(is, Charset.forName("UTF-8"));
 132  
             int contents;
 133  0
             while ((contents = isr.read()) != -1) {
 134  0
                 sb.append((char)contents);
 135  
 //                text += (char)contents;
 136  
             }
 137  0
         } catch (FileNotFoundException e) {
 138  0
             throw new RuntimeException(e);
 139  0
         } catch (IOException e) {
 140  0
             throw new RuntimeException(e);
 141  0
         } finally {
 142  
             try {
 143  0
                 if (isr != null) isr.close();
 144  0
                 if (is != null) is.close();
 145  0
             } catch (IOException e) {
 146  0
                 throw new RuntimeException(e);
 147  
             }
 148  0
         }
 149  0
         String text = sb.toString();
 150  0
         return text;
 151  
     }
 152  
 
 153  
 
 154  
     /**
 155  
      * @deprecated プロパティファイルの参照は ResourceReader へ移行しました。
 156  
      */
 157  0
     public static class QuickeProperty extends ResourceReader {
 158  
         
 159  
     }
 160  
     
 161  
 }