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.bootstrap.BootstrapCacheLoaderFactory;
20 import net.sf.ehcache.util.PropertyUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.Properties;
25
26
27 /***
28 * A factory to create a configured RMIBootstrapCacheLoader
29 * @author Greg Luck
30 * @version $Id: RMIBootstrapCacheLoaderFactory.html 13146 2011-08-01 17:12:39Z oletizi $
31 */
32 public class RMIBootstrapCacheLoaderFactory extends BootstrapCacheLoaderFactory<RMIBootstrapCacheLoader> {
33
34
35 /***
36 * The property name expected in ehcache.xml for the maximum chunk size in bytes
37 */
38 public static final String MAXIMUM_CHUNK_SIZE_BYTES = "maximumChunkSizeBytes";
39
40 /***
41 * The default maximum serialized size of the elements to request from a remote cache peer during bootstrap.
42 */
43 protected static final int DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES = 5000000;
44
45 /***
46 * The highest reasonable chunk size in bytes
47 */
48 protected static final int ONE_HUNDRED_MB = 100000000;
49
50 /***
51 * The lowest reasonable chunk size in bytes
52 */
53 protected static final int FIVE_KB = 5000;
54
55 private static final Logger LOG = LoggerFactory.getLogger(RMIBootstrapCacheLoaderFactory.class.getName());
56
57
58 /***
59 * Create a <code>BootstrapCacheLoader</code>
60 *
61 * @param properties implementation specific properties. These are configured as comma
62 * separated name value pairs in ehcache.xml
63 * @return a constructed BootstrapCacheLoader
64 */
65 public RMIBootstrapCacheLoader createBootstrapCacheLoader(Properties properties) {
66 boolean bootstrapAsynchronously = extractBootstrapAsynchronously(properties);
67 int maximumChunkSizeBytes = extractMaximumChunkSizeBytes(properties);
68 return new RMIBootstrapCacheLoader(bootstrapAsynchronously, maximumChunkSizeBytes);
69 }
70
71 /***
72 *
73 * @param properties the properties passed by the CacheManager, read from the configuration file
74 * @return the max chunk size in bytes
75 */
76 protected int extractMaximumChunkSizeBytes(Properties properties) {
77 int maximumChunkSizeBytes;
78 String maximumChunkSizeBytesString = PropertyUtil.extractAndLogProperty(MAXIMUM_CHUNK_SIZE_BYTES, properties);
79 if (maximumChunkSizeBytesString != null) {
80 try {
81 int maximumChunkSizeBytesCandidate = Integer.parseInt(maximumChunkSizeBytesString);
82 if ((maximumChunkSizeBytesCandidate < FIVE_KB) || (maximumChunkSizeBytesCandidate > ONE_HUNDRED_MB)) {
83 LOG.warn("Trying to set the chunk size to an unreasonable number. Using the default instead.");
84 maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
85 } else {
86 maximumChunkSizeBytes = maximumChunkSizeBytesCandidate;
87 }
88 } catch (NumberFormatException e) {
89 LOG.warn("Number format exception trying to set chunk size. Using the default instead.");
90 maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
91 }
92
93 } else {
94 maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
95 }
96 return maximumChunkSizeBytes;
97 }
98
99
100 }