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

com.pamirs.pradar.common.ServerDetector Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.pamirs.pradar.common;


/**
 * fork from
 *
 * @author shiyajian
 * @since : 2020-07-21
 */
public final class ServerDetector {

    private ServerDetector() { /* no instance */ }

    public enum ServerType {
        GLASSFISH("GlassFish"),
        JBOSS("JBoss"),
        JETTY("Jetty"),
        JONAS("JOnAS"),
        OC4J("OC4J"),
        RESIN("Resin"),
        TOMCAT("Tomcat"),
        UNKNOWN("Unknown"),
        WEBLOGIC("WebLogic"),
        WEBSPHERE("WebSphere"),
        WILDFLY("WildFly");

        private String desc;

        ServerType(String desc) {
            this.desc = desc;
        }

        public String getDesc() {
            return desc;
        }
    }

    private static boolean _hasSystemProperty(String key) {
        return System.getProperty(key) != null;
    }

    private static boolean _detect(String className) {
        try {
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            systemClassLoader.loadClass(className);
            return true;
        } catch (ClassNotFoundException classNotFoundException) {
            return ServerDetector.class.getResource(className) != null;
        }
    }

    public static ServerType getServerType() {

        if (_hasSystemProperty("com.sun.aas.instanceRoot")) {
            return ServerType.GLASSFISH;
        }

        if (_hasSystemProperty("jboss.home.dir")) {
            return ServerType.JBOSS;
        }

        if (_hasSystemProperty("jonas.base")) {
            return ServerType.JONAS;
        }

        if (_detect("oracle.oc4j.util.ClassUtils")) {
            return ServerType.OC4J;
        }

        if (_hasSystemProperty("resin.home")) {
            return ServerType.RESIN;
        }

        if (_detect("/weblogic/Server.class")) {
            return ServerType.WEBLOGIC;
        }

        if (_detect("/com/ibm/websphere/product/VersionInfo.class")) {
            return ServerType.WEBSPHERE;
        }

        if (_hasSystemProperty("jboss.home.dir")) {
            return ServerType.WILDFLY;
        }

        if (_hasSystemProperty("jetty.home")) {
            return ServerType.JETTY;
        }

        if (_hasSystemProperty("catalina.base")) {
            return ServerType.TOMCAT;
        }

        return ServerType.UNKNOWN;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy