1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.domain.key.rtaware;
17
18 import static de.smartics.properties.api.core.domain.ConfigMessageBean.namespace;
19 import static de.smartics.properties.api.core.domain.ConfigMessageBean.systemId;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Set;
24
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.JDOMException;
28 import org.jdom.Namespace;
29 import org.jdom.input.SAXBuilder;
30
31 import de.smartics.properties.api.config.domain.key.ApplicationId;
32 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
33 import de.smartics.properties.api.config.domain.key.EnvironmentId;
34 import de.smartics.properties.api.core.domain.ConfigCode;
35 import de.smartics.properties.api.core.domain.ConfigException;
36 import de.smartics.properties.impl.config.domain.key.envapp.AbstractDefinitionConfigParser;
37 import de.smartics.properties.spi.config.domain.key.PropertyResourceMatchers;
38 import de.smartics.util.lang.NullArgumentException;
39
40
41
42
43 public final class TenantUserDefinitionConfigParser extends
44 AbstractDefinitionConfigParser<TenantUserPropertiesDefinitionContext>
45 {
46
47
48
49
50
51
52
53
54
55 private static final Namespace NS = Namespace
56 .getNamespace("http://smartics.de/properties/definition/rt/1");
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public TenantUserDefinitionConfigParser()
76 {
77 super(NS);
78 }
79
80
81
82
83
84
85
86
87
88
89
90 @Override
91 public TenantUserPropertiesDefinitionContext parse(final String systemId,
92 final InputStream input) throws ConfigException
93 {
94 try
95 {
96 final SAXBuilder parser = new SAXBuilder();
97 final Document document = parser.build(input, systemId);
98
99 checkNamespace(systemId, document);
100
101 final Element rootNode = document.getRootElement();
102 final Set<String> tlds = readSet(rootNode, "tlds", "tld", null);
103 final Set<String> environments =
104 readSet(rootNode, "environments", "environment");
105 final Set<String> nodes = readSet(rootNode, "nodes", "node");
106 final Set<String> groups = readSet(rootNode, "groups", "group");
107 final PropertyResourceMatchers files = readFiles(systemId, rootNode);
108
109 final TenantUserPropertiesDefinitionContext context;
110 if (tlds == null)
111 {
112 context =
113 new TenantUserPropertiesDefinitionContext(environments, nodes,
114 groups, files);
115 }
116 else
117 {
118 context =
119 new TenantUserPropertiesDefinitionContext(tlds, environments,
120 nodes, groups, files);
121 }
122 return context;
123 }
124 catch (final JDOMException e)
125 {
126 throw new ConfigException(systemId(ConfigCode.CONFIG_FILE_CANNOT_BE_READ,
127 e, systemId));
128 }
129 catch (final IOException e)
130 {
131 throw new ConfigException(systemId(ConfigCode.CONFIG_FILE_CANNOT_BE_READ,
132 e, systemId));
133 }
134 }
135
136 private static void checkNamespace(final String systemId,
137 final Document document)
138 {
139 final Namespace actualNamespace = document.getRootElement().getNamespace();
140 if (!NS.equals(actualNamespace))
141 {
142 throw new ConfigException(namespace(systemId, NS.getURI(),
143 actualNamespace.getURI()));
144 }
145 }
146
147 @Override
148 protected ConfigurationKey<?> readKey(final Element element)
149 throws NullArgumentException
150 {
151 final EnvironmentId environmentId = readEnvironment(element);
152 final ApplicationId applicationId = readApplication(element);
153 final TenantId tenantId = readTenant(element);
154 final UserId userId = readUser(element);
155
156 final ConfigurationKey<?> key =
157 new TenantUserConfigurationKey(environmentId, applicationId, tenantId,
158 userId);
159 return key;
160 }
161
162 private TenantId readTenant(final Element element)
163 {
164 final Element tenant = element.getChild("tenant", namespace);
165
166 final TenantId tenantId;
167 if (tenant != null)
168 {
169 final String name = norm(tenant.getChildTextNormalize("name", namespace));
170
171 tenantId = new TenantId(name);
172 }
173 else
174 {
175 tenantId = TenantId.ANY_TENANT;
176 }
177 return tenantId;
178 }
179
180 private UserId readUser(final Element element)
181 {
182 final Element user = element.getChild("user", namespace);
183
184 final UserId userId;
185 if (user != null)
186 {
187 final String name = norm(user.getChildTextNormalize("name", namespace));
188
189 userId = new UserId(name);
190 }
191 else
192 {
193 userId = UserId.ANY_USER;
194 }
195 return userId;
196 }
197 }