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.bootstrap;
18  
19  import net.sf.ehcache.util.PropertyUtil;
20  
21  import java.util.Properties;
22  
23  /***
24   * An abstract factory for creating BootstrapCacheLoader instances. Implementers should provide their own
25   * concrete factory extending this factory. It can then be configured in ehcache.xml.
26   *
27   * @param <T> The BootstrapCacheLoader type this Factory will create
28   * @author Greg Luck
29   * @version $Id: BootstrapCacheLoaderFactory.html 13146 2011-08-01 17:12:39Z oletizi $
30   */
31  public abstract class BootstrapCacheLoaderFactory<T extends BootstrapCacheLoader> {
32  
33      /***
34       * The property name expected in ehcache.xml for the bootstrap asyncrhonously switch.
35       */
36      public static final String BOOTSTRAP_ASYNCHRONOUSLY = "bootstrapAsynchronously";
37  
38      /***
39       * Create a <code>BootstrapCacheLoader</code>
40       *
41       * @param properties implementation specific properties. These are configured as comma
42       *                   separated name value pairs in ehcache.xml
43       * @return a constructed BootstrapCacheLoader
44       */
45      public abstract T createBootstrapCacheLoader(Properties properties);
46  
47      /***
48       * Extracts the value of bootstrapAsynchronously from the properties
49       *
50       * @param properties the properties passed by the CacheManager, read from the configuration file
51       * @return true if to be bootstrapped asynchronously, false otherwise
52       */
53      protected boolean extractBootstrapAsynchronously(Properties properties) {
54          return extractBoolean(properties, BOOTSTRAP_ASYNCHRONOUSLY, true);
55      }
56  
57      /***
58       * Will retrieve the boolean value from the properties, defaulting if property isn't present
59       * @param properties the properties to use
60       * @param prop the property name to look for
61       * @param defaultValue the default value if property is missing
62       * @return the value, or it's default, for the property
63       */
64      protected boolean extractBoolean(final Properties properties, final String prop, final boolean defaultValue) {
65          boolean value;
66          String propString = PropertyUtil.extractAndLogProperty(prop, properties);
67          if (propString != null) {
68              value = PropertyUtil.parseBoolean(propString);
69          } else {
70              value = defaultValue;
71          }
72          return value;
73      }
74  
75      /***
76       * Will retrieve the boolean value from the properties, defaulting if property isn't present
77       * @param properties the properties to use
78       * @param prop the property name to look for
79       * @param defaultValue the default value if property is missing
80       * @return the value, or it's default, for the property
81       */
82      protected long extractLong(final Properties properties, final String prop, final long defaultValue) {
83          long value;
84          String propString = PropertyUtil.extractAndLogProperty(prop, properties);
85          if (propString != null) {
86              value = Long.parseLong(propString);
87          } else {
88              value = defaultValue;
89          }
90          return value;
91      }
92  }