View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
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.limmen.crs.client;
17  
18  import java.util.Date;
19  
20  import org.limmen.crs.Constants;
21  import org.limmen.crs.client.transport.HttpTransport;
22  import org.limmen.crs.client.transport.Transport;
23  import org.limmen.crs.exception.ConfigurationException;
24  import org.limmen.crs.model.Setting;
25  
26  /***
27   * This is the main class a program uses to retrieve it's configuration.
28   * The {@link ClientConfigurationUtil} is initialized using the
29   * {@link ClientConfigurationFactory} using a factory method.
30   * A {@link ClientConfigurationUtil} uses a configuration name (the name of the
31   * program) and a transport to retrieve the configuration through.
32   *
33   * @author Ivo Limmen
34   * @see ClientConfigurationFactory#createClientConfiguration(String, Transport)
35   * @see HttpTransport
36   */
37  public class ClientConfigurationUtil {
38  
39  	private static ClientConfigurationUtil current;
40  
41  	/***
42  	 * Returns the boolean value for the requested setting.
43  	 *
44  	 * @param aSection section for the setting to retrieve
45  	 * @param aSetting setting to retrieve
46  	 * @throws ConfigurationException
47  	 * 		when the ClientConfiguration has not been initialised or when the
48  	 * 		setting is not of the requested type
49  	 */
50  	public static boolean getBooleanSetting(String aSection, String aSetting) throws ConfigurationException {
51  
52  		return getCurrent().getSetting(aSection, aSetting).getBooleanValue();
53     }
54  
55  	/***
56  	 * Returns the current instance of the ClientConfiguration is properly
57  	 * configured.
58  	 * @throws ConfigurationException if the {@link ClientConfigurationFactory}
59  	 * 			has not been called yet
60  	 */
61     private static ClientConfigurationUtil getCurrent() throws ConfigurationException {
62  
63     	if (current == null) {
64  
65     		throw new ConfigurationException("ClientConfiguration is not initialized yet!");
66     	}
67  
68     	return current;
69     }
70  
71  	/***
72  	 * Returns the date value for the requested setting.
73  	 *
74  	 * @param aSection section for the setting to retrieve
75  	 * @param aSetting setting to retrieve
76  	 * @throws ConfigurationException
77  	 * 		when the ClientConfiguration has not been initialised or when the
78  	 * 		setting is not of the requested type
79  	 */
80  	public static Date getDateSetting(String aSection, String aSetting) throws ConfigurationException {
81  
82  		return getCurrent().getSetting(aSection, aSetting).getDateValue();
83     }
84  
85  	/***
86  	 * Returns the double value for the requested setting.
87  	 *
88  	 * @param aSection section for the setting to retrieve
89  	 * @param aSetting setting to retrieve
90  	 * @throws ConfigurationException
91  	 * 		when the ClientConfiguration has not been initialised or when the
92  	 * 		setting is not of the requested type
93  	 */
94  	public static Double getDoubleSetting(String aSection, String aSetting) throws ConfigurationException {
95  
96  		return getCurrent().getSetting(aSection, aSetting).getDoubleValue();
97     }
98  
99  	/***
100 	 * Returns the integer value for the requested setting.
101 	 *
102 	 * @param aSection section for the setting to retrieve
103 	 * @param aSetting setting to retrieve
104 	 * @throws ConfigurationException
105 	 * 		when the ClientConfiguration has not been initialised or when the
106 	 * 		setting is not of the requested type
107 	 */
108 	public static int getIntSetting(String aSection, String aSetting) throws ConfigurationException {
109 
110 		return getCurrent().getSetting(aSection, aSetting).getIntegerValue();
111    }
112 
113 	/***
114 	 * Returns a multistring value for the requested setting.
115 	 *
116 	 * @param aSection section for the setting to retrieve
117 	 * @param aSetting setting to retrieve
118 	 * @throws ConfigurationException
119 	 * 		when the ClientConfiguration has not been initialised or when the
120 	 * 		setting is not of the requested type
121 	 */
122 	public static String[] getMultiStringSetting(String aSection, String aSetting) throws ConfigurationException {
123 
124 		return getCurrent().getSetting(aSection, aSetting).getMultiStringValue();
125    }
126 
127 	/***
128 	 * Returns a string value for the requested setting.
129 	 *
130 	 * @param aSection section for the setting to retrieve
131 	 * @param aSetting setting to retrieve
132 	 * @throws ConfigurationException
133 	 * 		when the ClientConfiguration has not been initialised or when the
134 	 * 		setting is not of the requested type
135 	 */
136 	public static String getStringSetting(String aSection, String aSetting) throws ConfigurationException {
137 
138 		return getCurrent().getSetting(aSection, aSetting).getStringValue();
139    }
140 
141 	private final String configurationName;
142 
143    private final Transport transport;
144 
145    ClientConfigurationUtil(Transport transport) {
146 
147    	super();
148 
149    	this.configurationName = System.getProperty(Constants.CONFIGURATION_NAME_PROPERTY);
150 
151    	if (this.configurationName == null) {
152 
153    		throw new IllegalStateException(
154    				String.format(
155    						"Property '%0$s' has not been set!",
156    						Constants.CONFIGURATION_NAME_PROPERTY));
157    	}
158 
159    	this.transport = transport;
160    	current = this;
161    }
162 
163    private Setting getSetting(String aSection, String aSetting) throws ConfigurationException {
164 
165 		return transport.getSetting(configurationName, aSection, aSetting);
166 	}
167 }