1 /*
2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 package de.smartics.properties.spi.config.cache;
17
18 import java.io.Serializable;
19
20 import de.smartics.properties.api.config.cache.CacheConfiguration;
21 import de.smartics.properties.spi.config.config.ConfigurationJndiLookupLoader;
22 import de.smartics.properties.spi.config.config.PropertiesConfiguration;
23
24 /**
25 * Responsible to read configuration properties to connect to a cache stored via
26 * JNDI.
27 */
28 public class CacheConfigurationLoader implements Serializable
29 {
30 // ********************************* Fields *********************************
31
32 // --- constants ------------------------------------------------------------
33
34 /**
35 * The class version identifier.
36 * <p>
37 * The value of this constant is {@value}.
38 * </p>
39 */
40 private static final long serialVersionUID = 1L;
41
42 // --- members --------------------------------------------------------------
43
44 // ****************************** Initializer *******************************
45
46 // ****************************** Constructors ******************************
47
48 /**
49 * Default constructor.
50 */
51 public CacheConfigurationLoader()
52 {
53 }
54
55 // ****************************** Inner Classes *****************************
56
57 // ********************************* Methods ********************************
58
59 // --- init -----------------------------------------------------------------
60
61 // --- get&set --------------------------------------------------------------
62
63 // --- business -------------------------------------------------------------
64
65 /**
66 * Loads the cache configuration with a name fetched from the classpath.
67 *
68 * @return the cache configuration.
69 * @throws CacheException on any problem loading the cache.
70 */
71 public CacheConfiguration load() throws CacheException
72 {
73 try
74 {
75 final CacheConfigurationPropertiesLoader propertiesLoader =
76 new CacheConfigurationPropertiesLoader();
77 final CacheConfiguration config = propertiesLoader.load();
78 return config;
79 }
80 catch (final IllegalStateException e)
81 {
82 return retryWithJndi();
83 }
84 catch (final CacheException e)
85 {
86 return retryWithJndi();
87 }
88 }
89
90 private CacheConfiguration retryWithJndi()
91 {
92 final CacheConfigurationJndiLoader jndiLoader =
93 new CacheConfigurationJndiLoader();
94 try
95 {
96 final CacheConfiguration config = jndiLoader.load();
97 return config;
98 }
99 catch (final CacheException e)
100 {
101 final ConfigurationJndiLookupLoader loader =
102 new ConfigurationJndiLookupLoader();
103 try
104 {
105 final PropertiesConfiguration allConfig = loader.load();
106 final CacheConfiguration config = allConfig.getCacheConfiguration();
107 return config;
108 }
109 catch (final IllegalStateException e2)
110 {
111 throw new CacheException(CacheMessageBean.configFailure(e,
112 "boot.properties"));
113 }
114 }
115 }
116
117 // --- object basics --------------------------------------------------------
118
119 }