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.config.generator;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URL;
23  
24  import net.sf.ehcache.CacheException;
25  import net.sf.ehcache.config.Configuration;
26  import net.sf.ehcache.config.ConfigurationFactory;
27  
28  /***
29   * Class encapsulating the source of configuration for a cache manager
30   * 
31   * <p />
32   * 
33   * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
34   * 
35   */
36  public abstract class ConfigurationSource {
37      /***
38       * protected constructor
39       */
40      protected ConfigurationSource() {
41          //
42      }
43  
44      /***
45       * Utility factory method for creating a {@link ConfigurationSource} based on a file.
46       * 
47       * @param file
48       * @return ConfigurationSource for the input file
49       */
50      public static ConfigurationSource getConfigurationSource(File file) {
51          return new FileNameSource(file);
52      }
53  
54      /***
55       * Utility factory method for creating a {@link ConfigurationSource} based on {@link URL}
56       * 
57       * @param configFileURL
58       * @return ConfigurationSource for the input URL
59       */
60      public static ConfigurationSource getConfigurationSource(URL configFileURL) {
61          return new URLConfigurationSource(configFileURL);
62      }
63  
64      /***
65       * Utility factory method for creating a {@link ConfigurationSource} based on InputStream
66       * 
67       * @param configFileStream
68       * @return ConfigurationSource for the input InputStream
69       */
70      public static ConfigurationSource getConfigurationSource(InputStream configFileStream) {
71          return new InputStreamConfigurationSource(configFileStream);
72      }
73  
74      /***
75       * Utility factory method for creating a {@link ConfigurationSource} based on default settings (default ehcache.xml in classpath if one
76       * is present or an ehcache-failsafe provided with the kit
77       * 
78       * @return Default ConfigurationSource
79       */
80      public static ConfigurationSource getConfigurationSource() {
81          return DefaultConfigurationSource.INSTANCE;
82      }
83  
84      /***
85       * Abstract method used for creating a {@link Configuration} based on the source
86       * 
87       * @return {@link Configuration} based on the source
88       */
89      public abstract Configuration createConfiguration();
90  
91      /***
92       * {@link ConfigurationSource} based on file name
93       * 
94       */
95      private static class FileNameSource extends ConfigurationSource {
96  
97          private final File file;
98  
99          /***
100          * Constructor accepting the file name
101          * 
102          * @param fileName
103          */
104         public FileNameSource(File file) {
105             this.file = file;
106         }
107 
108         /***
109          * {@inheritDoc}
110          * 
111          * @see net.sf.ehcache.config.generator.ConfigurationSource#getConfiguration()
112          */
113         @Override
114         public Configuration createConfiguration() {
115             return ConfigurationFactory.parseConfiguration(file);
116         }
117     }
118 
119     /***
120      * {@link ConfigurationSource} based on URL
121      * 
122      */
123     private static class URLConfigurationSource extends ConfigurationSource {
124         private final URL url;
125 
126         /***
127          * Constructor accepting a URL
128          * 
129          * @param url
130          */
131         public URLConfigurationSource(URL url) {
132             this.url = url;
133         }
134 
135         /***
136          * {@inheritDoc}
137          * 
138          * @see net.sf.ehcache.config.generator.ConfigurationSource#createConfiguration()
139          */
140         @Override
141         public Configuration createConfiguration() {
142             return ConfigurationFactory.parseConfiguration(url);
143         }
144 
145     }
146 
147     /***
148      * {@link ConfigurationSource} based on {@link InputStream}
149      * 
150      */
151     private static class InputStreamConfigurationSource extends ConfigurationSource {
152         private final InputStream stream;
153 
154         /***
155          * Constructor accepting {@link InputStream}
156          * 
157          * @param stream
158          */
159         public InputStreamConfigurationSource(InputStream stream) {
160             this.stream = stream;
161             stream.mark(Integer.MAX_VALUE);
162         }
163 
164         /***
165          * {@inheritDoc}
166          * 
167          * @see net.sf.ehcache.config.generator.ConfigurationSource#createConfiguration()
168          */
169         @Override
170         public Configuration createConfiguration() {
171             try {
172                 stream.reset();
173                 return ConfigurationFactory.parseConfiguration(stream);
174             } catch (IOException e) {
175                 throw new CacheException(e);
176             }
177         }
178 
179     }
180 
181     /***
182      * Default {@link ConfigurationSource} based on default ehcache.xml in classpath (if one is present) or the ehcache-failsafe.xml
183      * provided with the kit
184      * 
185      */
186     private static class DefaultConfigurationSource extends ConfigurationSource {
187 
188         /***
189          * Singleton instance of {@link DefaultConfigurationSource}
190          */
191         public static final DefaultConfigurationSource INSTANCE = new DefaultConfigurationSource();
192 
193         /***
194          * private constructor
195          */
196         public DefaultConfigurationSource() {
197             //
198         }
199 
200         /***
201          * {@inheritDoc}
202          * 
203          * @see net.sf.ehcache.config.generator.ConfigurationSource#createConfiguration()
204          */
205         @Override
206         public Configuration createConfiguration() {
207             return ConfigurationFactory.parseConfiguration();
208         }
209 
210     }
211 
212 }