1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.domain.key.envapp;
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.spi.config.domain.key.PropertyResourceMatchers;
37
38
39
40
41 public final class EnvAppDefinitionConfigParser extends
42 AbstractDefinitionConfigParser<EnvAppPropertiesDefinitionContext>
43 {
44
45
46
47
48
49
50
51
52
53 private static final Namespace NS = Namespace
54 .getNamespace("http://smartics.de/properties/definition/1");
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public EnvAppDefinitionConfigParser()
74 {
75 super(NS);
76 }
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 public EnvAppPropertiesDefinitionContext parse(final String systemId,
90 final InputStream input) throws ConfigException
91 {
92 try
93 {
94 final SAXBuilder parser = new SAXBuilder();
95 final Document document = parser.build(input, systemId);
96
97 checkNamespace(systemId, document);
98
99 final Element rootNode = document.getRootElement();
100 final Set<String> tlds = readSet(rootNode, "tlds", "tld", null);
101 final Set<String> environments =
102 readSet(rootNode, "environments", "environment");
103 final Set<String> nodes = readSet(rootNode, "nodes", "node");
104 final Set<String> groups = readSet(rootNode, "groups", "group");
105 final PropertyResourceMatchers files = readFiles(systemId, rootNode);
106
107 final EnvAppPropertiesDefinitionContext context;
108 if (tlds == null)
109 {
110 context =
111 new EnvAppPropertiesDefinitionContext(environments, nodes, groups,
112 files);
113 }
114 else
115 {
116 context =
117 new EnvAppPropertiesDefinitionContext(tlds, environments, nodes,
118 groups, files);
119 }
120 return context;
121 }
122 catch (final JDOMException e)
123 {
124 throw new ConfigException(systemId(ConfigCode.CONFIG_FILE_CANNOT_BE_READ,
125 e, systemId));
126 }
127 catch (final IOException e)
128 {
129 throw new ConfigException(systemId(ConfigCode.CONFIG_FILE_CANNOT_BE_READ,
130 e, systemId));
131 }
132 }
133
134 private static void checkNamespace(final String systemId,
135 final Document document)
136 {
137 final Namespace actualNamespace = document.getRootElement().getNamespace();
138 if (!NS.equals(actualNamespace))
139 {
140 throw new ConfigException(namespace(systemId, NS.getURI(),
141 actualNamespace.getURI()));
142 }
143 }
144
145 @Override
146 protected ConfigurationKey<?> readKey(final Element element)
147 {
148 final EnvironmentId environmentId = readEnvironment(element);
149 final ApplicationId applicationId = readApplication(element);
150 final ConfigurationKey<?> key =
151 new EnvAppConfigurationKey(environmentId, applicationId);
152 return key;
153 }
154 }