View Javadoc

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   * @author skunk
51   */
52  public class ConfigurationManager implements IXMLSaveable, IXMLLoadable {
53  
54  	private static final transient Logger logger = Logger
55  			.getLogger(ConfigurationManager.class.getName());
56  
57  	/*** The only instance of ConfigurationManger present in the system.* */
58  	private static ConfigurationManager instance = null;
59  
60  	/*** The JDBS repository.* */
61  	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  	private SecurityLevel defaultBackupSecurityLevel = SecurityLevel.PRIVATE;
68  
69  	/*** The user's key ring location.* */
70  	private File keyRingLocation;
71  
72  	/*** The user location.* */
73  	private String userLocation = "";
74  
75  	/*** This is the JDBS User* */
76  	private User user = UserFactory.newUser();
77  
78  	/***
79  	 * This checks if before a peer download starts the peer is the real
80  	 * backups'owner.*
81  	 */
82  	private boolean enabledPeerDownloadAuthentication = true;
83  
84  	/***
85  	 * This is the peers' network connection type. It is used from other peers
86  	 * to estabilish and evaluate the available network bandwidth.*
87  	 */
88  	private NetworkConnectionType networkConnectionType = NetworkConnectionType.DSL;
89  
90  	/*** The default constructor.* */
91  	protected ConfigurationManager() {
92  
93  	}
94  
95  	/***
96  	 * @return the instance
97  	 */
98  	public static ConfigurationManager getInstance() {
99  		return instance == null ? instance = new ConfigurationManager()
100 				: instance;
101 	}
102 
103 	/***
104 	 * @return the repository
105 	 */
106 	public Repository getRepository() {
107 		if (this.repository == null) {
108 			this.repository = new Repository(null, 0);
109 		}
110 		return repository;
111 	}
112 
113 	/***
114 	 * This method reads from an XML document various configuration parameters
115 	 * for the JDBS.
116 	 * 
117 	 * @param document
118 	 *            An XML document that contains a list of configuration
119 	 *            parameters for the JDBS.
120 	 */
121 	public Object fromXml(Document document) {
122 		this.defaultBackupSecurityLevel = document
123 				.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 		this.enabledPeerDownloadAuthentication = document
130 				.getElementsByTagName("PeerDownloadAuthentication") != null ? Boolean
131 				.parseBoolean(document.getElementsByTagName(
132 						"PeerDownloadAuthentication").item(0).getAttributes()
133 						.getNamedItem("value").getNodeValue())
134 				: true;
135 
136 		this.networkConnectionType = document
137 				.getElementsByTagName("NetworkConnectionType") != null ? NetworkConnectionType
138 				.fromStringToConnectionType(document.getElementsByTagName(
139 						"PeerDownloadAuthentication").item(0).getAttributes()
140 						.getNamedItem("value").getNodeValue())
141 				: NetworkConnectionType.DSL;
142 
143 		this.keyRingLocation = document.getElementsByTagName("KeyRingLocation") != null ? new File(
144 				document.getElementsByTagName("KeyRingLocation").item(0)
145 						.getAttributes().getNamedItem("value").getNodeValue())
146 				: null;
147 
148 		this.userLocation = document.getElementsByTagName("UserLocation") != null ? document
149 				.getElementsByTagName("UserLocation").item(0).getAttributes()
150 				.getNamedItem("value").getNodeValue()
151 				: "";
152 
153 		this.repository = new Repository(document);
154 
155 		this.user = (User) UserFactory.newUser().fromXml(document);
156 		return this;
157 	}
158 
159 	/***
160 	 * This method writes to an XML document various configuration parameters of
161 	 * JDBS.
162 	 * 
163 	 * @param document
164 	 *            An XML document that will contain a list of configuration
165 	 *            parameters for the JDBS.
166 	 */
167 	public Node toXml(Document document) {
168 		Element configurationElement = document.createElement("JDBS");
169 
170 		Element defaultBackupSecurityLevelElement = document
171 				.createElement("SecurityLevel");
172 		defaultBackupSecurityLevelElement.setAttribute("value",
173 				this.defaultBackupSecurityLevel.toString());
174 		configurationElement.appendChild(defaultBackupSecurityLevelElement);
175 
176 		Element peerDownloadAuthenticationElement = document
177 				.createElement("PeerDownloadAuthentication");
178 		peerDownloadAuthenticationElement.setAttribute("value", ""
179 				+ this.enabledPeerDownloadAuthentication);
180 		configurationElement.appendChild(peerDownloadAuthenticationElement);
181 
182 		Element networkConnectionTypeElement = document
183 				.createElement("NetworkConnectionType");
184 		networkConnectionTypeElement
185 				.setAttribute("value", NetworkConnectionType
186 						.fromConnectionTypeToString(this.networkConnectionType));
187 		configurationElement.appendChild(networkConnectionTypeElement);
188 
189 		Element keyRingLocationElement = document
190 				.createElement("KeyRingLocation");
191 		keyRingLocationElement.setAttribute("value", "" + this.keyRingLocation);
192 		configurationElement.appendChild(keyRingLocationElement);
193 
194 		Element userLocationElement = document.createElement("UserLocation");
195 		userLocationElement.setAttribute("value", "" + this.userLocation);
196 		configurationElement.appendChild(userLocationElement);
197 
198 		configurationElement.appendChild(this.repository.toXml(document));
199 
200 		configurationElement.appendChild(this.user.toXml(document));
201 
202 		return configurationElement;
203 
204 	}
205 
206 	public static void loadConfiguration() {
207 		try {
208 			logger.debug("Reading JDBS's Configuration File...");
209 			DocumentBuilderFactory factory = DocumentBuilderFactory
210 					.newInstance();
211 			factory.setIgnoringComments(true);
212 			factory.setValidating(false);
213 			factory.setIgnoringElementContentWhitespace(true);
214 			DocumentBuilder docBuilder = factory.newDocumentBuilder();
215 			Document document = docBuilder.parse(new FileInputStream(new File(
216 					JDBSConstant.JDBS_CONFIGURATION_FILE)));
217 			ConfigurationManager.getInstance().fromXml(document);
218 		} catch (Exception ex) {
219 			ex.printStackTrace();
220 			logger.fatal(ex.getMessage());
221 			System.exit(-1);
222 		}
223 	}
224 
225 	public static void saveConfiguration() {
226 		try {
227 			logger.debug("Writing the JDBS's configuration file...");
228 			// Write It!
229 			Document doc = DocumentBuilderFactory.newInstance()
230 					.newDocumentBuilder().newDocument();
231 			doc.appendChild(ConfigurationManager.getInstance().toXml(doc));
232 			String fileName = new File(JDBSConstant.JDBS_CONFIGURATION_FILE)
233 					.getAbsolutePath();
234 			if (!fileName.endsWith(".xml"))
235 				fileName += ".xml";
236 			Transformer transformer = TransformerFactory.newInstance()
237 					.newTransformer();
238 			DOMSource source = new DOMSource(doc);
239 			StreamResult streamResult = new StreamResult(new PrintStream(
240 					fileName));
241 			transformer.transform(source, streamResult);
242 		} catch (Exception ex) {
243 			ex.printStackTrace();
244 			logger.fatal(ex.getMessage());
245 			System.exit(-1);
246 		}
247 	}
248 
249 	/***
250 	 * @return Returns the defaultBackupSecurityLevel.
251 	 */
252 	public SecurityLevel getDefaultBackupSecurityLevel() {
253 		return defaultBackupSecurityLevel;
254 	}
255 
256 	/***
257 	 * @param defaultBackupSecurityLevel
258 	 *            The defaultBackupSecurityLevel to set.
259 	 */
260 	public void setDefaultBackupSecurityLevel(
261 			SecurityLevel defaultBackupSecurityLevel) {
262 		this.defaultBackupSecurityLevel = defaultBackupSecurityLevel;
263 	}
264 
265 	/***
266 	 * @return Returns the keyRingLocation.
267 	 */
268 	public File getKeyRingLocation() {
269 		return keyRingLocation;
270 	}
271 
272 	/***
273 	 * @param keyRingLocation
274 	 *            The keyRingLocation to set.
275 	 */
276 	public void setKeyRingLocation(File keyRingLocation) {
277 		this.keyRingLocation = keyRingLocation;
278 	}
279 
280 	/***
281 	 * @return Returns the userLocation.
282 	 */
283 	public String getUserLocation() {
284 		return userLocation;
285 	}
286 
287 	/***
288 	 * @param userLocation
289 	 *            The userLocation to set.
290 	 */
291 	public void setUserLocation(String userLocation) {
292 		this.userLocation = userLocation;
293 	}
294 
295 	/***
296 	 * @return Returns the user.
297 	 */
298 	public User getUser() {
299 		return user;
300 	}
301 
302 	/***
303 	 * @param user
304 	 *            The user to set.
305 	 */
306 	public void setUser(User user) {
307 		this.user = user;
308 	}
309 
310 	/***
311 	 * @return Returns the enabledPeerDownloadAuthentication.
312 	 */
313 	public boolean isEnabledPeerDownloadAuthentication() {
314 		return enabledPeerDownloadAuthentication;
315 	}
316 
317 	/***
318 	 * @param enabledPeerDownloadAuthentication
319 	 *            The enabledPeerDownloadAuthentication to set.
320 	 */
321 	public void setEnabledPeerDownloadAuthentication(
322 			boolean enabledPeerDownloadAuthentication) {
323 		this.enabledPeerDownloadAuthentication = enabledPeerDownloadAuthentication;
324 	}
325 
326 	/***
327 	 * @return Returns the networkConnectionType.
328 	 */
329 	public NetworkConnectionType getNetworkConnectionType() {
330 		return networkConnectionType;
331 	}
332 
333 	/***
334 	 * @param networkConnectionType
335 	 *            The networkConnectionType to set.
336 	 */
337 	public void setNetworkConnectionType(
338 			NetworkConnectionType networkConnectionType) {
339 		this.networkConnectionType = networkConnectionType;
340 	}
341 
342 }