1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.cache;
17
18 import static de.smartics.properties.api.config.cache.CacheConfiguration.CACHE_NAME;
19 import static de.smartics.properties.api.config.cache.CacheConfiguration.JNDI_NAME;
20
21 import java.io.Serializable;
22
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26
27 import org.apache.commons.lang.StringUtils;
28
29 import de.smartics.properties.api.config.cache.CacheConfiguration;
30 import de.smartics.properties.api.core.app.JndiContext;
31
32
33
34
35
36 public class CacheConfigurationJndiLoader implements Serializable
37 {
38
39
40
41
42
43
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50
51
52
53
54
55
56
57
58
59 public CacheConfigurationJndiLoader()
60 {
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public CacheConfiguration load() throws CacheException
80 {
81 try
82 {
83 final Context context = new InitialContext();
84 final JndiContext lookup = JndiContext.boot(context);
85
86 final String sourceId = lookup.getSourceId();
87 final String jndiName = lookupMandatory(lookup, sourceId, JNDI_NAME);
88 final String cacheName = lookupMandatory(lookup, sourceId, CACHE_NAME);
89
90 final CacheConfiguration config = new CacheConfiguration();
91 config.setCacheId(sourceId);
92 config.setJndiName(jndiName);
93 config.setCacheName(cacheName);
94 return config;
95 }
96 catch (final Exception e)
97 {
98 throw new CacheException(CacheMessageBean.configFailure(e,
99 JndiContext.JNDI_CONTEXT_PATH_BOOT));
100 }
101 }
102
103 private String lookupMandatory(final JndiContext lookup,
104 final String sourceId, final String key) throws NamingException
105 {
106 final String value = lookup.lookup(key);
107 if (StringUtils.isBlank(value))
108 {
109 throw new CacheException(CacheMessageBean.missingProperty(sourceId, key));
110 }
111 return value;
112 }
113
114
115
116 }