View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
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                  // JBoss uses this system property to identify a server in a
46                  // cluster
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  }