View Javadoc
1   package org.esigate.extension;
2   
3   import java.io.File;
4   import java.util.Properties;
5   
6   import org.esigate.Driver;
7   import org.esigate.DriverFactory;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import sun.misc.Signal;
12  import sun.misc.SignalHandler;
13  
14  /**
15   * This extension reloads configuration when signal HUP is received. On POSIX systems, this signal is sent using :
16   * <code>
17   * kill -1 &lt;esigatepid&gt;
18   * </code>
19   * <p>
20   * This only works on configuration defined using "esigate.config" system property.
21   * <p>
22   * This class relies on the sun.misc package and may not work on all JVM.
23   * 
24   * @author Nicolas Richeton
25   * 
26   */
27  @SuppressWarnings("restriction")
28  public class ConfigReloadOnHup implements Extension {
29      private static final Logger LOG = LoggerFactory.getLogger(ConfigReloadOnHup.class);
30      private static final String SIGNAL_NAME = "HUP";
31  
32      @Override
33      public void init(Driver driver, Properties properties) {
34          // Initialization is done is static block.
35      }
36  
37      private static final SignalHandler SH = new SignalHandler() {
38          @Override
39          public void handle(Signal signal) {
40              if (SIGNAL_NAME.equals(signal.getName())) {
41                  LOG.warn("Signal " + SIGNAL_NAME + " received. Reloading configuration.");
42                  DriverFactory.configure();
43              }
44  
45          }
46      };
47  
48      // Static block to ensure signal handler is added only once.
49      static {
50          String envPath = System.getProperty("esigate.config");
51          if (envPath != null) {
52              File configuration = new File(envPath);
53  
54              // Register for signal
55              Signal signal = new Signal(SIGNAL_NAME);
56              Signal.handle(signal, SH);
57  
58              LOG.info("Will reload configuration from {} on signal {}", configuration.getAbsoluteFile(),
59                      signal.getNumber());
60          } else {
61              // Do nothing if configuration is loaded from the classpath
62              LOG.warn("Cannot reload configuration from classpath. Please use -Desigate.config");
63          }
64      }
65  }