View Javadoc

1   /*
2    * Copyright 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.admin.domain.service;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory;
25  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
26  import de.smartics.properties.api.config.domain.ConfigurationValidationException;
27  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
28  import de.smartics.properties.impl.config.ds.DataSourceConfiguration;
29  import de.smartics.properties.impl.config.resource.AutodetectDataSourceResourceConfigurationPropertiesFactory;
30  import de.smartics.properties.resource.domain.ArtifactId;
31  import de.smartics.properties.spi.config.support.ConfigurationPropertiesManagementFactory;
32  import de.smartics.util.lang.Arg;
33  
34  /**
35   * Application service to access configurations.
36   */
37  public class ConfigurationService implements Serializable
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    /**
44     * The class version identifier.
45     * <p>
46     * The value of this constant is {@value}.
47     * </p>
48     */
49    private static final long serialVersionUID = 1L;
50  
51    /**
52     * Reference to the logger for this class.
53     */
54    private static final Logger LOG = LoggerFactory
55        .getLogger(ConfigurationService.class);
56  
57    // --- members --------------------------------------------------------------
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    /**
74     * Creates a factory to create properties configurations.
75     *
76     * @param applicationId the identifier of the application the factory is
77     *          created for. The GAV coordinates identify an artifact that
78     *          references the resources to parse for property declarations and
79     *          definitions.
80     * @param dataSourceConfig the configuration to access property definitions in
81     *          a data source. May be <code>null</code> in which case the
82     *          configuration in {@code META-INF/services} is used. Otherwise a
83     *          datasource capable implementation will be instantiated regardless
84     *          of the configuration in {@code META-INF/services}.
85     * @return the factory to create property configurations.
86     * @throws NullPointerException if {@code applicationId} is <code>null</code>.
87     */
88    public RemoteConfigurationPropertiesManagementFactory createFactory(
89        final ArtifactId applicationId,
90        final DataSourceConfiguration dataSourceConfig)
91      throws NullPointerException
92    {
93      Arg.checkNotNull("applicationId", applicationId);
94  
95      final ConfigurationPropertiesManagementFactory factory;
96      if (dataSourceConfig != null)
97      {
98        factory =
99            new AutodetectDataSourceResourceConfigurationPropertiesFactory(
100               dataSourceConfig);
101     }
102     else
103     {
104       factory =
105           (ConfigurationPropertiesManagementFactory) ConfigurationPropertiesFactoryFactory
106               .createDefaultFactory();
107     }
108     final String remoteUrl = factory.addRootUrls(applicationId);
109 
110     final Collection<ConfigurationKey<?>> keys = factory.materialize();
111     logKeysFound(keys);
112 
113     return new RemoteConfigurationPropertiesManagementFactory(factory,
114         remoteUrl);
115   }
116 
117   private static void logKeysFound(final Collection<ConfigurationKey<?>> keys)
118   {
119     if (LOG.isDebugEnabled())
120     {
121       final StringBuilder buffer = new StringBuilder();
122 
123       for (final ConfigurationKey<?> key : keys)
124       {
125         buffer.append(' ').append(key);
126       }
127 
128       LOG.debug("Found the following configurations:{}", buffer.toString());
129     }
130   }
131 
132   /**
133    * Validates the configuration specified by its key.
134    *
135    * @param factory the factory to fetch the configuration by its {@code key}.
136    * @param key the key to the configuration to be validated.
137    * @param lenient flag to indicate whether (<code>true</code>) or not (
138    *          <code>false</code>) the validation should be conducted lenient.
139    * @throws NullPointerException if {@code factory} or {@code key} is
140    *           <code>null</code>.
141    * @throws ConfigurationValidationException if the configuration is invalid.
142    */
143   public void validate(final ConfigurationPropertiesManagementFactory factory,
144       final ConfigurationKey<?> key, final boolean lenient)
145     throws NullPointerException, ConfigurationValidationException
146   {
147     final ConfigurationPropertiesManagement config =
148         Arg.checkNotNull("factory", factory).create(key);
149     config.validate(lenient);
150   }
151 
152   // --- object basics --------------------------------------------------------
153 
154 }