Javaでwebサーバーの名前(tomcat、jetty、jboss、glassish、jonas、weblogic、websphere)を取得するプログラム
package com.arkgame.combine.server;
import com.arkgame.combine.StringEx;
import com.arkgame.combine.sys.ClassUtil;
//クラスServerDetector
public class ServerDetector {
public static final String TOMCAT_ID = “tomcat";
public static final String JETTY_ID = “jetty";
public static final String JBOSS_ID = “jboss";
public static final String GLASSFISH_ID = “glassfish";
public static final String GERONIMO_ID = “geronimo";
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 WEBLOGIC_ID = “weblogic";
public static final String WEBSPHERE_ID = “websphere";
private static String _serverId;
private static int _geronimo = -1;
private static int _glassfish = -1;
private static int _jBoss = -1;
private static int _jetty = -1;
private static int _jonas = -1;
private static int _oc4j = -1;
private static int _resin = -1;
private static int _tomcat = -1;
private static int _webLogic = -1;
private static int _webSphere = -1;
static {
if (isTomcat()) {
_serverId = TOMCAT_ID;
} else if (isJetty()) {
_serverId = JETTY_ID;
} else if (isJBoss()) {
_serverId = JBOSS_ID;
} else if (isGlassfish()) {
_serverId = GLASSFISH_ID;
} else if (isResin()) {
_serverId = RESIN_ID;
} else if (isWebLogic()) {
_serverId = WEBLOGIC_ID;
} else if (isWebSphere()) {
_serverId = WEBSPHERE_ID;
} else if (isOC4J()) {
_serverId = OC4J_ID;
} else if (isGeronimo()) {
_serverId = GERONIMO_ID;
} else if (isJOnAS()) {
_serverId = JONAS_ID;
} else {
throw new RuntimeException(“Server is not supported");
}
}
public static String getServerId() {
return _serverId;
}
public static boolean isGeronimo() {
if (-1 == _geronimo) {
_geronimo = ClassUtil.detectResource(“/org/apache/geronimo/system/main/Daemon.class") ? 1 : 0;
}
return 1 == _geronimo;
}
public static boolean isGlassfish() {
if (-1 == _glassfish) {
_glassfish = !StringEx.isNullOrEmpty(System.getProperty(“com.sun.aas.instanceRoot")) ? 1 : 0;
}
return 1 == _glassfish;
}
public static String getClassfishVersion() {
if (!isGlassfish()) {
return null;
}
String value = System.getProperty(“product.name").trim();
//"GlassFish/v3″
if (!StringEx.isNullOrEmpty(value)) {
return value.replace(“GlassFish/", “");
}
return null;
}
public static boolean isJBoss() {
if (-1 == _jBoss) {
_jBoss = ClassUtil.detectResource(“/org/jboss/Main.class") ? 1 : 0;
}
return 1 == _jBoss;
}
public static boolean isJetty() {
if (-1 == _jetty) {
_jetty = ClassUtil.detectResource(“/org/mortbay/jetty/Server.class") ? 1 : 0;
}
return 1 == _jetty;
}
public static boolean isJOnAS() {
if (-1 == _jonas) {
_jonas = ClassUtil.detectResource(“/org/objectweb/jonas/server/Server.class") ? 1 : 0;
}
return 1 == _jonas;
}
public static boolean isOC4J() {
if (-1 == _oc4j) {
_oc4j = ClassUtil.detectClass(“oracle.oc4j.util.ClassUtils") ? 1 : 0;
}
return 1 == _oc4j;
}
public static boolean isResin() {
if (-1 == _resin) {
_resin = ClassUtil.detectResource(“/com/caucho/server/resin/Resin.class") ? 1 : 0;
}
return 1 == _resin;
}
public static boolean isTomcat() {
if (-1 == _tomcat) {
_tomcat = ClassUtil.detectResource(“/org/apache/catalina/startup/Bootstrap.class")
|| ClassUtil.detectResource(“/org/apache/catalina/startup/Embedded.class")
? 1 : 0;
}
return 1 == _tomcat;
}
public static boolean isWebLogic() {
if (-1 == _webLogic) {
_webLogic = ClassUtil.detectResource(“/weblogic/Server.class") ? 1 : 0;
}
return 1 == _webLogic;
}
public static boolean isWebSphere() {
if (-1 == _webSphere) {
_webSphere = ClassUtil.detectResource(“/com/ibm/websphere/product/VersionInfo.class") ? 1 : 0;
}
return 1 == _webSphere;
}
}
//クラスClassUtil
package com.arkgame.combine.sys;
public class ClassUtil {
/**
* クラスが存在するかどうかを確認
* @param className クラス名前
* @return
*/
public static boolean detectClass(String className) {
try {
ClassLoader.getSystemClassLoader().loadClass(className);
return true;
} catch (ClassNotFoundException cnfe) {
return false;
}
}
public static boolean detectResource(String fullName) {
return ClassUtil.class.getResource(fullName) != null;
}
}
//クラスStringEx
.package com.arkgame.combine;
public class StringEx{
public static boolean isNullOrEmpty(String str) {
return (null == str) || (str.equals(“"));
}
}