package de.ivu.elect.jee.jboss;

import java.io.Serializable;
import java.lang.management.ManagementFactory;

import javax.inject.Named;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named(JBossVersion.COMPONENT_NAME)

public class JBossVersion implements Serializable {

  private static final Logger LOG = LoggerFactory.getLogger(JBossVersion.class);

  public static final String COMPONENT_NAME = "jBossVersion";

  public static final String UNKNOWN = "Unknown";


  @Deprecated // immer Wildfly
  public static final boolean IS_WILDFLY = true;


  private static String bestimmeServerVersion() {
    try {
      MBeanServer server = ManagementFactory.getPlatformMBeanServer();
      ObjectName name = new ObjectName("jboss.as:management-root=server");
      String version = (String) (server.getAttribute(name, "releaseVersion"));
      return version;
    } catch (Exception e) {
      LOG.warn("Server-Version kann nicht bestimmt werden. " + e.getMessage());
    }
    return JBossVersion.UNKNOWN;
  }

  @Deprecated // immer Wildfly
  public boolean isWildfly() {
    return IS_WILDFLY;
  }

  public String getServerVersion() {
    return bestimmeServerVersion();
  }

}
