JavaPlatformStringLookup.java

  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. import java.util.Locale;

  19. import org.apache.commons.lang3.StringUtils;
  20. import org.apache.commons.text.StringSubstitutor;

  21. /**
  22.  * Looks up keys related to Java: Java version, JRE version, VM version, and so on.
  23.  * <p>
  24.  * The lookup keys with examples are:
  25.  * </p>
  26.  * <ul>
  27.  * <li><strong>version</strong>: "Java version 1.8.0_181"</li>
  28.  * <li><strong>runtime</strong>: "Java(TM) SE Runtime Environment (build 1.8.0_181-b13) from Oracle Corporation"</li>
  29.  * <li><strong>vm</strong>: "Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)"</li>
  30.  * <li><strong>os</strong>: "Windows 10 10.0, architecture: amd64-64"</li>
  31.  * <li><strong>hardware</strong>: "processors: 4, architecture: amd64-64, instruction sets: amd64"</li>
  32.  * <li><strong>locale</strong>: "default locale: en_US, platform encoding: iso-8859-1"</li>
  33.  * </ul>
  34.  *
  35.  * <p>
  36.  * Using a {@link StringLookup} from the {@link StringLookupFactory}:
  37.  * </p>
  38.  *
  39.  * <pre>
  40.  * StringLookupFactory.INSTANCE.javaPlatformStringLookup().lookup("version");
  41.  * </pre>
  42.  * <p>
  43.  * Using a {@link StringSubstitutor}:
  44.  * </p>
  45.  *
  46.  * <pre>
  47.  * StringSubstitutor.createInterpolator().replace("... ${java:version} ..."));
  48.  * </pre>
  49.  * <p>
  50.  * The above examples convert {@code "version"} to the current VM version, for example,
  51.  * {@code "Java version 1.8.0_181"}.
  52.  * </p>
  53.  *
  54.  * @since 1.3
  55.  */
  56. final class JavaPlatformStringLookup extends AbstractStringLookup {

  57.     /**
  58.      * Defines the singleton for this class.
  59.      */
  60.     static final JavaPlatformStringLookup INSTANCE = new JavaPlatformStringLookup();
  61.     /** {@code hardware} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
  62.     private static final String KEY_HARDWARE = "hardware";
  63.     /** {@code locale} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
  64.     private static final String KEY_LOCALE = "locale";
  65.     /** {@code os} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
  66.     private static final String KEY_OS = "os";
  67.     /** {@code runtime} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
  68.     private static final String KEY_RUNTIME = "runtime";
  69.     /** {@code version} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
  70.     private static final String KEY_VERSION = "version";

  71.     /** {@code vm} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */
  72.     private static final String KEY_VM = "vm";

  73.     /**
  74.      * The main method for running the JavaPlatformStringLookup.
  75.      *
  76.      * @param args the standard Java main method parameter which is unused for our running of this class.
  77.      */
  78.     public static void main(final String[] args) {
  79.         System.out.println(JavaPlatformStringLookup.class);
  80.         System.out.printf("%s = %s%n", KEY_VERSION, JavaPlatformStringLookup.INSTANCE.lookup(KEY_VERSION));
  81.         System.out.printf("%s = %s%n", KEY_RUNTIME, JavaPlatformStringLookup.INSTANCE.lookup(KEY_RUNTIME));
  82.         System.out.printf("%s = %s%n", KEY_VM, JavaPlatformStringLookup.INSTANCE.lookup(KEY_VM));
  83.         System.out.printf("%s = %s%n", KEY_OS, JavaPlatformStringLookup.INSTANCE.lookup(KEY_OS));
  84.         System.out.printf("%s = %s%n", KEY_HARDWARE, JavaPlatformStringLookup.INSTANCE.lookup(KEY_HARDWARE));
  85.         System.out.printf("%s = %s%n", KEY_LOCALE, JavaPlatformStringLookup.INSTANCE.lookup(KEY_LOCALE));
  86.     }

  87.     /**
  88.      * No need to build instances for now.
  89.      */
  90.     private JavaPlatformStringLookup() {
  91.         // empty
  92.     }

  93.     /**
  94.      * Accessible through the Lookup key {@code hardware}.
  95.      *
  96.      * @return hardware processor information.
  97.      */
  98.     String getHardware() {
  99.         return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: "
  100.             + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model")
  101.             + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist");
  102.     }

  103.     /**
  104.      * Accessible through the Lookup key {@code locale}.
  105.      *
  106.      * @return system locale and file encoding information.
  107.      */
  108.     String getLocale() {
  109.         return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
  110.     }

  111.     /**
  112.      * Accessible through the Lookup key {@code os}.
  113.      *
  114.      * @return operating system information.
  115.      */
  116.     String getOperatingSystem() {
  117.         return getSystemProperty("os.name") + " " + getSystemProperty("os.version")
  118.             + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch")
  119.             + getSystemProperty("-", "sun.arch.data.model");
  120.     }

  121.     /**
  122.      * Accessible through the Lookup key {@code runtime}.
  123.      *
  124.      * @return Java Runtime Environment information.
  125.      */
  126.     String getRuntime() {
  127.         return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version")
  128.             + ") from " + getSystemProperty("java.vendor");
  129.     }

  130.     /**
  131.      * Gets the given system property.
  132.      *
  133.      * @param name a system property name.
  134.      * @return a system property value.
  135.      */
  136.     private String getSystemProperty(final String name) {
  137.         return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.lookup(name);
  138.     }

  139.     /**
  140.      * Gets the given system property.
  141.      *
  142.      * @param prefix the prefix to use for the result string
  143.      * @param name a system property name.
  144.      * @return The prefix + a system property value.
  145.      */
  146.     private String getSystemProperty(final String prefix, final String name) {
  147.         final String value = getSystemProperty(name);
  148.         if (StringUtils.isEmpty(value)) {
  149.             return StringUtils.EMPTY;
  150.         }
  151.         return prefix + value;
  152.     }

  153.     /**
  154.      * Accessible through the Lookup key {@code vm}.
  155.      *
  156.      * @return Java Virtual Machine information.
  157.      */
  158.     String getVirtualMachine() {
  159.         return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", "
  160.             + getSystemProperty("java.vm.info") + ")";
  161.     }

  162.     /**
  163.      * Looks up the value of the Java platform key.
  164.      * <p>
  165.      * The lookup keys with examples are:
  166.      * </p>
  167.      * <ul>
  168.      * <li><strong>version</strong>: "Java version 1.8.0_181"</li>
  169.      * <li><strong>runtime</strong>: "Java(TM) SE Runtime Environment (build 1.8.0_181-b13) from Oracle Corporation"</li>
  170.      * <li><strong>vm</strong>: "Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)"</li>
  171.      * <li><strong>os</strong>: "Windows 10 10.0, architecture: amd64-64"</li>
  172.      * <li><strong>hardware</strong>: "processors: 4, architecture: amd64-64, instruction sets: amd64"</li>
  173.      * <li><strong>locale</strong>: "default locale: en_US, platform encoding: iso-8859-1"</li>
  174.      * </ul>
  175.      *
  176.      * @param key the key to be looked up, may be null
  177.      * @return The value of the environment variable.
  178.      */
  179.     @Override
  180.     public String lookup(final String key) {
  181.         if (key == null) {
  182.             return null;
  183.         }
  184.         switch (key) {
  185.         case KEY_VERSION:
  186.             return "Java version " + getSystemProperty("java.version");
  187.         case KEY_RUNTIME:
  188.             return getRuntime();
  189.         case KEY_VM:
  190.             return getVirtualMachine();
  191.         case KEY_OS:
  192.             return getOperatingSystem();
  193.         case KEY_HARDWARE:
  194.             return getHardware();
  195.         case KEY_LOCALE:
  196.             return getLocale();
  197.         default:
  198.             throw new IllegalArgumentException(key);
  199.         }
  200.     }
  201. }