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.spi.config.ds;
17  
18  import javax.annotation.concurrent.NotThreadSafe;
19  
20  import de.smartics.properties.api.config.domain.Property;
21  import de.smartics.properties.api.config.domain.PropertyCollection;
22  import de.smartics.properties.api.config.domain.PropertyLocation;
23  import de.smartics.properties.api.config.domain.PropertyProvider;
24  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
25  import de.smartics.util.lang.Arg;
26  
27  /**
28   * Provides an adapter for properties in a data source to match the
29   * {@link PropertyProvider} interface.
30   */
31  @NotThreadSafe
32  public final class DataSourceConfigurationPropertyProvider implements
33      PropertyProvider
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * The class version identifier.
41     */
42    private static final long serialVersionUID = 1L;
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The identifier of the configuration the provided properties are part of.
48     *
49     * @serial
50     */
51    private final ConfigurationKey<?> configurationKey;
52  
53    /**
54     * The access to the properties of the data source.
55     *
56     * @serial
57     */
58    private final PropertiesStore manager;
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Default constructor.
66     *
67     * @param configurationKey the identifier of the configuration the provided
68     *          properties are part of.
69     * @param manager the access to the properties of the data source.
70     * @throws NullPointerException if {@code configurationKey} or {@code manager}
71     *           is <code>null</code>.
72     */
73    public DataSourceConfigurationPropertyProvider(
74        final ConfigurationKey<?> configurationKey, final PropertiesStore manager)
75      throws NullPointerException
76    {
77      this.configurationKey =
78          Arg.checkNotNull("configurationKey", configurationKey);
79      this.manager = Arg.checkNotNull("manager", manager);
80    }
81  
82    // ****************************** Inner Classes *****************************
83  
84    // ********************************* Methods ********************************
85  
86    // --- init -----------------------------------------------------------------
87  
88    // --- get&set --------------------------------------------------------------
89  
90    @Override
91    public ConfigurationKey<?> getConfigurationKey()
92    {
93      return configurationKey;
94    }
95  
96    @Override
97    public PropertyLocation getSourceId()
98    {
99      return new PropertyLocation(manager.getDataSourceId());
100   }
101 
102   @Override
103   public boolean isLazy()
104   {
105     return true;
106   }
107 
108   // --- business -------------------------------------------------------------
109 
110   @Override
111   public Property getProperty(final String name) throws NullPointerException,
112     IllegalArgumentException
113   {
114     final String config = configurationKey.toString();
115     final Property property = manager.getProperty(config, name);
116     return property;
117   }
118 
119   @Override
120   public PropertyCollection getProperties()
121   {
122     final String config = configurationKey.toString();
123     return manager.getCollection(config);
124   }
125 
126   @Override
127   public boolean containsKey(final String name) throws NullPointerException
128   {
129     final String config = configurationKey.toString();
130     return manager.containsKey(config, name);
131   }
132 
133   // --- object basics --------------------------------------------------------
134 
135   /**
136    * Returns the string representation of the object.
137    *
138    * @return the string representation of the object.
139    */
140   @Override
141   public String toString()
142   {
143     final StringBuilder buffer = new StringBuilder();
144 
145     buffer.append(configurationKey).append(": ").append(manager);
146 
147     return buffer.toString();
148   }
149 }