java判断服务器是那种,例如区分tomcat和weblogic

来源:互联网 发布:linux shell 当前目录 编辑:程序博客网 时间:2024/05/22 17:17
下面是服务器类型探测的类,原理就是用每个应用服务器自己独特的启动类来判断
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.roger.query.util;  
  2. import org.apache.log4j.Logger;  
  3. /** 
  4.  * @  服务器类型探测 
  5.  * @Date  2011/04/13 
  6.  * **/  
  7. public class ServerUtil {  
  8.  public static final String GERONIMO_CLASS = "/org/apache/geronimo/system/main/Daemon.class";  
  9.  public static final String JBOSS_CLASS = "/org/jboss/Main.class";  
  10.  public static final String JETTY_CLASS = "/org/mortbay/jetty/Server.class";  
  11.  public static final String JONAS_CLASS = "/org/objectweb/jonas/server/Server.class";  
  12.  public static final String OC4J_CLASS = "/oracle/jsp/oc4jutil/Oc4jUtil.class";  
  13.  public static final String ORION_CLASS = "/com/evermind/server/ApplicationServer.class";  
  14.  public static final String PRAMATI_CLASS = "/com/pramati/Server.class";  
  15.  public static final String RESIN_CLASS = "/com/caucho/server/resin/Resin.class";  
  16.  public static final String REXIP_CLASS = "/com/tcc/Main.class";  
  17.  public static final String SUN7_CLASS = "/com/iplanet/ias/tools/cli/IasAdminMain.class";  
  18.  public static final String SUN8_CLASS = "/com/sun/enterprise/cli/framework/CLIMain.class";  
  19.  public static final String TOMCAT_CLASS = "/org/apache/catalina/startup/Bootstrap.class";  
  20.  public static final String WEBLOGIC_CLASS = "/weblogic/Server.class";  
  21.  public static final String WEBSPHERE_CLASS = "/com/ibm/websphere/product/VersionInfo.class";  
  22.  public static String getServerId() {  
  23.   ServerUtil sd = _instance;  
  24.   if (sd._serverId == null) {  
  25.    if (ServerUtil.isGeronimo()) {  
  26.     sd._serverId = "geronimo";  
  27.    } else if (ServerUtil.isJBoss()) {  
  28.     sd._serverId = "jboss";  
  29.    } else if (ServerUtil.isJOnAS()) {  
  30.     sd._serverId = "jonas";  
  31.    } else if (ServerUtil.isOC4J()) {  
  32.     sd._serverId = "oc4j";  
  33.    } else if (ServerUtil.isOrion()) {  
  34.     sd._serverId = "orion";  
  35.    } else if (ServerUtil.isResin()) {  
  36.     sd._serverId = "resin";  
  37.    } else if (ServerUtil.isWebLogic()) {  
  38.     sd._serverId = "weblogic";  
  39.    } else if (ServerUtil.isWebSphere()) {  
  40.     sd._serverId = "websphere";  
  41.    }  
  42.    if (ServerUtil.isJetty()) {  
  43.     if (sd._serverId == null) {  
  44.      sd._serverId = "jetty";  
  45.     } else {  
  46.      sd._serverId += "-jetty";  
  47.     }  
  48.    } else if (ServerUtil.isTomcat()) {  
  49.     if (sd._serverId == null) {  
  50.      sd._serverId = "tomcat";  
  51.     } else {  
  52.      sd._serverId += "-tomcat";  
  53.     }  
  54.    }  
  55.    if (_log.isInfoEnabled()) {  
  56.     _log.info("Detected server " + sd._serverId);  
  57.    }  
  58.    if (sd._serverId == null) {  
  59.     throw new RuntimeException("Server is not supported");  
  60.    }  
  61.   }  
  62.   return sd._serverId;  
  63.  }  
  64.  public static boolean isGeronimo() {  
  65.   ServerUtil sd = _instance;  
  66.   if (sd._geronimo == null) {  
  67.    Class c = sd.getClass();  
  68.    if (c.getResource(GERONIMO_CLASS) != null) {  
  69.     sd._geronimo = Boolean.TRUE;  
  70.    } else {  
  71.     sd._geronimo = Boolean.FALSE;  
  72.    }  
  73.   }  
  74.   return sd._geronimo.booleanValue();  
  75.  }  
  76.  public static boolean isJBoss() {  
  77.   ServerUtil sd = _instance;  
  78.   if (sd._jBoss == null) {  
  79.    Class c = sd.getClass();  
  80.    if (c.getResource(JBOSS_CLASS) != null) {  
  81.     sd._jBoss = Boolean.TRUE;  
  82.    } else {  
  83.     sd._jBoss = Boolean.FALSE;  
  84.    }  
  85.   }  
  86.   return sd._jBoss.booleanValue();  
  87.  }  
  88.  public static boolean isJetty() {  
  89.   ServerUtil sd = _instance;  
  90.   if (sd._jetty == null) {  
  91.    Class c = sd.getClass();  
  92.    if (c.getResource(JETTY_CLASS) != null) {  
  93.     sd._jetty = Boolean.TRUE;  
  94.    } else {  
  95.     sd._jetty = Boolean.FALSE;  
  96.    }  
  97.   }  
  98.   return sd._jetty.booleanValue();  
  99.  }  
  100.  public static boolean isJOnAS() {  
  101.   ServerUtil sd = _instance;  
  102.   if (sd._jonas == null) {  
  103.    Class c = sd.getClass();  
  104.    if (c.getResource(JONAS_CLASS) != null) {  
  105.     sd._jonas = Boolean.TRUE;  
  106.    } else {  
  107.     sd._jonas = Boolean.FALSE;  
  108.    }  
  109.   }  
  110.   return sd._jonas.booleanValue();  
  111.  }  
  112.  public static boolean isOC4J() {  
  113.   ServerUtil sd = _instance;  
  114.   if (sd._oc4j == null) {  
  115.    Class c = sd.getClass();  
  116.    if (c.getResource(OC4J_CLASS) != null) {  
  117.     sd._oc4j = Boolean.TRUE;  
  118.    } else {  
  119.     sd._oc4j = Boolean.FALSE;  
  120.    }  
  121.   }  
  122.   return sd._oc4j.booleanValue();  
  123.  }  
  124.  public static boolean isOrion() {  
  125.   ServerUtil sd = _instance;  
  126.   if (sd._orion == null) {  
  127.    Class c = sd.getClass();  
  128.    if (c.getResource(ORION_CLASS) != null) {  
  129.     sd._orion = Boolean.TRUE;  
  130.    } else {  
  131.     sd._orion = Boolean.FALSE;  
  132.    }  
  133.   }  
  134.   return sd._orion.booleanValue();  
  135.  }  
  136.  public static boolean isPramati() {  
  137.   ServerUtil sd = _instance;  
  138.   if (sd._pramati == null) {  
  139.    Class c = sd.getClass();  
  140.    if (c.getResource(PRAMATI_CLASS) != null) {  
  141.     sd._pramati = Boolean.TRUE;  
  142.    } else {  
  143.     sd._pramati = Boolean.FALSE;  
  144.    }  
  145.   }  
  146.   return sd._pramati.booleanValue();  
  147.  }  
  148.  public static boolean isResin() {  
  149.   ServerUtil sd = _instance;  
  150.   if (sd._resin == null) {  
  151.    Class c = sd.getClass();  
  152.    if (c.getResource(RESIN_CLASS) != null) {  
  153.     sd._resin = Boolean.TRUE;  
  154.    } else {  
  155.     sd._resin = Boolean.FALSE;  
  156.    }  
  157.   }  
  158.   return sd._resin.booleanValue();  
  159.  }  
  160.  public static boolean isRexIP() {  
  161.   ServerUtil sd = _instance;  
  162.   if (sd._rexIP == null) {  
  163.    Class c = sd.getClass();  
  164.    if (c.getResource(REXIP_CLASS) != null) {  
  165.     sd._rexIP = Boolean.TRUE;  
  166.    } else {  
  167.     sd._rexIP = Boolean.FALSE;  
  168.    }  
  169.   }  
  170.   return sd._rexIP.booleanValue();  
  171.  }  
  172.  public static boolean isSun() {  
  173.   if (isSun7() || isSun8()) {  
  174.    return true;  
  175.   } else {  
  176.    return false;  
  177.   }  
  178.  }  
  179.  public static boolean isSun7() {  
  180.   ServerUtil sd = _instance;  
  181.   if (sd._sun7 == null) {  
  182.    Class c = sd.getClass();  
  183.    if (c.getResource(SUN7_CLASS) != null) {  
  184.     sd._sun7 = Boolean.TRUE;  
  185.    } else {  
  186.     sd._sun7 = Boolean.FALSE;  
  187.    }  
  188.   }  
  189.   return sd._sun7.booleanValue();  
  190.  }  
  191.  public static boolean isSun8() {  
  192.   ServerUtil sd = _instance;  
  193.   if (sd._sun8 == null) {  
  194.    Class c = sd.getClass();  
  195.    if (c.getResource(SUN8_CLASS) != null) {  
  196.     sd._sun8 = Boolean.TRUE;  
  197.    } else {  
  198.     sd._sun8 = Boolean.FALSE;  
  199.    }  
  200.   }  
  201.   return sd._sun8.booleanValue();  
  202.  }  
  203.  public static boolean isTomcat() {  
  204.   ServerUtil sd = _instance;  
  205.   if (sd._tomcat == null) {  
  206.    Class c = sd.getClass();  
  207.    if (c.getResource(TOMCAT_CLASS) != null) {  
  208.     sd._tomcat = Boolean.TRUE;  
  209.    } else {  
  210.     sd._tomcat = Boolean.FALSE;  
  211.    }  
  212.   }  
  213.   return sd._tomcat.booleanValue();  
  214.  }  
  215.  public static boolean isWebLogic() {  
  216.   ServerUtil sd = _instance;  
  217.   if (sd._webLogic == null) {  
  218.    Class c = sd.getClass();  
  219.    if (c.getResource(WEBLOGIC_CLASS) != null) {  
  220.     sd._webLogic = Boolean.TRUE;  
  221.    } else {  
  222.     sd._webLogic = Boolean.FALSE;  
  223.    }  
  224.   }  
  225.   return sd._webLogic.booleanValue();  
  226.  }  
  227.  public static boolean isWebSphere() {  
  228.   ServerUtil sd = _instance;  
  229.   if (sd._webSphere == null) {  
  230.    Class c = sd.getClass();  
  231.    if (c.getResource(WEBSPHERE_CLASS) != null) {  
  232.     sd._webSphere = Boolean.TRUE;  
  233.    } else {  
  234.     sd._webSphere = Boolean.FALSE;  
  235.    }  
  236.   }  
  237.   return sd._webSphere.booleanValue();  
  238.  }  
  239.  private ServerUtil() {  
  240.  }  
  241.  private static Logger _log = Logger.getLogger(ServerUtil.class);  
  242.  private static ServerUtil _instance = new ServerUtil();  
  243.  private String _serverId;  
  244.  private Boolean _geronimo;  
  245.  private Boolean _jBoss;  
  246.  private Boolean _jetty;  
  247.  private Boolean _jonas;  
  248.  private Boolean _oc4j;  
  249.  private Boolean _orion;  
  250.  private Boolean _pramati;  
  251.  private Boolean _resin;  
  252.  private Boolean _rexIP;  
  253.  private Boolean _sun7;  
  254.  private Boolean _sun8;  
  255.  private Boolean _tomcat;  
  256.  private Boolean _webLogic;  
  257.  private Boolean _webSphere;     
  258.    
  259. }  
0 0
原创粉丝点击