1 /**
2  *  Copyright 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         @Override
119         public String toString() {
120             return "FileNameSource [file=" + file + "]";
121         }
122     }
123
124     /**
125      * {@link ConfigurationSource} based on URL
126      *
127      */

128     private static class URLConfigurationSource extends ConfigurationSource {
129         private final URL url;
130
131         /**
132          * Constructor accepting a URL
133          *
134          * @param url
135          */

136         public URLConfigurationSource(URL url) {
137             this.url = url;
138         }
139
140         /**
141          * {@inheritDoc}
142          *
143          * @see net.sf.ehcache.config.generator.ConfigurationSource#createConfiguration()
144          */

145         @Override
146         public Configuration createConfiguration() {
147             return ConfigurationFactory.parseConfiguration(url);
148         }
149
150         @Override
151         public String toString() {
152             return "URLConfigurationSource [url=" + url + "]";
153         }
154     }
155
156     /**
157      * {@link ConfigurationSource} based on {@link InputStream}
158      *
159      */

160     private static class InputStreamConfigurationSource extends ConfigurationSource {
161         private final InputStream stream;
162
163         /**
164          * Constructor accepting {@link InputStream}
165          *
166          * @param stream
167          */

168         public InputStreamConfigurationSource(InputStream stream) {
169             this.stream = stream;
170             stream.mark(Integer.MAX_VALUE);
171         }
172
173         /**
174          * {@inheritDoc}
175          *
176          * @see net.sf.ehcache.config.generator.ConfigurationSource#createConfiguration()
177          */

178         @Override
179         public Configuration createConfiguration() {
180             try {
181                 stream.reset();
182                 return ConfigurationFactory.parseConfiguration(stream);
183             } catch (IOException e) {
184                 throw new CacheException(e);
185             }
186         }
187
188         @Override
189         public String toString() {
190             return "InputStreamConfigurationSource [stream=" + stream + "]";
191         }
192
193     }
194
195     /**
196      * Default {@link ConfigurationSource} based on default ehcache.xml in classpath (if one is present) or the ehcache-failsafe.xml
197      * provided with the kit
198      *
199      */

200     private static class DefaultConfigurationSource extends ConfigurationSource {
201
202         /**
203          * Singleton instance of {@link DefaultConfigurationSource}
204          */

205         public static final DefaultConfigurationSource INSTANCE = new DefaultConfigurationSource();
206
207         /**
208          * private constructor
209          */

210         public DefaultConfigurationSource() {
211             //
212         }
213
214         /**
215          * {@inheritDoc}
216          *
217          * @see net.sf.ehcache.config.generator.ConfigurationSource#createConfiguration()
218          */

219         @Override
220         public Configuration createConfiguration() {
221             return ConfigurationFactory.parseConfiguration();
222         }
223
224         @Override
225         public String toString() {
226             return "DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]";
227         }
228
229     }
230
231 }
232