001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.lookup;
018
019import java.util.Locale;
020
021import org.apache.logging.log4j.core.LogEvent;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.util.Strings;
024
025/**
026 * Looks up keys related to Java: Java version, JRE version, VM version, and so on.
027 */
028@Plugin(name = "java", category = StrLookup.CATEGORY)
029public class JavaLookup extends AbstractLookup {
030
031    private final SystemPropertiesLookup spLookup = new SystemPropertiesLookup();
032
033    /**
034     * Accessible through the Lookup key {@code hw}.
035     * @return hardware processor information.
036     */
037    public String getHardware() {
038        return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: "
039                + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model")
040                + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist");
041    }
042
043    /**
044     * Accessible through the Lookup key {@code locale}.
045     * @return system locale and file encoding information.
046     */
047    public String getLocale() {
048        return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
049    }
050
051    /**
052     * Accessible through the Lookup key {@code os}.
053     * @return operating system information.
054     */
055    public String getOperatingSystem() {
056        return getSystemProperty("os.name") + " " + getSystemProperty("os.version")
057                + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch")
058                + getSystemProperty("-", "sun.arch.data.model");
059    }
060
061    /**
062     * Accessible through the Lookup key {@code runtime}.
063     * @return Java Runtime Environment information.
064     */
065    public String getRuntime() {
066        return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version")
067                + ") from " + getSystemProperty("java.vendor");
068    }
069
070    private String getSystemProperty(final String name) {
071        return spLookup.lookup(name);
072    }
073
074    private String getSystemProperty(final String prefix, final String name) {
075        final String value = getSystemProperty(name);
076        if (Strings.isEmpty(value)) {
077            return Strings.EMPTY;
078        }
079        return prefix + value;
080    }
081
082    /**
083     * Accessible through the Lookup key {@code vm}.
084     * @return Java Virtual Machine information.
085     */
086    public String getVirtualMachine() {
087        return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", "
088                + getSystemProperty("java.vm.info") + ")";
089    }
090
091    /**
092     * Looks up the value of the environment variable.
093     *
094     * @param event
095     *        The current LogEvent (is ignored by this StrLookup).
096     * @param key
097     *        the key to be looked up, may be null
098     * @return The value of the environment variable.
099     */
100    @Override
101    public String lookup(final LogEvent event, final String key) {
102        switch (key) {
103        case "version":
104            return "Java version " + getSystemProperty("java.version");
105        case "runtime":
106            return getRuntime();
107        case "vm":
108            return getVirtualMachine();
109        case "os":
110            return getOperatingSystem();
111        case "hw":
112            return getHardware();
113        case "locale":
114            return getLocale();
115        default:
116            throw new IllegalArgumentException(key);
117        }
118    }
119}