1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.jndi;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.naming.NameClassPair;
22 import javax.naming.NamingEnumeration;
23 import javax.naming.NamingException;
24
25 import de.smartics.properties.api.config.domain.Property;
26 import de.smartics.properties.api.config.domain.PropertyCollection;
27 import de.smartics.properties.api.config.domain.PropertyStoreAccessor;
28 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29 import de.smartics.properties.api.core.app.JndiContext;
30 import de.smartics.properties.spi.config.support.NativePropertyCollection;
31
32
33
34
35 final class JndiPropertyStoreAccessor implements PropertyStoreAccessor
36 {
37
38
39
40
41
42
43
44
45
46
47 private static final long serialVersionUID = 1L;
48
49
50
51
52
53
54 private final ConfigurationKey<?> key;
55
56
57
58
59 private final JndiContext context;
60
61
62
63
64
65 JndiPropertyStoreAccessor(final ConfigurationKey<?> key,
66 final JndiContext context)
67 {
68 this.key = key;
69 this.context = context;
70 }
71
72
73
74
75
76
77
78
79
80
81
82 @Override
83 public Property getPropertyFromStore(final String name)
84 throws JndiPropertyStoreException
85 {
86 try
87 {
88 final String value = context.lookup(name);
89 final Property property =
90 new Property(context.getSourceId(), name, value);
91 return property;
92 }
93 catch (final NamingException e)
94 {
95 throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
96 JndiPropertyStoreCode.CANNOT_GET_PROPERTY, e, key, name));
97 }
98 }
99
100 @Override
101 public Property setPropertyToStore(final String name, final String value)
102 throws JndiPropertyStoreException
103 {
104 try
105 {
106 final Property oldProperty = getPropertyFromStore(name);
107 context.set(name, value);
108 return oldProperty;
109 }
110 catch (final NamingException e)
111 {
112 throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
113 JndiPropertyStoreCode.CANNOT_SET_PROPERTY, e, key, name));
114 }
115 }
116
117 @Override
118 public Property deletePropertyInStore(final String name)
119 throws JndiPropertyStoreException
120 {
121 try
122 {
123 final Property oldProperty = getPropertyFromStore(name);
124 context.set(name, null);
125 return oldProperty;
126 }
127 catch (final NamingException e)
128 {
129 throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
130 JndiPropertyStoreCode.CANNOT_DELETE_PROPERTY, e, key, name));
131 }
132 }
133
134 @Override
135 public PropertyCollection getPropertyCollectionFromStore()
136 throws JndiPropertyStoreException
137 {
138 try
139 {
140 final Map<String, Property> properties = new HashMap<String, Property>();
141 final NamingEnumeration<NameClassPair> list = context.list();
142 while (list.hasMore())
143 {
144 final NameClassPair pair = list.next();
145 final String name = pair.getName();
146 final Property property = getPropertyFromStore(name);
147 properties.put(name, property);
148 }
149
150 return new NativePropertyCollection(properties);
151 }
152 catch (final NamingException e)
153 {
154 throw new JndiPropertyStoreException(
155 JndiPropertyStoreMessageBean.collection(e, key));
156 }
157 }
158
159
160
161 @Override
162 public String toString()
163 {
164 return context.getSourceId();
165 }
166 }