View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.distribution;
18  
19  import net.sf.ehcache.CacheException;
20  import net.sf.ehcache.CacheManager;
21  import net.sf.ehcache.util.PropertyUtil;
22  
23  import java.net.UnknownHostException;
24  import java.util.Properties;
25  
26  /***
27   * Builds a listener based on RMI.
28   * <p/>
29   * Expected configuration line:
30   * <p/>
31   * <code>
32   * &lt;cachePeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
33   * properties="hostName=localhost, port=5000" /&gt;
34   * </code>
35   *
36   * @author Greg Luck
37   * @version $Id: RMICacheManagerPeerListenerFactory.html 13146 2011-08-01 17:12:39Z oletizi $
38   */
39  public class RMICacheManagerPeerListenerFactory extends CacheManagerPeerListenerFactory {
40  
41      /***
42       * The default timeout for cache replication for a single replication action.
43       * This may need to be increased for large data transfers.
44       */
45      public static final Integer DEFAULT_SOCKET_TIMEOUT_MILLIS = Integer.valueOf(120000);
46  
47      private static final String HOSTNAME = "hostName";
48      private static final String PORT = "port";
49      private static final String REMOTE_OBJECT_PORT = "remoteObjectPort";
50      private static final String SOCKET_TIMEOUT_MILLIS = "socketTimeoutMillis";
51  
52      /***
53       * @param properties implementation specific properties. These are configured as comma
54       *                   separated name value pairs in ehcache.xml
55       */
56      public final CacheManagerPeerListener createCachePeerListener(CacheManager cacheManager, Properties properties)
57              throws CacheException {
58          String hostName = PropertyUtil.extractAndLogProperty(HOSTNAME, properties);
59  
60          String portString = PropertyUtil.extractAndLogProperty(PORT, properties);
61          Integer port = null;
62          if (portString != null && portString.length() != 0) {
63              port = Integer.valueOf(portString);
64          } else {
65              port = Integer.valueOf(0);
66          }
67  
68          //0 means any port in UnicastRemoteObject, so it is ok if not specified to make it 0
69          String remoteObjectPortString = PropertyUtil.extractAndLogProperty(REMOTE_OBJECT_PORT, properties);
70          Integer remoteObjectPort = null;
71          if (remoteObjectPortString != null && remoteObjectPortString.length() != 0) {
72              remoteObjectPort = Integer.valueOf(remoteObjectPortString);
73          } else {
74              remoteObjectPort = Integer.valueOf(0);
75          }
76  
77          String socketTimeoutMillisString = PropertyUtil.extractAndLogProperty(SOCKET_TIMEOUT_MILLIS, properties);
78          Integer socketTimeoutMillis;
79          if (socketTimeoutMillisString == null || socketTimeoutMillisString.length() == 0) {
80              socketTimeoutMillis = DEFAULT_SOCKET_TIMEOUT_MILLIS;
81          } else {
82              socketTimeoutMillis = Integer.valueOf(socketTimeoutMillisString);
83          }
84          return doCreateCachePeerListener(hostName, port, remoteObjectPort, cacheManager, socketTimeoutMillis);
85      }
86  
87      /***
88       * A template method to actually create the factory
89       *
90       * @param hostName
91       * @param port
92       * @param remoteObjectPort
93       * @param cacheManager
94       * @param socketTimeoutMillis @return a crate CacheManagerPeerListener
95       */
96      protected CacheManagerPeerListener doCreateCachePeerListener(String hostName,
97                                                                   Integer port,
98                                                                   Integer remoteObjectPort,
99                                                                   CacheManager cacheManager,
100                                                                  Integer socketTimeoutMillis) {
101         try {
102             return new RMICacheManagerPeerListener(hostName, port, remoteObjectPort, cacheManager, socketTimeoutMillis);
103         } catch (UnknownHostException e) {
104             throw new CacheException("Unable to create CacheManagerPeerListener. Initial cause was " + e.getMessage(), e);
105         }
106     }
107 }