1 /*
2 * #%L
3 * settings4j
4 * ===============================================================
5 * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6 * ===============================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
19 */
20 package org.settings4j.helper.configuration;
21
22 import org.apache.commons.configuration.Configuration;
23 import org.settings4j.connector.AbstractPropertyConnector;
24
25
26 /**
27 * Adapter to use an <a href="http://commons.apache.org/proper/commons-configuration/">Apache Commons Configuration</a> as Settings4j connector.
28 * <h3>Example Usage</h3>
29 * <p>
30 * Create a {@link org.apache.commons.configuration.XMLConfiguration} instance and add it to the Settings4j instance as Connector.
31 * </p>
32 *
33 * <pre>
34 * String connectorName = "myCommonsConfigXmlConfigConnector";
35 * Connector connector = Settings4j.getSettings().getConnector(connectorName);
36 * if (connector == null) {
37 * XMLConfiguration configuration = new XMLConfiguration(new File(.....));
38 *
39 * connector = new ConfigurationToConnectorAdapter(connectorName, configuration);
40 *
41 * // add the connecter after the last SystemPropertyConnector or add it as first connector.
42 * Settings4j.getSettings().addConnector(connector, //
43 * ConnectorPositions.firstValid(//
44 * ConnectorPositions.afterLast(SystemPropertyConnector.class), //
45 * ConnectorPositions.atFirst() // if no SystemPropertyConnector is configured.
46 * )//
47 * );
48 * }
49 * </pre>
50 *
51 * @author brabenetz
52 */
53 public class ConfigurationToConnectorAdapter extends AbstractPropertyConnector {
54
55 private final Configuration configuration;
56
57 /**
58 * @param name The unique name of this connector.
59 * @param configuration The apache commons configuration instance to wrap and use as Settings4j connector.
60 */
61 public ConfigurationToConnectorAdapter(final String name, final Configuration configuration) {
62 super();
63 this.configuration = configuration;
64 this.setName(name);
65 }
66
67 @Override
68 public String getString(final String key) {
69 return this.configuration.getString(key);
70 }
71
72 public Configuration getConfiguration() {
73 return this.configuration;
74 }
75
76 }