All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.common.jvm_version.internal.InternalJvmVersion Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

The newest version!
/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.speedment.common.jvm_version.internal;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author Per Minborg
 */
public final class InternalJvmVersion {

    private final String specificationTitle;
    private final String specificationVersion;
    private final String specificationVendor;
    //
    private final String implementationTitle;
    private final String implementationVersion;
    private final String implementationVendor;

    private final int major;
    private final int minor;
    private final int security;

    public InternalJvmVersion() {

        specificationTitle = System.getProperty("java.vm.specification.name");
        specificationVersion = System.getProperty("java.vm.specification.version");
        specificationVendor = System.getProperty("java.vm.specification.vendor");
//        specificationTitle = package_.getSpecificationTitle();
        //specificationVersion = package_.getSpecificationVersion();
        //specificationVendor = package_.getSpecificationVendor();

        implementationTitle = System.getProperty("java.specification.name");
        major = version("major", 1);
        minor = version("minor", 2);
        security = version("security", 3);
        implementationVersion = String.format("%d.%d.%d", major, minor, security);
        implementationVendor = System.getProperty("java.specification.vendor");
        //implementationVendor = package_.getImplementationVendor();
    }

    public String getSpecificationTitle() {
        return specificationTitle;
    }

    public String getSpecificationVersion() {
        return specificationVersion;
    }

    public String getSpecificationVendor() {
        return specificationVendor;
    }

    public String getImplementationTitle() {
        return implementationTitle;
    }

    public String getImplementationVersion() {
        return implementationVersion;
    }

    public String getImplementationVendor() {
        return implementationVendor;
    }

    /**
     * Returns the major version number.
     *
     * @return The major version number
     */
    public int major() {
        return major;
    }

    /**
     * Returns the minor version number or zero if it was
     * not set.
     *
     * @return The minor version number or zero if it was not set
     */
    public int minor() {
        return minor;
    }

    /**
     * Returns the security version number or zero if it
     * was not set.
     *
     * @return The security version number or zero if it was not set
     */
    public int security() {
        return security;
    }

    private int version(String java9Name, int java8Index) {
        try {
            // Try Java 9 first
            final Method method = Runtime.class.getDeclaredMethod("version");
            if (method != null) {
                final Object version = method.invoke(Runtime.getRuntime());
                final Class clazz = Class.forName("java.lang.Runtime$Version");
                return ((Integer) clazz.getDeclaredMethod(java9Name).invoke(version)).intValue();

            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
            // we are pre Java 9
        }
        try {
            return Integer.parseInt(System.getProperty("java.runtime.version").split("([\\._-])")[java8Index]);
        } catch (Throwable t) {
            return 0;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy