Line | Hits | Source |
---|---|---|
1 | /******************************************************************************* | |
2 | *InternetCafe is a software solution that helps the management of Cybercafes | |
3 | * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005. | |
4 | * Copyright (C) 2006 Guido Angelo Ingenito | |
5 | ||
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | ||
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | *******************************************************************************/ | |
20 | ||
21 | package base.jdbs; | |
22 | ||
23 | import java.io.File; | |
24 | import java.io.FileInputStream; | |
25 | import java.io.PrintStream; | |
26 | ||
27 | import javax.xml.parsers.DocumentBuilder; | |
28 | import javax.xml.parsers.DocumentBuilderFactory; | |
29 | import javax.xml.transform.Transformer; | |
30 | import javax.xml.transform.TransformerFactory; | |
31 | import javax.xml.transform.dom.DOMSource; | |
32 | import javax.xml.transform.stream.StreamResult; | |
33 | ||
34 | import org.apache.log4j.Logger; | |
35 | import org.w3c.dom.Document; | |
36 | import org.w3c.dom.Element; | |
37 | import org.w3c.dom.Node; | |
38 | ||
39 | import base.IXMLLoadable; | |
40 | import base.IXMLSaveable; | |
41 | import base.user.User; | |
42 | import base.user.UserFactory; | |
43 | ||
44 | /** | |
45 | * This is the class where every JDBS configuration paramenter is placed. This | |
46 | * class is helpful to write and load parameters configured by the user in xml | |
47 | * format and it is used from the entire application. Only one instance of this | |
48 | * class is present at runtime in the system | |
49 | * | |
50 | 0 | * @author skunk |
51 | */ | |
52 | 0 | public class ConfigurationManager implements IXMLSaveable, IXMLLoadable { |
53 | ||
54 | 0 | private static final transient Logger logger = Logger |
55 | 0 | .getLogger(ConfigurationManager.class.getName()); |
56 | ||
57 | /** The only instance of ConfigurationManger present in the system.* */ | |
58 | 0 | private static ConfigurationManager instance = null; |
59 | ||
60 | /** The JDBS repository.* */ | |
61 | 0 | private Repository repository = new Repository(new File(""), 0); |
62 | ||
63 | /** | |
64 | * The default security level for JDBS's created backups. (One between | |
65 | * PUBLIC or PRIVATE).* | |
66 | */ | |
67 | 0 | private SecurityLevel defaultBackupSecurityLevel = SecurityLevel.PRIVATE; |
68 | ||
69 | 0 | /** The user's key ring location.* */ |
70 | private File keyRingLocation; | |
71 | ||
72 | 0 | /** The user location.* */ |
73 | 0 | private String userLocation = ""; |
74 | ||
75 | 0 | /** This is the JDBS User* */ |
76 | 0 | private User user = UserFactory.newUser(); |
77 | ||
78 | 0 | /** |
79 | * This checks if before a peer download starts the peer is the real | |
80 | 0 | * backups'owner.* |
81 | */ | |
82 | 0 | private boolean enabledPeerDownloadAuthentication = true; |
83 | ||
84 | /** | |
85 | * This is the peers' network connection type. It is used from other peers | |
86 | 0 | * to estabilish and evaluate the available network bandwidth.* |
87 | */ | |
88 | 0 | private NetworkConnectionType networkConnectionType = NetworkConnectionType.DSL; |
89 | ||
90 | /** The default constructor.* */ | |
91 | 0 | protected ConfigurationManager() { |
92 | ||
93 | 0 | } |
94 | 0 | |
95 | /** | |
96 | 0 | * @return the instance |
97 | */ | |
98 | public static ConfigurationManager getInstance() { | |
99 | 0 | return instance == null ? instance = new ConfigurationManager() |
100 | : instance; | |
101 | } | |
102 | ||
103 | /** | |
104 | * @return the repository | |
105 | */ | |
106 | public Repository getRepository() { | |
107 | 0 | if (this.repository == null) { |
108 | 0 | this.repository = new Repository(null, 0); |
109 | 0 | } |
110 | 0 | return repository; |
111 | 0 | } |
112 | ||
113 | 0 | /** |
114 | * This method reads from an XML document various configuration parameters | |
115 | * for the JDBS. | |
116 | 0 | * |
117 | * @param document | |
118 | 0 | * An XML document that contains a list of configuration |
119 | * parameters for the JDBS. | |
120 | 0 | */ |
121 | public Object fromXml(Document document) { | |
122 | 0 | this.defaultBackupSecurityLevel = document |
123 | 0 | .getElementsByTagName("SecurityLevel") != null ? (document |
124 | .getElementsByTagName("SecurityLevel").item(0).getAttributes() | |
125 | .getNamedItem("value").getNodeValue().equalsIgnoreCase( | |
126 | SecurityLevel.PUBLIC.toString()) ? SecurityLevel.PUBLIC | |
127 | : SecurityLevel.PRIVATE) : SecurityLevel.PRIVATE; | |
128 | ||
129 | 0 | this.enabledPeerDownloadAuthentication = document |
130 | .getElementsByTagName("PeerDownloadAuthentication") != null ? Boolean | |
131 | .parseBoolean(document.getElementsByTagName( | |
132 | "PeerDownloadAuthentication").item(0).getAttributes() | |
133 | .getNamedItem("value").getNodeValue()) | |
134 | : true; | |
135 | 0 | |
136 | 0 | this.networkConnectionType = document |
137 | 0 | .getElementsByTagName("NetworkConnectionType") != null ? NetworkConnectionType |
138 | 0 | .fromStringToConnectionType(document.getElementsByTagName( |
139 | 0 | "PeerDownloadAuthentication").item(0).getAttributes() |
140 | .getNamedItem("value").getNodeValue()) | |
141 | 0 | : NetworkConnectionType.DSL; |
142 | 0 | |
143 | 0 | this.keyRingLocation = document.getElementsByTagName("KeyRingLocation") != null ? new File( |
144 | document.getElementsByTagName("KeyRingLocation").item(0) | |
145 | 0 | .getAttributes().getNamedItem("value").getNodeValue()) |
146 | 0 | : null; |
147 | 0 | |
148 | 0 | this.userLocation = document.getElementsByTagName("UserLocation") != null ? document |
149 | 0 | .getElementsByTagName("UserLocation").item(0).getAttributes() |
150 | 0 | .getNamedItem("value").getNodeValue() |
151 | 0 | : ""; |
152 | ||
153 | 0 | this.repository = new Repository(document); |
154 | 0 | |
155 | 0 | this.user = (User) UserFactory.newUser().fromXml(document); |
156 | 0 | return this; |
157 | 0 | } |
158 | ||
159 | 0 | /** |
160 | * This method writes to an XML document various configuration parameters of | |
161 | 0 | * JDBS. |
162 | * | |
163 | * @param document | |
164 | * An XML document that will contain a list of configuration | |
165 | * parameters for the JDBS. | |
166 | */ | |
167 | 0 | public Node toXml(Document document) { |
168 | 0 | Element configurationElement = document.createElement("JDBS"); |
169 | 0 | |
170 | 0 | Element defaultBackupSecurityLevelElement = document |
171 | 0 | .createElement("SecurityLevel"); |
172 | 0 | defaultBackupSecurityLevelElement.setAttribute("value", |
173 | 0 | this.defaultBackupSecurityLevel.toString()); |
174 | 0 | configurationElement.appendChild(defaultBackupSecurityLevelElement); |
175 | 0 | |
176 | 0 | Element peerDownloadAuthenticationElement = document |
177 | 0 | .createElement("PeerDownloadAuthentication"); |
178 | 0 | peerDownloadAuthenticationElement.setAttribute("value", "" |
179 | 0 | + this.enabledPeerDownloadAuthentication); |
180 | 0 | configurationElement.appendChild(peerDownloadAuthenticationElement); |
181 | ||
182 | 0 | Element networkConnectionTypeElement = document |
183 | .createElement("NetworkConnectionType"); | |
184 | 0 | networkConnectionTypeElement |
185 | .setAttribute("value", NetworkConnectionType | |
186 | 0 | .fromConnectionTypeToString(this.networkConnectionType)); |
187 | 0 | configurationElement.appendChild(networkConnectionTypeElement); |
188 | 0 | |
189 | 0 | Element keyRingLocationElement = document |
190 | 0 | .createElement("KeyRingLocation"); |
191 | 0 | keyRingLocationElement.setAttribute("value", "" + this.keyRingLocation); |
192 | 0 | configurationElement.appendChild(keyRingLocationElement); |
193 | 0 | |
194 | 0 | Element userLocationElement = document.createElement("UserLocation"); |
195 | 0 | userLocationElement.setAttribute("value", "" + this.userLocation); |
196 | 0 | configurationElement.appendChild(userLocationElement); |
197 | 0 | |
198 | 0 | configurationElement.appendChild(this.repository.toXml(document)); |
199 | 0 | |
200 | 0 | configurationElement.appendChild(this.user.toXml(document)); |
201 | ||
202 | 0 | return configurationElement; |
203 | ||
204 | } | |
205 | ||
206 | 0 | public static void loadConfiguration() { |
207 | try { | |
208 | 0 | logger.debug("Reading JDBS's Configuration File..."); |
209 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory |
210 | .newInstance(); | |
211 | 0 | factory.setIgnoringComments(true); |
212 | 0 | factory.setValidating(false); |
213 | 0 | factory.setIgnoringElementContentWhitespace(true); |
214 | 0 | DocumentBuilder docBuilder = factory.newDocumentBuilder(); |
215 | 0 | Document document = docBuilder.parse(new FileInputStream(new File( |
216 | JDBSConstant.JDBS_CONFIGURATION_FILE))); | |
217 | 0 | ConfigurationManager.getInstance().fromXml(document); |
218 | 0 | } catch (Exception ex) { |
219 | 0 | ex.printStackTrace(); |
220 | 0 | logger.fatal(ex.getMessage()); |
221 | 0 | System.exit(-1); |
222 | 0 | } |
223 | 0 | } |
224 | ||
225 | public static void saveConfiguration() { | |
226 | try { | |
227 | 0 | logger.debug("Writing the JDBS's configuration file..."); |
228 | 0 | // Write It! |
229 | 0 | Document doc = DocumentBuilderFactory.newInstance() |
230 | .newDocumentBuilder().newDocument(); | |
231 | 0 | doc.appendChild(ConfigurationManager.getInstance().toXml(doc)); |
232 | 0 | String fileName = new File(JDBSConstant.JDBS_CONFIGURATION_FILE) |
233 | .getAbsolutePath(); | |
234 | 0 | if (!fileName.endsWith(".xml")) |
235 | 0 | fileName += ".xml"; |
236 | 0 | Transformer transformer = TransformerFactory.newInstance() |
237 | .newTransformer(); | |
238 | 0 | DOMSource source = new DOMSource(doc); |
239 | 0 | StreamResult streamResult = new StreamResult(new PrintStream( |
240 | fileName)); | |
241 | 0 | transformer.transform(source, streamResult); |
242 | 0 | } catch (Exception ex) { |
243 | 0 | ex.printStackTrace(); |
244 | 0 | logger.fatal(ex.getMessage()); |
245 | 0 | System.exit(-1); |
246 | 0 | } |
247 | 0 | } |
248 | ||
249 | 0 | /** |
250 | * @return Returns the defaultBackupSecurityLevel. | |
251 | */ | |
252 | public SecurityLevel getDefaultBackupSecurityLevel() { | |
253 | 0 | return defaultBackupSecurityLevel; |
254 | } | |
255 | ||
256 | 0 | /** |
257 | 0 | * @param defaultBackupSecurityLevel |
258 | * The defaultBackupSecurityLevel to set. | |
259 | */ | |
260 | public void setDefaultBackupSecurityLevel( | |
261 | SecurityLevel defaultBackupSecurityLevel) { | |
262 | 0 | this.defaultBackupSecurityLevel = defaultBackupSecurityLevel; |
263 | 0 | } |
264 | ||
265 | /** | |
266 | * @return Returns the keyRingLocation. | |
267 | */ | |
268 | public File getKeyRingLocation() { | |
269 | 0 | return keyRingLocation; |
270 | 0 | } |
271 | 0 | |
272 | /** | |
273 | * @param keyRingLocation | |
274 | * The keyRingLocation to set. | |
275 | */ | |
276 | public void setKeyRingLocation(File keyRingLocation) { | |
277 | 0 | this.keyRingLocation = keyRingLocation; |
278 | 0 | } |
279 | ||
280 | /** | |
281 | * @return Returns the userLocation. | |
282 | */ | |
283 | public String getUserLocation() { | |
284 | 0 | return userLocation; |
285 | 0 | } |
286 | ||
287 | /** | |
288 | * @param userLocation | |
289 | * The userLocation to set. | |
290 | */ | |
291 | public void setUserLocation(String userLocation) { | |
292 | 0 | this.userLocation = userLocation; |
293 | 0 | } |
294 | ||
295 | /** | |
296 | * @return Returns the user. | |
297 | */ | |
298 | public User getUser() { | |
299 | 0 | return user; |
300 | } | |
301 | ||
302 | /** | |
303 | * @param user | |
304 | * The user to set. | |
305 | */ | |
306 | public void setUser(User user) { | |
307 | 0 | this.user = user; |
308 | 0 | } |
309 | ||
310 | /** | |
311 | * @return Returns the enabledPeerDownloadAuthentication. | |
312 | */ | |
313 | public boolean isEnabledPeerDownloadAuthentication() { | |
314 | 0 | return enabledPeerDownloadAuthentication; |
315 | } | |
316 | ||
317 | /** | |
318 | * @param enabledPeerDownloadAuthentication | |
319 | * The enabledPeerDownloadAuthentication to set. | |
320 | */ | |
321 | public void setEnabledPeerDownloadAuthentication( | |
322 | boolean enabledPeerDownloadAuthentication) { | |
323 | 0 | this.enabledPeerDownloadAuthentication = enabledPeerDownloadAuthentication; |
324 | 0 | } |
325 | ||
326 | /** | |
327 | * @return Returns the networkConnectionType. | |
328 | */ | |
329 | public NetworkConnectionType getNetworkConnectionType() { | |
330 | 0 | return networkConnectionType; |
331 | } | |
332 | ||
333 | /** | |
334 | * @param networkConnectionType | |
335 | * The networkConnectionType to set. | |
336 | */ | |
337 | public void setNetworkConnectionType( | |
338 | NetworkConnectionType networkConnectionType) { | |
339 | 0 | this.networkConnectionType = networkConnectionType; |
340 | 0 | } |
341 | ||
342 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |