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.io.IOException;
19 import java.io.ObjectInputStream;
20
21 import javax.annotation.concurrent.ThreadSafe;
22 import javax.naming.Context;
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
25
26 import de.smartics.properties.api.config.domain.Property;
27 import de.smartics.properties.api.config.domain.PropertyProvider;
28 import de.smartics.properties.api.config.domain.PropertyStoreAccessor;
29 import de.smartics.properties.api.config.domain.SerializableConfigurationPropertiesManagement;
30 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
31 import de.smartics.properties.api.core.app.JndiContext;
32 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
33 import de.smartics.properties.api.core.security.PropertyValueSecurity;
34 import de.smartics.properties.spi.config.support.AbstractExternalConfigurationPropertiesManagement;
35 import de.smartics.properties.spi.config.support.SerializableConfigurationPropertiesManagementSpi;
36 import de.smartics.util.lang.NullArgumentException;
37
38
39
40
41 @ThreadSafe
42 public final class JndiConfigurationProperties extends
43 AbstractExternalConfigurationPropertiesManagement implements
44 SerializableConfigurationPropertiesManagement,
45 SerializableConfigurationPropertiesManagementSpi
46 {
47
48
49
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56
57
58
59
60
61 private transient JndiContext context;
62
63
64
65
66
67
68 private transient PropertyStoreAccessor propertyStoreAccessor;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public JndiConfigurationProperties(final ConfigurationKey<?> key,
86 final PropertyDescriptorRegistry registry,
87 final PropertyValueSecurity decrypter) throws NullArgumentException,
88 NamingException, IllegalArgumentException
89 {
90 super(key, registry, decrypter);
91
92 this.context = initContext(key);
93 this.propertyStoreAccessor = new JndiPropertyStoreAccessor(key, context);
94 }
95
96
97
98
99
100
101
102 private static JndiContext initContext(final ConfigurationKey<?> key)
103 throws NamingException, IllegalArgumentException
104 {
105 final Context jndi = new InitialContext();
106 final JndiContext context = JndiContext.config(jndi, key.toString());
107 return context;
108 }
109
110
111
112 @Override
113 public PropertyStoreAccessor getPropertyStoreAccessor()
114 {
115 return propertyStoreAccessor;
116 }
117
118
119
120 @Override
121 protected void setPropertiesToStore(final PropertyProvider provider)
122 throws JndiPropertyStoreException
123 {
124 String name = "<unknown>";
125 try
126 {
127 for (final Property property : provider.getProperties())
128 {
129 name = property.getName();
130 final String value = property.getValue();
131 context.set(name, value);
132 }
133 }
134 catch (final NamingException e)
135 {
136 throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
137 JndiPropertyStoreCode.CANNOT_SET_PROPERTY, e, getKey(), name));
138 }
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public SerializableConfigurationPropertiesManagement toSerializable()
156 {
157 return this;
158 }
159
160
161
162
163
164
165
166
167
168
169
170 private void readObject(final ObjectInputStream in) throws IOException,
171 ClassNotFoundException
172 {
173 in.defaultReadObject();
174
175 try
176 {
177 this.context = initContext(getKey());
178 this.propertyStoreAccessor =
179 new JndiPropertyStoreAccessor(getKey(), context);
180 }
181 catch (final NamingException e)
182 {
183 throw new IOException("Cannot reestablish the JNDI context.", e);
184 }
185 catch (final IllegalArgumentException e)
186 {
187 throw new IOException("Cannot reestablish the JNDI context.", e);
188 }
189 }
190 }