source: src/main/java/geniusweb/profilesserver/Constants.java@ 38

Last change on this file since 38 was 38, checked in by bart, 3 years ago

refactor to help reusing partiesserver

File size: 2.0 KB
Line 
1package geniusweb.profilesserver;
2
3import java.lang.management.ManagementFactory;
4import java.net.InetAddress;
5import java.net.UnknownHostException;
6import java.util.Set;
7
8import javax.management.MBeanServer;
9import javax.management.MalformedObjectNameException;
10import javax.management.ObjectName;
11import javax.management.Query;
12
13import 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 */
22public 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}
Note: See TracBrowser for help on using the repository browser.