java 获取服务器名_JAVA获取WEB服务器的名称

本文介绍了一个JAVA类ServerDetector,用于检测当前应用运行于何种应用服务器上,如Tomcat、WebLogic等。通过检查特定的类资源来判断服务器类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA获取服务器的名称,比如服务器是tomcat还是weblogic等等。在portal-kernel里面找到的。

1.[文件] ServerDetector.java ~ 5KB     下载(26)

package com.post.common.utils.other;

public class ServerDetector {

public static final String GERONIMO_ID = "geronimo";

public static final String GLASSFISH_ID = "glassfish";

public static final String JBOSS_ID = "jboss";

public static final String JETTY_ID = "jetty";

public static final String JONAS_ID = "jonas";

public static final String OC4J_ID = "oc4j";

public static final String RESIN_ID = "resin";

public static final String TOMCAT_ID = "tomcat";

public static final String WEBLOGIC_ID = "weblogic";

public static final String WEBSPHERE_ID = "websphere";

private static ServerDetector _instance = new ServerDetector();

private String _serverId;

private Boolean _geronimo;

private Boolean _glassfish;

private Boolean _glassfish2;

private Boolean _glassfish3;

private Boolean _jBoss;

private Boolean _jetty;

private Boolean _jonas;

private Boolean _oc4j;

private Boolean _resin;

private Boolean _tomcat;

private Boolean _webLogic;

private Boolean _webSphere;

public static String getServerId() {

ServerDetector sd = _instance;

if (sd._serverId == null) {

if (isGeronimo()) {

sd._serverId = "geronimo";

} else if (isGlassfish()) {

sd._serverId = "glassfish";

} else if (isJBoss()) {

sd._serverId = "jboss";

} else if (isJOnAS()) {

sd._serverId = "jonas";

} else if (isOC4J()) {

sd._serverId = "oc4j";

} else if (isResin()) {

sd._serverId = "resin";

} else if (isWebLogic()) {

sd._serverId = "weblogic";

} else if (isWebSphere()) {

sd._serverId = "websphere";

}

if (isJetty()) {

if (sd._serverId == null) {

sd._serverId = "jetty";

} else {

sd._serverId += "-jetty";

}

} else if (isTomcat()) {

if (sd._serverId == null) {

sd._serverId = "tomcat";

} else {

sd._serverId += "-tomcat";

}

}

if (sd._serverId == null) {

throw new RuntimeException("Server is not supported");

}

}

return sd._serverId;

}

public static boolean isGeronimo() {

ServerDetector sd = _instance;

if (sd._geronimo == null) {

sd._geronimo = _detect("/org/apache/geronimo/system/main/Daemon.class");

}

return sd._geronimo.booleanValue();

}

public static boolean isGlassfish() {

ServerDetector sd = _instance;

if (sd._glassfish == null) {

String value = System.getProperty("com.sun.aas.instanceRoot");

if (value != null) {

sd._glassfish = Boolean.TRUE;

} else {

sd._glassfish = Boolean.FALSE;

}

}

return sd._glassfish.booleanValue();

}

public static boolean isGlassfish2() {

ServerDetector sd = _instance;

if (sd._glassfish2 == null) {

if ((isGlassfish()) && (!(isGlassfish3()))) {

sd._glassfish2 = Boolean.TRUE;

} else {

sd._glassfish2 = Boolean.FALSE;

}

}

return sd._glassfish2.booleanValue();

}

public static boolean isGlassfish3() {

ServerDetector sd = _instance;

if (sd._glassfish3 == null) {

String value = "";

if (isGlassfish()) {

value = System.getProperty("product.name").trim();

}

if (value.equals("GlassFish/v3")) {

sd._glassfish3 = Boolean.TRUE;

} else {

sd._glassfish3 = Boolean.FALSE;

}

}

return sd._glassfish3.booleanValue();

}

public static boolean isJBoss() {

ServerDetector sd = _instance;

if (sd._jBoss == null) {

sd._jBoss = _detect("/org/jboss/Main.class");

}

return sd._jBoss.booleanValue();

}

public static boolean isJetty() {

ServerDetector sd = _instance;

if (sd._jetty == null) {

sd._jetty = _detect("/org/mortbay/jetty/Server.class");

}

return sd._jetty.booleanValue();

}

public static boolean isJOnAS() {

ServerDetector sd = _instance;

if (sd._jonas == null) {

sd._jonas = _detect("/org/objectweb/jonas/server/Server.class");

}

return sd._jonas.booleanValue();

}

public static boolean isOC4J() {

ServerDetector sd = _instance;

if (sd._oc4j == null) {

sd._oc4j = _detect("oracle.oc4j.util.ClassUtils");

}

return sd._oc4j.booleanValue();

}

public static boolean isResin() {

ServerDetector sd = _instance;

if (sd._resin == null) {

sd._resin = _detect("/com/caucho/server/resin/Resin.class");

}

return sd._resin.booleanValue();

}

public static boolean isSupportsComet() {

return false;

}

public static boolean isTomcat() {

ServerDetector sd = _instance;

if (sd._tomcat == null) {

sd._tomcat = _detect("/org/apache/catalina/startup/Bootstrap.class");

}

if (sd._tomcat == null) {

sd._tomcat = _detect("/org/apache/catalina/startup/Embedded.class");

}

return sd._tomcat.booleanValue();

}

public static boolean isWebLogic() {

ServerDetector sd = _instance;

if (sd._webLogic == null) {

sd._webLogic = _detect("/weblogic/Server.class");

}

return sd._webLogic.booleanValue();

}

public static boolean isWebSphere() {

ServerDetector sd = _instance;

if (sd._webSphere == null) {

sd._webSphere = _detect("/com/ibm/websphere/product/VersionInfo.class");

}

return sd._webSphere.booleanValue();

}

private static Boolean _detect(String className) {

try {

ClassLoader.getSystemClassLoader().loadClass(className);

return Boolean.TRUE;

} catch (ClassNotFoundException cnfe) {

ServerDetector sd = _instance;

Class c = sd.getClass();

if (c.getResource(className) != null) {

return Boolean.TRUE;

}

}

return Boolean.FALSE;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值