View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
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.bootstrap.BootstrapCacheLoader;
21  import net.sf.ehcache.util.PropertyUtil;
22  
23  import java.util.Properties;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * A factory to create a configured RMIBootstrapCacheLoader
30   * @author Greg Luck
31   * @version $Id: RMIBootstrapCacheLoaderFactory.java 512 2007-07-10 09:18:45Z gregluck $
32   */
33  public class RMIBootstrapCacheLoaderFactory extends BootstrapCacheLoaderFactory {
34  
35  
36      /**
37       * The property name expected in ehcache.xml for the bootstrap asyncrhonously switch.
38       */
39      public static final String BOOTSTRAP_ASYNCHRONOUSLY = "bootstrapAsynchronously";
40  
41      /**
42       * The property name expected in ehcache.xml for the maximum chunk size in bytes
43       */
44      public static final String MAXIMUM_CHUNK_SIZE_BYTES = "maximumChunkSizeBytes";
45  
46      /**
47       * The default maximum serialized size of the elements to request from a remote cache peer during bootstrap.
48       */
49      protected static final int DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES = 5000000;
50  
51      /**
52       * The highest reasonable chunk size in bytes
53       */
54      protected static final int ONE_HUNDRED_MB = 100000000;
55  
56      /**
57       * The lowest reasonable chunk size in bytes
58       */
59      protected static final int FIVE_KB = 5000;
60  
61      private static final Log LOG = LogFactory.getLog(RMIBootstrapCacheLoaderFactory.class.getName());
62  
63  
64      /**
65       * Create a <code>BootstrapCacheLoader</code>
66       *
67       * @param properties implementation specific properties. These are configured as comma
68       *                   separated name value pairs in ehcache.xml
69       * @return a constructed BootstrapCacheLoader
70       */
71      public BootstrapCacheLoader createBootstrapCacheLoader(Properties properties) {
72          boolean bootstrapAsynchronously = extractBootstrapAsynchronously(properties);
73          int maximumChunkSizeBytes = extractMaximumChunkSizeBytes(properties);
74          return new RMIBootstrapCacheLoader(bootstrapAsynchronously, maximumChunkSizeBytes);
75      }
76  
77      /**
78       *
79       * @param properties
80       */
81      protected int extractMaximumChunkSizeBytes(Properties properties) {
82          int maximumChunkSizeBytes = 0;
83          String maximumChunkSizeBytesString = PropertyUtil.extractAndLogProperty(MAXIMUM_CHUNK_SIZE_BYTES, properties);
84          if (maximumChunkSizeBytesString != null) {
85              try {
86                  int maximumChunkSizeBytesCandidate = Integer.parseInt(maximumChunkSizeBytesString);
87                  if ((maximumChunkSizeBytesCandidate < FIVE_KB) || (maximumChunkSizeBytesCandidate > ONE_HUNDRED_MB)) {
88                      LOG.warn("Trying to set the chunk size to an unreasonable number. Using the default instead.");
89                      maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
90                  } else {
91                      maximumChunkSizeBytes = maximumChunkSizeBytesCandidate;
92                  }
93              } catch (NumberFormatException e) {
94                  LOG.warn("Number format exception trying to set chunk size. Using the default instead.");
95                  maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
96              }
97  
98          } else {
99              maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
100         }
101         return maximumChunkSizeBytes;
102     }
103 
104 
105     /**
106      * Extracts the value of bootstrapAsynchronously from the properties
107      *
108      * @param properties
109      */
110     protected boolean extractBootstrapAsynchronously(Properties properties) {
111         boolean bootstrapAsynchronously;
112         String bootstrapAsynchronouslyString = PropertyUtil.extractAndLogProperty(BOOTSTRAP_ASYNCHRONOUSLY, properties);
113         if (bootstrapAsynchronouslyString != null) {
114             bootstrapAsynchronously = PropertyUtil.parseBoolean(bootstrapAsynchronouslyString);
115         } else {
116             bootstrapAsynchronously = true;
117         }
118         return bootstrapAsynchronously;
119     }
120 }