View Javadoc
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  
70      /** {@code hardware} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
71      private static final String KEY_HARDWARE = "hardware";
72  
73      /** {@code locale} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
74      private static final String KEY_LOCALE = "locale";
75  
76      /** {@code os} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
77      private static final String KEY_OS = "os";
78  
79      /** {@code runtime} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
80      private static final String KEY_RUNTIME = "runtime";
81  
82      /** {@code version} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
83      private static final String KEY_VERSION = "version";
84  
85      /** {@code vm} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
86      private static final String KEY_VM = "vm";
87  
88      /**
89       * The main method for running the JavaPlatformStringLookup.
90       *
91       * @param args the standard Java main method parameter which is unused for our running of this class.
92       */
93      public static void main(final String[] args) {
94          System.out.println(JavaPlatformStringLookup.class);
95          System.out.printf("%s = %s%n", KEY_VERSION, INSTANCE.lookup(KEY_VERSION));
96          System.out.printf("%s = %s%n", KEY_RUNTIME, INSTANCE.lookup(KEY_RUNTIME));
97          System.out.printf("%s = %s%n", KEY_VM, INSTANCE.lookup(KEY_VM));
98          System.out.printf("%s = %s%n", KEY_OS, INSTANCE.lookup(KEY_OS));
99          System.out.printf("%s = %s%n", KEY_HARDWARE, INSTANCE.lookup(KEY_HARDWARE));
100         System.out.printf("%s = %s%n", KEY_LOCALE, INSTANCE.lookup(KEY_LOCALE));
101     }
102 
103     /**
104      * No need to build instances for now.
105      */
106     private JavaPlatformStringLookup() {
107         // empty
108     }
109 
110     /**
111      * Accessible through the Lookup key {@code hardware}.
112      *
113      * @return hardware processor information.
114      */
115     String getHardware() {
116         return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: "
117             + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model")
118             + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist");
119     }
120 
121     /**
122      * Accessible through the Lookup key {@code locale}.
123      *
124      * @return system locale and file encoding information.
125      */
126     String getLocale() {
127         return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
128     }
129 
130     /**
131      * Accessible through the Lookup key {@code os}.
132      *
133      * @return operating system information.
134      */
135     String getOperatingSystem() {
136         return getSystemProperty("os.name") + " " + getSystemProperty("os.version")
137             + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch")
138             + getSystemProperty("-", "sun.arch.data.model");
139     }
140 
141     /**
142      * Accessible through the Lookup key {@code runtime}.
143      *
144      * @return Java Runtime Environment information.
145      */
146     String getRuntime() {
147         return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version")
148             + ") from " + getSystemProperty("java.vendor");
149     }
150 
151     /**
152      * Gets the given system property.
153      *
154      * @param name a system property name.
155      * @return a system property value.
156      */
157     private String getSystemProperty(final String name) {
158         return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.apply(name);
159     }
160 
161     /**
162      * Gets the given system property.
163      *
164      * @param prefix the prefix to use for the result string.
165      * @param name a system property name.
166      * @return The prefix + a system property value.
167      */
168     private String getSystemProperty(final String prefix, final String name) {
169         final String value = getSystemProperty(name);
170         if (StringUtils.isEmpty(value)) {
171             return StringUtils.EMPTY;
172         }
173         return prefix + value;
174     }
175 
176     /**
177      * Accessible through the Lookup key {@code vm}.
178      *
179      * @return Java Virtual Machine information.
180      */
181     String getVirtualMachine() {
182         return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", "
183             + getSystemProperty("java.vm.info") + ")";
184     }
185 
186     /**
187      * Looks up the value of the Java platform key.
188      * <p>
189      * The lookup keys with examples are:
190      * </p>
191      * <ul>
192      * <li><strong>version</strong>: "Java version 1.8.0_181"</li>
193      * <li><strong>runtime</strong>: "Java(TM) SE Runtime Environment (build 1.8.0_181-b13) from Oracle Corporation"</li>
194      * <li><strong>vm</strong>: "Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)"</li>
195      * <li><strong>os</strong>: "Windows 10 10.0, architecture: amd64-64"</li>
196      * <li><strong>hardware</strong>: "processors: 4, architecture: amd64-64, instruction sets: amd64"</li>
197      * <li><strong>locale</strong>: "default locale: en_US, platform encoding: iso-8859-1"</li>
198      * </ul>
199      *
200      * @param key the key to be looked up, may be null.
201      * @return The value of the environment variable.
202      */
203     @Override
204     public String lookup(final String key) {
205         if (key == null) {
206             return null;
207         }
208         switch (key) {
209         case KEY_VERSION:
210             return "Java version " + getSystemProperty("java.version");
211         case KEY_RUNTIME:
212             return getRuntime();
213         case KEY_VM:
214             return getVirtualMachine();
215         case KEY_OS:
216             return getOperatingSystem();
217         case KEY_HARDWARE:
218             return getHardware();
219         case KEY_LOCALE:
220             return getLocale();
221         default:
222             throw new IllegalArgumentException(key);
223         }
224     }
225 }