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