View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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   *****************************************************************************/
17  package org.settings4j;
18  
19  import java.util.List;
20  import java.util.Map;
21  
22  
23  /**
24   * SettingsInstance is used to get simply access to Application settings.
25   * 
26   * @see Settings4j
27   * @author Harald.Brabenetz
28   */
29  public interface Settings4jInstance {
30  
31      /**
32       * return the found String-Value for the given key.<br />
33       * The {@link Settings4jInstance} Instance iterates all his {@link Connector}s and return the first found Value.
34       * <p>
35       * Returns null if no connector found a Value for the given key<br />
36       * 
37       * @param key the Key for the configuration-property. e.g.: "com/mycompany/myapp/myParameterKey"
38       * @return the found String-Value for the given key
39       */
40      String getString(String key);
41  
42      /**
43       * return the found byte[]-Value for the given key.<br />
44       * The {@link Settings4jInstance} Instance iterates all his {@link Connector}s and return the first found Value.
45       * <p>
46       * Returns null if no connector found a Value for the given key<br />
47       * 
48       * @param key the Key for the configuration-property. e.g.: "com/mycompany/myapp/myParameterKey"
49       * @return the found byte[]-Value for the given key
50       */
51      byte[] getContent(String key);
52  
53      /**
54       * return the found Object-Value for the given key.<br />
55       * The {@link Settings4jInstance} iterates all his {@link Connector}s and return the first found Value. <br />
56       * Returns null if no connector found a Value for the given key<br />
57       * 
58       * @param key the Key for the configuration-property. e.g.: "com/mycompany/myapp/myParameterKey"
59       * @return the found Object-Value for the given key
60       */
61      Object getObject(String key);
62  
63      /**
64       * Add a {@link Connector}.<br />
65       * This method will be call, if you create a connector-ref to a connector configuration in your settings4j.xml
66       * 
67       * <pre>
68       * Example configuration in settings4j.xml:
69       * --------------------------------------
70       * &lt;settings name="com.mycompany" &gt;
71       *     &lt;connector-ref name="DBConnector" /&gt;
72       * &lt;/settings&gt;
73       * --------------------------------------
74       * </pre>
75       * 
76       * @param connector The connector to add.
77       */
78      void addConnector(Connector connector);
79  
80      /**
81       * Add a custom {@link Connector} to the right position in relation to the other connectors.<br />
82       * This method can be used to add custom connectors into the {@link Settings4jInstance}
83       * <p>
84       * <h4>Example usage 1:</h4>
85       * 
86       * <pre>
87       * --------------------------------------
88       * Connector myConnector = ...
89       * addConnector(myConnector, {@link ConnectorPositions}.afterLast(SystemPropertyConnector.class));
90       * --------------------------------------
91       * </pre>
92       * 
93       * <h4>Example usage 2:</h4> <br />
94       * <code>{@link ConnectorPositions}.afterLast(SystemPropertyConnector.class)</code> will throw an exception if the
95       * {@link Settings4jInstance} doesn't have a SystemPropertyConnector. <br />
96       * To prevent an Exception you can provide multiple ConnectorPositions as fallback:
97       * 
98       * <pre>
99       * --------------------------------------
100      * Connector myConnector = ...
101      * addConnector(myConnector, ConnectorPositions.firstValid(//
102      *     ConnectorPositions.afterLast(SystemPropertyConnector.class),
103      *     ConnectorPositions.atFirst() // fallback if no SystemPropertyConnector exists.
104      *   )
105      * );
106      * --------------------------------------
107      * </pre>
108      * 
109      * <h4>Example usage 3:</h4> <br />
110      * {@link #addConnector(Connector)} will throw an exception if a connector with the same name already exists. <br />
111      * To prevent this Exception you should also check with {@link #getConnector(String)} if the Connector already
112      * exists:
113      * 
114      * <pre>
115      * --------------------------------------
116      * Connector myConnector = ...
117      * if(getConnector(myConnector.getName()) == null) {
118      *   addConnector(myConnector, ConnectorPositions.firstValid(//
119      *       ConnectorPositions.afterLast(SystemPropertyConnector.class),
120      *       ConnectorPositions.atFirst() // fallback if no SystemPropertyConnector exists.
121      *     )
122      *   );
123      * }
124      * --------------------------------------
125      * </pre>
126      * 
127      * @param connector The connector to add.
128      * @param position The position where the connector should be added.
129      */
130     void addConnector(Connector connector, ConnectorPosition position);
131 
132     /**
133      * Remove all Settings. (Internal use only)
134      */
135     void removeAllConnectors();
136 
137     /**
138      * The key mapping defined in settings4j.xml.
139      * <p>
140      * if some Sub-Modules of your App defines separated Keys for e.g. the DataSource, you can refer it to one unique
141      * Key:
142      * 
143      * <pre>
144      * Example:
145      * &lt;mapping name="defaultMapping"&gt;
146      *     &lt;entry key="com/mycompany/moduleX/datasource" ref-key="global/datasource"/&gt;
147      *     &lt;entry key="com/mycompany/moduleY/datasource" ref-key="global/datasource"/&gt;
148      * &lt;/mapping&gt;
149      * </pre>
150      * 
151      * Settings4j.getXXX("com/mycompany/moduleX/datasource"); <br />
152      * should return the configured value under "global/datasource" <br />
153      * <br />
154      * 
155      * @return the Mappings of this Settings-Object (without inheritances)
156      */
157     Map getMapping();
158 
159     /**
160      * Set the mapping for this Settings-Object without inheritance.<br />
161      * This method will be call, if you create a mapping-ref to a mapping configuration in your settings4j.xml
162      * 
163      * <pre>
164      * Example:
165      * &lt;root mapping-ref="<b>defaultMapping</b>" &gt;
166      *    ...
167      * &lt;/root&gt;
168      * 
169      * &lt;mapping name="<b>defaultMapping</b>"&gt;
170      *     &lt;entry key="com/mycompany/moduleX/datasource" ref-key="global/datasource"/&gt;
171      *     &lt;entry key="com/mycompany/moduleY/datasource" ref-key="global/datasource"/&gt;
172      * &lt;/mapping&gt;
173      * </pre>
174      * 
175      * @param mapping The Mapping between available settings to used settings.
176      */
177     void setMapping(Map mapping);
178 
179     /**
180      * Return a List off {@link Connector} who can be used with this {@link Settings4jInstance} instance.
181      * 
182      * @return a list off all Connectors who can be used with this {@link Settings4jInstance} instance
183      */
184     List<Connector> getConnectors();
185 
186     /**
187      * Return the {@link Connector} for the given Name.
188      * 
189      * @param connectorName The Connector Name.
190      * @return The {@link Connector} for the given Name.
191      */
192     Connector getConnector(String connectorName);
193 }