Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConfigurationManagerFactory |
|
| 1.6666666666666667;1.667 |
1 | package org.naftulin.configmgr; | |
2 | ||
3 | /** | |
4 | * A factory that creates and maintains configuration management engine and configuration manager. | |
5 | * | |
6 | * @author Henry Naftulin | |
7 | * @since 1.0 | |
8 | */ | |
9 | 5 | public class ConfigurationManagerFactory { |
10 | 5 | private static String configurationFile = "config.xml"; |
11 | private static ConfigurationManagement management; | |
12 | private static ConfigurationManager manager; | |
13 | ||
14 | /** | |
15 | * Package private constructor, needed to access clear method. Only used | |
16 | * for unit testing. | |
17 | */ | |
18 | 0 | ConfigurationManagerFactory() {} |
19 | ||
20 | public static synchronized boolean isIniialized() { | |
21 | 15 | return management != null; |
22 | } | |
23 | /** | |
24 | * Sets a file from which master configuration would be read. | |
25 | * By default, the file is 'config.xml', but it could be owervritten by using this API. | |
26 | * Only valid before configuration management is initialized, otherwise throws an exception. | |
27 | * @param configFile the configuration file for master record. | |
28 | * @throws ConfigurationManagerException if a configuration management engine is already initiazed. | |
29 | */ | |
30 | public static synchronized void setConfigurationFile(final String configFile) throws ConfigurationManagerException { | |
31 | 80 | if (management != null) { |
32 | 0 | throw new ConfigurationManagerException("Configuration Manager already initialize"); |
33 | } | |
34 | 80 | configurationFile = configFile; |
35 | 80 | } |
36 | ||
37 | /** | |
38 | * Returns configuration management engine object. If the object was not created yet, | |
39 | * this call will create it. | |
40 | * @return configuration management engine object. | |
41 | * @throws ConfigurationManagerException if an error occured while creating a configuration | |
42 | * managment object. | |
43 | */ | |
44 | public static synchronized ConfigurationManagement getConfigurationManagement() throws ConfigurationManagerException { | |
45 | 135 | if (management == null) { |
46 | 80 | management = new ConfigurationManagementImpl(configurationFile); |
47 | 80 | management.reload(); |
48 | } | |
49 | 135 | return management; |
50 | } | |
51 | ||
52 | /** | |
53 | * Returns configuration manager. If the manager was not created yet, this call | |
54 | * will create it. | |
55 | * @return configuration manager. | |
56 | * @throws ConfigurationManagerException if an error occurs while creating a configuration | |
57 | * manager. | |
58 | */ | |
59 | public static synchronized ConfigurationManager getConfigurationManager() throws ConfigurationManagerException { | |
60 | 135 | if (manager == null) { |
61 | 70 | manager = getConfigurationManagement().getConfigurationManager(); |
62 | } | |
63 | 135 | return manager; |
64 | } | |
65 | ||
66 | /** | |
67 | * Clears the factory: resets configuration management, configuration manager and configuration file. | |
68 | * Needed for unit test settings only. | |
69 | */ | |
70 | static void clear() { | |
71 | 190 | synchronized(ConfigurationManagerFactory.class) { |
72 | 95 | ConfigurationManagerFactory.management = null; |
73 | 95 | ConfigurationManagerFactory.manager = null; |
74 | 95 | ConfigurationManagerFactory.configurationFile = "config.xml"; |
75 | } | |
76 | 95 | } |
77 | } |