001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;
018: import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
019: import net.sf.ehcache.util.PropertyUtil;
020:
021: import java.util.Properties;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /**
027: * A factory to create a configured RMIBootstrapCacheLoader
028: * @author Greg Luck
029: * @version $Id: RMIBootstrapCacheLoaderFactory.java 519 2007-07-27 07:11:45Z gregluck $
030: */
031: public class RMIBootstrapCacheLoaderFactory extends
032: BootstrapCacheLoaderFactory {
033:
034: /**
035: * The property name expected in ehcache.xml for the bootstrap asyncrhonously switch.
036: */
037: public static final String BOOTSTRAP_ASYNCHRONOUSLY = "bootstrapAsynchronously";
038:
039: /**
040: * The property name expected in ehcache.xml for the maximum chunk size in bytes
041: */
042: public static final String MAXIMUM_CHUNK_SIZE_BYTES = "maximumChunkSizeBytes";
043:
044: /**
045: * The default maximum serialized size of the elements to request from a remote cache peer during bootstrap.
046: */
047: protected static final int DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES = 5000000;
048:
049: /**
050: * The highest reasonable chunk size in bytes
051: */
052: protected static final int ONE_HUNDRED_MB = 100000000;
053:
054: /**
055: * The lowest reasonable chunk size in bytes
056: */
057: protected static final int FIVE_KB = 5000;
058:
059: private static final Log LOG = LogFactory
060: .getLog(RMIBootstrapCacheLoaderFactory.class.getName());
061:
062: /**
063: * Create a <code>BootstrapCacheLoader</code>
064: *
065: * @param properties implementation specific properties. These are configured as comma
066: * separated name value pairs in ehcache.xml
067: * @return a constructed BootstrapCacheLoader
068: */
069: public BootstrapCacheLoader createBootstrapCacheLoader(
070: Properties properties) {
071: boolean bootstrapAsynchronously = extractBootstrapAsynchronously(properties);
072: int maximumChunkSizeBytes = extractMaximumChunkSizeBytes(properties);
073: return new RMIBootstrapCacheLoader(bootstrapAsynchronously,
074: maximumChunkSizeBytes);
075: }
076:
077: /**
078: *
079: * @param properties
080: */
081: protected int extractMaximumChunkSizeBytes(Properties properties) {
082: int maximumChunkSizeBytes = 0;
083: String maximumChunkSizeBytesString = PropertyUtil
084: .extractAndLogProperty(MAXIMUM_CHUNK_SIZE_BYTES,
085: properties);
086: if (maximumChunkSizeBytesString != null) {
087: try {
088: int maximumChunkSizeBytesCandidate = Integer
089: .parseInt(maximumChunkSizeBytesString);
090: if ((maximumChunkSizeBytesCandidate < FIVE_KB)
091: || (maximumChunkSizeBytesCandidate > ONE_HUNDRED_MB)) {
092: LOG
093: .warn("Trying to set the chunk size to an unreasonable number. Using the default instead.");
094: maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
095: } else {
096: maximumChunkSizeBytes = maximumChunkSizeBytesCandidate;
097: }
098: } catch (NumberFormatException e) {
099: LOG
100: .warn("Number format exception trying to set chunk size. Using the default instead.");
101: maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
102: }
103:
104: } else {
105: maximumChunkSizeBytes = DEFAULT_MAXIMUM_CHUNK_SIZE_BYTES;
106: }
107: return maximumChunkSizeBytes;
108: }
109:
110: /**
111: * Extracts the value of bootstrapAsynchronously from the properties
112: *
113: * @param properties
114: */
115: protected boolean extractBootstrapAsynchronously(
116: Properties properties) {
117: boolean bootstrapAsynchronously;
118: String bootstrapAsynchronouslyString = PropertyUtil
119: .extractAndLogProperty(BOOTSTRAP_ASYNCHRONOUSLY,
120: properties);
121: if (bootstrapAsynchronouslyString != null) {
122: bootstrapAsynchronously = PropertyUtil
123: .parseBoolean(bootstrapAsynchronouslyString);
124: } else {
125: bootstrapAsynchronously = true;
126: }
127: return bootstrapAsynchronously;
128: }
129: }
|