View Javadoc
1   /**
2    * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3    * 
4    * This program is free software; you can redistribute
5    * it and/or modify it under the terms of the GNU General
6    * Public License version 2 as published by the Free Software
7    * Foundation.
8    *
9    * This program is distributed in the hope that it will be
10   * useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   * PURPOSE.  See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public
16   * License along with this program; if not, write to the
17   * Free Software Foundation, Inc., 59 Temple Place,
18   * Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.surethingies.properties;
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.SortedSet;
30  
31  import de.surethingies.properties.io.ConfigurationIO;
32  import de.surethingies.properties.plain.Configuration;
33  import de.surethingies.properties.plain.Entry;
34  import de.surethingies.properties.plain.Setting;
35  
36  public class ParameterFactory {
37  
38      private static final String VERSION = "1.0";
39  
40      private File currentFile;
41  
42      private Parameters parameters;
43  
44      // Singleton
45      private static ParameterFactory instance;
46  
47      private ParameterFactory() {
48          parameters = new Parameters();
49      }
50  
51      /**
52       * @return an instance of this Factory
53       */
54      public static synchronized ParameterFactory instance() {
55          if (instance == null) {
56              instance = new ParameterFactory();
57          }
58  
59          return instance;
60      }
61  
62      public static void init(File propertyFile) {
63          instance().currentFile = propertyFile;
64      }
65  
66      public static synchronized void reset() {
67          instance().currentFile = null;
68          instance().parameters.reset();
69      }
70  
71      /**
72       * Get a Parameter identified
73       * 
74       * @param group by the Settings group
75       * @return
76       */
77      public List<TypedParameter> get(String group) {
78          List<TypedParameter> result = new ArrayList<TypedParameter>();
79  
80          for (TypedParameter typedParam : parameters.getTypedParameters()) {
81              if (typedParam.getGroup().equals(group)) {
82                  result.add(typedParam);
83              }
84          }
85  
86          return result;
87      }
88  
89      /**
90       * Get a Parameter identified
91       * 
92       * @param group by the Settings group
93       * @param identifier and a unique Identifier
94       * @return
95       */
96      public List<TypedParameter> get(String group, String identifier) {
97          List<TypedParameter> result = new ArrayList<TypedParameter>();
98  
99          for (TypedParameter typedParam : parameters.getTypedParameters()) {
100             if (typedParam.getGroup().equals(group) && typedParam.getIdentifier().equals(identifier)) {
101                 result.add(typedParam);
102             }
103         }
104 
105         return result;
106     }
107 
108     public List<TypedParameter> get(Parameter parameter) {
109         List<TypedParameter> result = new ArrayList<TypedParameter>();
110 
111         for (TypedParameter typedParam : parameters.getTypedParameters()) {
112             if (typedParam.getParameter().equals(parameter)) {
113                 result.add(typedParam);
114             }
115         }
116 
117         return result;
118     }
119 
120     public TypedParameter get(Parameter parameter, String identifier) {
121         TypedParameter result = null;
122 
123         for (TypedParameter typedParam : parameters.getTypedParameters()) {
124             if (typedParam.getParameter().equals(parameter) 
125                 && typedParam.getIdentifier().equals(identifier)) {
126                 result = typedParam;
127                 break;
128             }
129         }
130 
131         return result;
132     }
133 
134     /**
135      * @return get a List of the managed Groups
136      */
137     public List<String> getGroups() {
138         List<String> result = new ArrayList<String>();
139         for (TypedParameter typedParam : parameters.getTypedParameters()) {
140             if (!result.contains(typedParam.getGroup())) {
141                 result.add(typedParam.getGroup());
142             }
143         }
144 
145         return result;
146     }
147 
148     /**
149      * Add a Parameter which could be used to translate Settings in
150      * TypedParameters
151      * 
152      * @param group The Property Group
153      * @param params
154      */
155     public static void addDefaultParameters(String group, SortedSet<Parameter> params) {
156         instance().parameters.addDefaultParameters(group, params);
157     }
158 
159     /**
160      * Add a TypedParameters
161      * 
162      * @param parameter
163      */
164     public void addParameter(TypedParameter parameter) {
165         parameters.getTypedParameters().add(parameter);
166     }
167 
168     /**
169      * Add a List of TypedParameters
170      * 
171      * @param typedParameters
172      */
173     public void addParameters(Set<TypedParameter> typedParameters) {
174         parameters.getTypedParameters().addAll(typedParameters);
175     }
176 
177     /**
178      * Load all Properties from File properties into this Factory
179      * 
180      * @param propertyFile the File to load the Properties from
181      * @throws Exception
182      */
183     public void loadProperties() throws Exception {
184         if (currentFile == null) {
185             throw new ParameterException("Filename not set");
186         }
187 
188         loadProperties(currentFile);
189     }
190 
191     /**
192      * Load all Properties from File properties into this Factory
193      * 
194      * @param propertyFile the File to load the Properties from
195      * @throws Exception
196      */
197     public void loadProperties(File propertyFile) throws Exception {
198         currentFile = propertyFile;
199         if (!currentFile.exists() || propertyFile.length() <= 0) {
200             saveProperties(propertyFile);
201         }
202      
203         Configuration conf = ConfigurationIO.instance().load(propertyFile);
204 
205         for (Setting settings: conf.settings) {
206             parameters.setParametersFromSettings(settings);
207         }
208     }
209 
210     public void saveProperties() throws Exception {
211         if (currentFile == null) {
212             throw new ParameterException("Filename not set");
213         }
214 
215         saveProperties(currentFile);
216     }
217 
218     /**
219      * Safe the current Properties to the given File
220      * 
221      * @param propertyFile the File to safe the Properties to
222      * @throws Exception Errors while safing
223      */
224     public void saveProperties(File propertyFile) throws Exception {
225         currentFile = propertyFile;
226 
227         Configuration conf = new Configuration();
228         conf.version = VERSION;
229         conf.settings = getSettings();
230 
231         ConfigurationIO.instance().store(conf, propertyFile);
232     }
233 
234     protected Parameters parameters() {
235         return parameters;
236     }
237 
238     private Collection<Setting> getSettings() {
239         Map<String, Setting> tmpStore = new HashMap<String, Setting>();
240 
241         for (TypedParameter param : parameters.getTypedParameters()) {
242             String key = param.getGroup() + "#" + param.getIdentifier();
243             Entry entry = new Entry();
244             entry.key = param.getParameter().getIdentifier();
245             if (param.getValue() == null) {
246                 entry.value = param.getParameter().getDefaultValue();
247             } else {
248                 entry.value = param.getValue();
249             }
250 
251             // Ensure tmpStore has Settings
252             Setting settings;
253             if (tmpStore.containsKey(key)) {
254                 settings = tmpStore.get(key);
255             } else {
256                 settings = new Setting();
257                 settings.group = param.getGroup();
258                 settings.id = param.getIdentifier();
259                 settings.entries = new ArrayList<Entry>();
260 
261                 tmpStore.put(key, settings);
262             }
263 
264             settings.entries.add(entry);
265         }
266 
267         return tmpStore.values();
268     }
269 }