OSDN Git Service

4d3502532fa6fd16d5113874007aa8cea2618802
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / config / EnvInfo.java
1 /*
2  * environment information
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.config;
9
10 import java.io.File;
11 import java.text.NumberFormat;
12 import java.util.Set;
13 import java.util.SortedMap;
14 import java.util.TreeMap;
15
16 /**
17  * 実行環境に関する各種情報。
18  */
19 public final class EnvInfo{
20
21     /** OS名。 */
22     public static final String OS_NAME;
23     /** OSバージョン。 */
24     public static final String OS_VERSION;
25     /** アーキテクチャ種別。 */
26     public static final String OS_ARCH;
27     /** Java実行系ベンダ。 */
28     public static final String JAVA_VENDOR;
29     /** Java実行形バージョン。 */
30     public static final String JAVA_VERSION;
31
32     /** 最大ヒープメモリ。 */
33     public static final long MAX_MEMORY;
34
35     private static final SortedMap<String, String> PROPERTY_MAP =
36             new TreeMap<>();
37
38     private static final SortedMap<String, String> ENVIRONMENT_MAP =
39             new TreeMap<>();
40
41     private static final String[] CLASSPATHS;
42
43     static{
44         OS_NAME      = getSecureProperty("os.name");
45         OS_VERSION   = getSecureProperty("os.version");
46         OS_ARCH      = getSecureProperty("os.arch");
47         JAVA_VENDOR  = getSecureProperty("java.vendor");
48         JAVA_VERSION = getSecureProperty("java.version");
49
50         getSecureEnvironment("LANG");
51         getSecureEnvironment("DISPLAY");
52
53         Runtime runtime = Runtime.getRuntime();
54         MAX_MEMORY = runtime.maxMemory();
55
56         String classpath   = getSecureProperty("java.class.path");
57         String[] pathVec;
58         if(classpath != null){
59             pathVec = classpath.split(File.pathSeparator);
60         }else{
61             pathVec = new String[0];
62         }
63         CLASSPATHS = pathVec;
64
65     }
66
67
68     /**
69      * 隠れコンストラクタ。
70      */
71     private EnvInfo(){
72         throw new AssertionError();
73     }
74
75
76     /**
77      * 可能ならシステムプロパティを読み込む。
78      * @param key キー
79      * @return プロパティ値。セキュリティ上読み込み禁止の場合はnull。
80      */
81     private static String getSecureProperty(String key){
82         String result;
83         try{
84             result = System.getProperty(key);
85             if(result != null) PROPERTY_MAP.put(key, result);
86         }catch(SecurityException e){
87             result = null;
88         }
89         return result;
90     }
91
92     /**
93      * 可能なら環境変数を読み込む。
94      * @param name 環境変数名
95      * @return 環境変数値。セキュリティ上読み込み禁止の場合はnull。
96      */
97     private static String getSecureEnvironment(String name){
98         String result;
99         try{
100             result = System.getenv(name);
101             if(result != null) ENVIRONMENT_MAP.put(name, result);
102         }catch(SecurityException e){
103             result = null;
104         }
105         return result;
106     }
107
108     /**
109      * VM詳細情報を文字列化する。
110      * @return VM詳細情報
111      */
112     public static String getVMInfo(){
113         StringBuilder result = new StringBuilder();
114         NumberFormat nform = NumberFormat.getNumberInstance();
115
116         result.append("最大ヒープメモリ量: ")
117               .append(nform.format(MAX_MEMORY))
118               .append(" bytes\n");
119
120         result.append("\n");
121
122         result.append("主要システムプロパティ:\n");
123         Set<String> propKeys = PROPERTY_MAP.keySet();
124         for(String propKey : propKeys){
125             if(propKey.equals("java.class.path")) continue;
126             String value = PROPERTY_MAP.get(propKey);
127             result.append("  ");
128             result.append(propKey).append("=").append(value).append("\n");
129         }
130
131         result.append("\n");
132
133         result.append("主要環境変数:\n");
134         Set<String> envKeys = ENVIRONMENT_MAP.keySet();
135         for(String envKey : envKeys){
136             String value = ENVIRONMENT_MAP.get(envKey);
137             result.append("  ");
138             result.append(envKey).append("=").append(value).append("\n");
139         }
140
141         result.append("\n");
142
143         result.append("クラスパス:\n");
144         for(String path : CLASSPATHS){
145             result.append("  ");
146             result.append(path).append("\n");
147         }
148
149         result.append("\n");
150
151         return result.toString();
152     }
153
154 }