package geniusweb.partiesserver;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* A central class containing the constants/configuration of the server.
*
* This class should is private to the server and should not be used by
* others. However servers that derive from this server can completely override
* this class to change the constants/configuration
*/
public class Constants {
private static transient ObjectMapper jackson; // cache
private static transient String hostport = ""; // cache
/**
*
* @return the object mapper, so that we can extend the objectmapper (with
* custom classes) centrally. Also allows us to cache the objectmapper
*/
public static ObjectMapper getJackson() {
if (jackson == null) {
jackson = new ObjectMapper();
}
return jackson;
}
/**
*
* @return a string like "127.0.1.1:8080" containing the server address on which
* this service is running.
* @throws UnknownHostException if we can not get the host ip
* @throws MalformedObjectNameException if we can not get the host ip
*/
public static String getIpAddressAndPort() throws UnknownHostException, MalformedObjectNameException {
synchronized (hostport) {
if (hostport.isEmpty()) {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String host = InetAddress.getLocalHost().getHostAddress();
String port = objectNames.iterator().next().getKeyProperty("port");
hostport = host + ":" + port;
}
return hostport;
}
}
}