View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Parses the <code>definition.xml</code> file for properties.
40   */
41  public final class EnvAppDefinitionConfigParser extends
42      AbstractDefinitionConfigParser<EnvAppPropertiesDefinitionContext>
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    // --- members --------------------------------------------------------------
49  
50    /**
51     * The namespace of definition files parsed by this parser.
52     */
53    private static final Namespace NS = Namespace
54        .getNamespace("http://smartics.de/properties/definition/1");
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    // ****************************** Inner Classes *****************************
61  
62    // ********************************* Methods ********************************
63  
64    // --- init -----------------------------------------------------------------
65  
66    // --- get&set --------------------------------------------------------------
67  
68    // --- business -------------------------------------------------------------
69  
70    /**
71     * Default constructor.
72     */
73    public EnvAppDefinitionConfigParser()
74    {
75      super(NS);
76    }
77  
78    // --- object basics --------------------------------------------------------
79  
80    /**
81     * Parses the <code>definition.xml</code> from the given stream.
82     *
83     * @param systemId the identifier of the resource on the stream.
84     * @param input the <code>definition.xml</code> on the stream.
85     * @return the parsed context.
86     * @throws ConfigException on any problem reading from the stream.
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 }