View Javadoc
1   package com.atlassian.cache.ehcache.cluster;
2   
3   import java.io.IOException;
4   import java.io.StringWriter;
5   import java.net.InetAddress;
6   import java.net.URL;
7   import java.net.UnknownHostException;
8   import java.util.Properties;
9   
10  import net.sf.ehcache.config.Configuration;
11  import net.sf.ehcache.config.ConfigurationFactory;
12  import net.sf.ehcache.config.FactoryConfiguration;
13  import net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory;
14  
15  
16  /**
17   * Builds configuration details for ehcache.
18   */
19  public class EhCacheConfigurationFactory
20  {
21      public static final String LOCALHOST = getLocalHostName();
22      public static final int BASE_PORT = 40000;
23      public static final int SOCKET_TIMEOUT_MILLIS = 5000;
24  
25      public static Configuration newConfiguration(final int thisNodeId, final int maxNodeId)
26      {
27          final FactoryConfiguration<?> peerListenerFactory = buildPeerListenerFactory(thisNodeId);
28          final FactoryConfiguration<?> peerProviderFactory = buildPeerProviderFactory(thisNodeId, maxNodeId);
29  
30          final URL configXml = EhCacheConfigurationFactory.class.getClassLoader().getResource("ehcache.xml");
31          if (configXml == null)
32          {
33              throw new AssertionError("Failed to load ehcache.xml");
34          }
35  
36          return ConfigurationFactory.parseConfiguration(configXml)
37                  .name("node" + thisNodeId)
38                  .cacheManagerPeerProviderFactory(peerProviderFactory)
39                  .cacheManagerPeerListenerFactory(peerListenerFactory);
40      }
41  
42      private static FactoryConfiguration<?> buildPeerListenerFactory(final int thisNodeId)
43      {
44          final Properties properties = new Properties();
45          properties.setProperty("hostName", LOCALHOST);
46          properties.setProperty("port", String.valueOf(BASE_PORT + thisNodeId));
47          properties.setProperty("socketTimeoutMillis", String.valueOf(SOCKET_TIMEOUT_MILLIS));
48  
49          return new FactoryConfiguration()
50                  .className(RMICacheManagerPeerListenerFactory.class.getName())
51                  .properties(propertiesToString(properties));
52      }
53  
54      private static FactoryConfiguration<?> buildPeerProviderFactory(final int thisNodeId, final int maxNodeId)
55      {
56          final Properties properties = new Properties();
57          properties.setProperty(EhCachePeerProvider.THIS_NODE_ID, String.valueOf(thisNodeId));
58          properties.setProperty(EhCachePeerProvider.MAX_NODE_ID, String.valueOf(maxNodeId));
59  
60          return new FactoryConfiguration()
61                  .className(EhCachePeerProviderFactory.class.getName())
62                  .properties(propertiesToString(properties));
63      }
64  
65      private static String propertiesToString(final Properties properties)
66      {
67          final StringWriter out = new StringWriter();
68          try
69          {
70              properties.store(out, null);
71          }
72          catch (IOException e)
73          {
74              // Nothing should go wrong with a String Writer
75              throw new RuntimeException(e);
76          }
77          return out.toString();
78      }
79  
80      private static String getLocalHostName()
81      {
82          try
83          {
84              final InetAddress host = InetAddress.getLocalHost();
85              final String hostname = host.getHostName();
86              return (hostname != null && !hostname.equals("localhost")) ? hostname : host.getCanonicalHostName();
87          }
88          catch (UnknownHostException e)
89          {
90              throw new AssertionError(e);
91          }
92      }
93  }