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.config.transfer.jdbc;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import de.smartics.properties.api.config.domain.PropertyLocation;
22  import de.smartics.properties.api.config.domain.PropertyProvider;
23  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
24  import de.smartics.properties.api.config.transfer.PropertySink;
25  import de.smartics.properties.api.config.transfer.TransferException;
26  import de.smartics.properties.spi.config.ds.DataSourceException;
27  import de.smartics.properties.spi.config.ds.PropertiesStore;
28  
29  /**
30   * Writes properties to a database via JDBC.
31   * <p>
32   * Properties are <strong>not</strong> stored within a transaction.
33   * </p>
34   */
35  public final class JdbcPropertySink implements PropertySink
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * Reference to the logger for this class.
43     */
44    private static final Logger LOG = LoggerFactory
45        .getLogger(JdbcPropertySink.class);
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The store of the properties.
51     */
52    private final PropertiesStore store;
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Constructors ******************************
57  
58    /**
59     * Default constructor.
60     *
61     * @param store the store of the properties.
62     */
63    public JdbcPropertySink(final PropertiesStore store)
64    {
65      this.store = store;
66    }
67  
68    // ****************************** Inner Classes *****************************
69  
70    // ********************************* Methods ********************************
71  
72    // --- init -----------------------------------------------------------------
73  
74    // --- get&set --------------------------------------------------------------
75  
76    // --- business -------------------------------------------------------------
77  
78    @Override
79    public void clear() throws TransferException, DataSourceException
80    {
81      store.deleteProperties();
82    }
83  
84    @Override
85    public void write(final PropertyProvider provider) throws TransferException
86    {
87      final PropertyLocation location = provider.getSourceId();
88      final ConfigurationKey<?> key = provider.getConfigurationKey();
89  
90      LOG.info("Writing configuration '{}' properties: {}", key, location);
91      store.setProperties(provider);
92    }
93  
94    @Override
95    public void write(final Iterable<PropertyProvider> providers)
96      throws TransferException
97    {
98      for (final PropertyProvider provider : providers)
99      {
100       write(provider);
101     }
102   }
103 
104   /**
105    * {@inheritDoc}
106    * <p>
107    * This is a no-op.
108    * </p>
109    */
110   @Override
111   public void close() throws TransferException
112   {
113   }
114 
115   // --- object basics --------------------------------------------------------
116 
117 }