1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.commons.text.lookup;
18
19 import java.util.Locale;
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.apache.commons.text.StringSubstitutor;
23
24 /**
25 * Looks up keys related to Java: Java version, JRE version, VM version, and so on.
26 * <p>
27 * The lookup keys with examples are:
28 * </p>
29 * <ul>
30 * <li><strong>version</strong>: "Java version 1.8.0_181"</li>
31 * <li><strong>runtime</strong>: "Java(TM) SE Runtime Environment (build 1.8.0_181-b13) from Oracle Corporation"</li>
32 * <li><strong>vm</strong>: "Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)"</li>
33 * <li><strong>os</strong>: "Windows 10 10.0, architecture: amd64-64"</li>
34 * <li><strong>hardware</strong>: "processors: 4, architecture: amd64-64, instruction sets: amd64"</li>
35 * <li><strong>locale</strong>: "default locale: en_US, platform encoding: iso-8859-1"</li>
36 * </ul>
37 *
38 * <p>
39 * Using a {@link StringLookup} from the {@link StringLookupFactory}:
40 * </p>
41 *
42 * <pre>
43 * StringLookupFactory.INSTANCE.javaPlatformStringLookup().lookup("version");
44 * </pre>
45 * <p>
46 * Using a {@link StringSubstitutor}:
47 * </p>
48 *
49 * <pre>
50 * StringSubstitutor.createInterpolator().replace("... ${java:version} ..."));
51 * </pre>
52 * <p>
53 * The above examples convert {@code "version"} to the current VM version, for example,
54 * {@code "Java version 1.8.0_181"}.
55 * </p>
56 * <p>
57 * Public access is through {@link StringLookupFactory}.
58 * </p>
59 *
60 * @see StringLookupFactory
61 * @since 1.3
62 */
63 final class JavaPlatformStringLookup extends AbstractStringLookup {
64
65 /**
66 * Defines the singleton for this class.
67 */
68 static final JavaPlatformStringLookup INSTANCE = new JavaPlatformStringLookup();
69 /** {@code hardware} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
70 private static final String KEY_HARDWARE = "hardware";
71 /** {@code locale} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
72 private static final String KEY_LOCALE = "locale";
73 /** {@code os} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
74 private static final String KEY_OS = "os";
75 /** {@code runtime} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
76 private static final String KEY_RUNTIME = "runtime";
77 /** {@code version} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
78 private static final String KEY_VERSION = "version";
79
80 /** {@code vm} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
81 private static final String KEY_VM = "vm";
82
83 /**
84 * The main method for running the JavaPlatformStringLookup.
85 *
86 * @param args the standard Java main method parameter which is unused for our running of this class.
87 */
88 public static void main(final String[] args) {
89 System.out.println(JavaPlatformStringLookup.class);
90 System.out.printf("%s = %s%n", KEY_VERSION, INSTANCE.lookup(KEY_VERSION));
91 System.out.printf("%s = %s%n", KEY_RUNTIME, INSTANCE.lookup(KEY_RUNTIME));
92 System.out.printf("%s = %s%n", KEY_VM, INSTANCE.lookup(KEY_VM));
93 System.out.printf("%s = %s%n", KEY_OS, INSTANCE.lookup(KEY_OS));
94 System.out.printf("%s = %s%n", KEY_HARDWARE, INSTANCE.lookup(KEY_HARDWARE));
95 System.out.printf("%s = %s%n", KEY_LOCALE, INSTANCE.lookup(KEY_LOCALE));
96 }
97
98 /**
99 * No need to build instances for now.
100 */
101 private JavaPlatformStringLookup() {
102 // empty
103 }
104
105 /**
106 * Accessible through the Lookup key {@code hardware}.
107 *
108 * @return hardware processor information.
109 */
110 String getHardware() {
111 return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: "
112 + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model")
113 + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist");
114 }
115
116 /**
117 * Accessible through the Lookup key {@code locale}.
118 *
119 * @return system locale and file encoding information.
120 */
121 String getLocale() {
122 return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
123 }
124
125 /**
126 * Accessible through the Lookup key {@code os}.
127 *
128 * @return operating system information.
129 */
130 String getOperatingSystem() {
131 return getSystemProperty("os.name") + " " + getSystemProperty("os.version")
132 + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch")
133 + getSystemProperty("-", "sun.arch.data.model");
134 }
135
136 /**
137 * Accessible through the Lookup key {@code runtime}.
138 *
139 * @return Java Runtime Environment information.
140 */
141 String getRuntime() {
142 return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version")
143 + ") from " + getSystemProperty("java.vendor");
144 }
145
146 /**
147 * Gets the given system property.
148 *
149 * @param name a system property name.
150 * @return a system property value.
151 */
152 private String getSystemProperty(final String name) {
153 return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.apply(name);
154 }
155
156 /**
157 * Gets the given system property.
158 *
159 * @param prefix the prefix to use for the result string.
160 * @param name a system property name.
161 * @return The prefix + a system property value.
162 */
163 private String getSystemProperty(final String prefix, final String name) {
164 final String value = getSystemProperty(name);
165 if (StringUtils.isEmpty(value)) {
166 return StringUtils.EMPTY;
167 }
168 return prefix + value;
169 }
170
171 /**
172 * Accessible through the Lookup key {@code vm}.
173 *
174 * @return Java Virtual Machine information.
175 */
176 String getVirtualMachine() {
177 return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", "
178 + getSystemProperty("java.vm.info") + ")";
179 }
180
181 /**
182 * Looks up the value of the Java platform key.
183 * <p>
184 * The lookup keys with examples are:
185 * </p>
186 * <ul>
187 * <li><strong>version</strong>: "Java version 1.8.0_181"</li>
188 * <li><strong>runtime</strong>: "Java(TM) SE Runtime Environment (build 1.8.0_181-b13) from Oracle Corporation"</li>
189 * <li><strong>vm</strong>: "Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)"</li>
190 * <li><strong>os</strong>: "Windows 10 10.0, architecture: amd64-64"</li>
191 * <li><strong>hardware</strong>: "processors: 4, architecture: amd64-64, instruction sets: amd64"</li>
192 * <li><strong>locale</strong>: "default locale: en_US, platform encoding: iso-8859-1"</li>
193 * </ul>
194 *
195 * @param key the key to be looked up, may be null.
196 * @return The value of the environment variable.
197 */
198 @Override
199 public String lookup(final String key) {
200 if (key == null) {
201 return null;
202 }
203 switch (key) {
204 case KEY_VERSION:
205 return "Java version " + getSystemProperty("java.version");
206 case KEY_RUNTIME:
207 return getRuntime();
208 case KEY_VM:
209 return getVirtualMachine();
210 case KEY_OS:
211 return getOperatingSystem();
212 case KEY_HARDWARE:
213 return getHardware();
214 case KEY_LOCALE:
215 return getLocale();
216 default:
217 throw new IllegalArgumentException(key);
218 }
219 }
220 }