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;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import javax.naming.ConfigurationException;
22  
23  import org.apache.log4j.LogManager;
24  import org.apache.log4j.Logger;
25  import org.limmen.crs.api.ConfigurationManagementService;
26  import org.limmen.crs.api.ConfigurationService;
27  import org.limmen.crs.exception.ConfigurationNotFoundException;
28  import org.limmen.crs.exception.SectionNotFoundException;
29  import org.limmen.crs.exception.SettingNotFoundException;
30  import org.limmen.crs.model.Configuration;
31  import org.limmen.crs.model.Setting;
32  import org.limmen.crs.storage.api.ConfigurationStorage;
33  import org.limmen.crs.storage.api.ConfigurationStorageFactory;
34  import org.limmen.crs.storage.api.exception.UnknownStorageTypeException;
35  
36  public class ConfigurationServiceImpl implements ConfigurationService, ConfigurationManagementService {
37  
38  	private final static Logger logger = LogManager.getLogger(ConfigurationServiceImpl.class);
39  
40  	private final ConfigurationStorage configurationStorage;
41  
42  	public ConfigurationServiceImpl() throws UnknownStorageTypeException {
43  
44  		super();
45  
46  		configurationStorage = ConfigurationStorageFactory.getInstance().getStorage("XML");
47  
48  		getLogger().info("Indexing all existing configuration files...");
49  
50        for (String id : configurationStorage.getConfigurationIdList()) {
51  
52        	Configuration configuration = configurationStorage.loadConfiguration(id);
53  
54        	getLogger().info("Indexing: " + configuration.getId());
55  
56  	      BaseConfigurationIndex.addToIndex(configuration);
57        }
58  
59        getLogger().info("Finished indexing...");
60  
61  
62  	}
63  
64  	public void deleteConfiguration(String configurationId) {
65  
66  		try {
67  
68  			BaseConfigurationIndex.removeFromIndex(
69  	      		loadConfiguration(configurationId));
70  
71  			configurationStorage.deleteConfiguration(configurationId);
72        }
73  		catch (ConfigurationNotFoundException e) {
74  
75  			// should be there
76        }
77     }
78  
79  	public String getBaseConfiguration(String configurationId) {
80  
81  	   return BaseConfigurationIndex.getBaseConfiguration(configurationId);
82  	}
83  
84  	public List<String> getDerivativesOfBase(String configurationId) {
85  
86  	   return BaseConfigurationIndex.getDerivativesOfBase(configurationId);
87     }
88  
89  	public Map<String, List<String>> getHierarchialList() {
90  
91  	   return BaseConfigurationIndex.getHierarchialList();
92  	}
93  
94  	public List<String> getList() {
95  
96  	   return BaseConfigurationIndex.getConfigurationList();
97     }
98  
99     private Logger getLogger() {
100 
101 		return logger;
102 	}
103 
104 	public Setting getSetting(String configurationId, String section, String setting) throws ConfigurationNotFoundException, SectionNotFoundException, SettingNotFoundException {
105 
106 	   return loadConfiguration(configurationId).getSetting(section, setting);
107    }
108 
109 	public Configuration loadConfiguration(String configurationId) throws ConfigurationNotFoundException {
110 
111 		Configuration configuration = null;
112 
113       try {
114 
115       	configuration = configurationStorage.loadConfiguration(configurationId);
116       }
117       catch (Exception e) {
118 
119       	throw new ConfigurationNotFoundException(e);
120       }
121 
122       if (configuration == null) {
123 
124       	throw new ConfigurationNotFoundException(
125       			String.format("Configuration '%0$s' not found.", configurationId));
126       }
127 
128 		return configuration;
129    }
130 
131 	public void storeConfiguration(Configuration configuration) throws ConfigurationException {
132 
133 		BaseConfigurationIndex.addToIndex(configuration);
134 
135 		configurationStorage.storeConfiguration(configuration);
136    }
137 }