[46] | 1 | package geniusweb.partiesserver;
|
---|
| 2 |
|
---|
| 3 | import java.lang.management.ManagementFactory;
|
---|
| 4 | import java.net.InetAddress;
|
---|
| 5 | import java.net.UnknownHostException;
|
---|
| 6 | import java.util.Set;
|
---|
| 7 |
|
---|
| 8 | import javax.management.MBeanServer;
|
---|
| 9 | import javax.management.MalformedObjectNameException;
|
---|
| 10 | import javax.management.ObjectName;
|
---|
| 11 | import javax.management.Query;
|
---|
| 12 |
|
---|
| 13 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * A central class containing the constants/configuration of the server.
|
---|
| 17 | *
|
---|
| 18 | * <b> This class should is private to the server and should not be used by
|
---|
| 19 | * others. However servers that derive from this server can completely override
|
---|
| 20 | * this class to change the constants/configuration</b>
|
---|
| 21 | */
|
---|
| 22 | public class Constants {
|
---|
| 23 | private static transient ObjectMapper jackson; // cache
|
---|
| 24 | private static transient String hostport = ""; // cache
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | *
|
---|
| 28 | * @return the object mapper, so that we can extend the objectmapper (with
|
---|
| 29 | * custom classes) centrally. Also allows us to cache the objectmapper
|
---|
| 30 | */
|
---|
| 31 | public static ObjectMapper getJackson() {
|
---|
| 32 | if (jackson == null) {
|
---|
| 33 | jackson = new ObjectMapper();
|
---|
| 34 | }
|
---|
| 35 | return jackson;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | *
|
---|
| 40 | * @return a string like "127.0.1.1:8080" containing the server address on which
|
---|
| 41 | * this service is running.
|
---|
| 42 | * @throws UnknownHostException if we can not get the host ip
|
---|
| 43 | * @throws MalformedObjectNameException if we can not get the host ip
|
---|
| 44 | */
|
---|
| 45 | public static String getIpAddressAndPort() throws UnknownHostException, MalformedObjectNameException {
|
---|
| 46 | synchronized (hostport) {
|
---|
| 47 | if (hostport.isEmpty()) {
|
---|
| 48 | MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
|
---|
| 49 |
|
---|
| 50 | Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
|
---|
| 51 | Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
|
---|
| 52 |
|
---|
| 53 | String host = InetAddress.getLocalHost().getHostAddress();
|
---|
| 54 | String port = objectNames.iterator().next().getKeyProperty("port");
|
---|
| 55 |
|
---|
| 56 | hostport = host + ":" + port;
|
---|
| 57 | }
|
---|
| 58 | return hostport;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | }
|
---|
| 62 | } |
---|