1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.jumpmind.symmetric.util;
21
22 import java.net.InetAddress;
23 import java.util.Date;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.lang.time.FastDateFormat;
27 import org.jumpmind.symmetric.SymmetricEngine;
28 import org.jumpmind.symmetric.SymmetricWebServer;
29 import org.jumpmind.symmetric.common.Constants;
30
31 public class AppUtils {
32
33 private static String serverId;
34
35 private static FastDateFormat timezoneFormatter = FastDateFormat.getInstance("Z");
36
37 /***
38 * Get a unique identifier that represents the JVM instance this server is
39 * currently running in.
40 */
41 public static String getServerId() {
42 if (StringUtils.isBlank(serverId)) {
43 serverId = System.getProperty("runtime.symmetric.cluster.server.id", null);
44 if (StringUtils.isBlank(serverId)) {
45
46
47 serverId = System.getProperty("bind.address", null);
48 if (StringUtils.isBlank(serverId)) {
49 try {
50 serverId = InetAddress.getLocalHost().getHostName();
51 } catch (Exception ex) {
52 serverId = "unknown";
53 }
54 }
55 }
56 }
57 return serverId;
58 }
59
60 /***
61 * This method will return the timezone in RFC822 format.
62 * </p>
63 * The format ("-+HH:MM") has advantages over the older timezone codes
64 * ("AAA"). The difference of 5 hours from GMT is obvious with "-05:00" but
65 * only implied with "EST". There is no ambiguity saying "-06:00", but you
66 * don't know if "CST" means Central Standard Time ("-06:00") or China
67 * Standard Time ("+08:00"). The timezone codes need to be loaded on the
68 * system, and definitions are not standardized between systems. Therefore,
69 * to remain agnostic to operating systems and databases, the RFC822 format
70 * is the best choice.
71 */
72 public static String getTimezoneOffset() {
73 String tz = timezoneFormatter.format(new Date());
74 if (tz != null && tz.length() == 5) {
75 return tz.substring(0, 3) + ":" + tz.substring(3, 5);
76 }
77 return null;
78 }
79
80 /***
81 * Handy utility method to look up a SymmetricDS component given the bean
82 * name.
83 *
84 * @see Constants
85 */
86 @SuppressWarnings("unchecked")
87 public static <T> T find(String name, SymmetricEngine engine) {
88 return (T) engine.getApplicationContext().getBean(name);
89 }
90
91 @SuppressWarnings("unchecked")
92 public static <T> T find(String name, SymmetricWebServer server) {
93 return (T) server.getEngine().getApplicationContext().getBean(name);
94 }
95 }