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.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   * Parses the <code>definition.xml</code> file for properties.
42   */
43  public final class TenantUserDefinitionConfigParser extends
44      AbstractDefinitionConfigParser<TenantUserPropertiesDefinitionContext>
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    // --- members --------------------------------------------------------------
51  
52    /**
53     * The namespace of definition files parsed by this parser.
54     */
55    private static final Namespace NS = Namespace
56        .getNamespace("http://smartics.de/properties/definition/rt/1");
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    // ****************************** Inner Classes *****************************
63  
64    // ********************************* Methods ********************************
65  
66    // --- init -----------------------------------------------------------------
67  
68    // --- get&set --------------------------------------------------------------
69  
70    // --- business -------------------------------------------------------------
71  
72    /**
73     * Default constructor.
74     */
75    public TenantUserDefinitionConfigParser()
76    {
77      super(NS);
78    }
79  
80    // --- object basics --------------------------------------------------------
81  
82    /**
83     * Parses the <code>definition.xml</code> from the given stream.
84     *
85     * @param systemId the identifier of the resource on the stream.
86     * @param input the <code>definition.xml</code> on the stream.
87     * @return the parsed context.
88     * @throws ConfigException on any problem reading from the stream.
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 }