View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
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 org.aludratest.service;
17  
18  import java.io.IOException;
19  
20  import org.aludratest.config.ComponentConfigurator;
21  import org.aludratest.config.Configurable;
22  import org.aludratest.config.MutablePreferences;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   * Abstract parent class for AludraTest service implementations which are configurable. Additionally to the common implementations
27   * provided by {@link AbstractAludraService}, this class provides helpers and default implementations dealing with configuration
28   * aspects.
29   * 
30   * @author falbrech
31   * 
32   */
33  public abstract class AbstractConfigurableAludraService extends AbstractAludraService implements Configurable {
34  
35      /**
36       * Returns the name of the properties resource containing the default values for this service. This resource is loaded using
37       * the class' ClassLoader and its <code>getResource()</code> method. The default implementation returns the value of
38       * {@link #getPropertiesBaseName()}, concatenated with the suffix <code>".properties.default"</code>. Subclasses can override.
39       * 
40       * @return The name of the properties resource containing the default values for this service, e.g.
41       *         <code>fileService.properties.default</code>. A value of <code>null</code> indicates that no default configuration
42       *         is available.
43       */
44      protected String getDefaultsResourceName() {
45          return getPropertiesBaseName() + ".properties.default";
46      }
47  
48      /**
49       * This default implementation asks {@link #getDefaultsResourceName()} for the resource to load from the ClassLoader and
50       * retrieve its contents as default configuration values.
51       * 
52       * @see org.aludratest.config.Configurable#fillDefaults(org.aludratest.config.MutablePreferences)
53       */
54      @Override
55      public void fillDefaults(MutablePreferences preferences) {
56          String resourceName = getDefaultsResourceName();
57          // the following line causes a false positive of Sonar. Subclasses could override getDefaultsResourceName() and return
58          // null.
59          if (resourceName == null) { // NOSONAR
60              return;
61          }
62          try {
63              ComponentConfigurator.fillPreferencesFromPropertiesResource(preferences, resourceName, getClass().getClassLoader());
64          }
65          catch (IOException e) {
66              LoggerFactory.getLogger(getClass()).error("Could not load default preferences from properties resource", e);
67          }
68      }
69  
70  }