Coverage details for base.jdbs.ConfigurationManager

LineHitsSource
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  *
500 * @author skunk
51  */
520public class ConfigurationManager implements IXMLSaveable, IXMLLoadable {
53  
540    private static final transient Logger logger = Logger
550            .getLogger(ConfigurationManager.class.getName());
56  
57     /** The only instance of ConfigurationManger present in the system.* */
580    private static ConfigurationManager instance = null;
59  
60     /** The JDBS repository.* */
610    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      */
670    private SecurityLevel defaultBackupSecurityLevel = SecurityLevel.PRIVATE;
68  
690    /** The user's key ring location.* */
70     private File keyRingLocation;
71  
720    /** The user location.* */
730    private String userLocation = "";
74  
750    /** This is the JDBS User* */
760    private User user = UserFactory.newUser();
77  
780    /**
79      * This checks if before a peer download starts the peer is the real
800     * backups'owner.*
81      */
820    private boolean enabledPeerDownloadAuthentication = true;
83  
84     /**
85      * This is the peers' network connection type. It is used from other peers
860     * to estabilish and evaluate the available network bandwidth.*
87      */
880    private NetworkConnectionType networkConnectionType = NetworkConnectionType.DSL;
89  
90     /** The default constructor.* */
910    protected ConfigurationManager() {
92  
930    }
940 
95     /**
960     * @return the instance
97      */
98     public static ConfigurationManager getInstance() {
990        return instance == null ? instance = new ConfigurationManager()
100                 : instance;
101     }
102  
103     /**
104      * @return the repository
105      */
106     public Repository getRepository() {
1070        if (this.repository == null) {
1080            this.repository = new Repository(null, 0);
1090        }
1100        return repository;
1110    }
112  
1130    /**
114      * This method reads from an XML document various configuration parameters
115      * for the JDBS.
1160     *
117      * @param document
1180     * An XML document that contains a list of configuration
119      * parameters for the JDBS.
1200     */
121     public Object fromXml(Document document) {
1220        this.defaultBackupSecurityLevel = document
1230                .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  
1290        this.enabledPeerDownloadAuthentication = document
130                 .getElementsByTagName("PeerDownloadAuthentication") != null ? Boolean
131                 .parseBoolean(document.getElementsByTagName(
132                         "PeerDownloadAuthentication").item(0).getAttributes()
133                         .getNamedItem("value").getNodeValue())
134                 : true;
1350 
1360        this.networkConnectionType = document
1370                .getElementsByTagName("NetworkConnectionType") != null ? NetworkConnectionType
1380                .fromStringToConnectionType(document.getElementsByTagName(
1390                        "PeerDownloadAuthentication").item(0).getAttributes()
140                         .getNamedItem("value").getNodeValue())
1410                : NetworkConnectionType.DSL;
1420 
1430        this.keyRingLocation = document.getElementsByTagName("KeyRingLocation") != null ? new File(
144                 document.getElementsByTagName("KeyRingLocation").item(0)
1450                        .getAttributes().getNamedItem("value").getNodeValue())
1460                : null;
1470 
1480        this.userLocation = document.getElementsByTagName("UserLocation") != null ? document
1490                .getElementsByTagName("UserLocation").item(0).getAttributes()
1500                .getNamedItem("value").getNodeValue()
1510                : "";
152  
1530        this.repository = new Repository(document);
1540 
1550        this.user = (User) UserFactory.newUser().fromXml(document);
1560        return this;
1570    }
158  
1590    /**
160      * This method writes to an XML document various configuration parameters of
1610     * JDBS.
162      *
163      * @param document
164      * An XML document that will contain a list of configuration
165      * parameters for the JDBS.
166      */
1670    public Node toXml(Document document) {
1680        Element configurationElement = document.createElement("JDBS");
1690 
1700        Element defaultBackupSecurityLevelElement = document
1710                .createElement("SecurityLevel");
1720        defaultBackupSecurityLevelElement.setAttribute("value",
1730                this.defaultBackupSecurityLevel.toString());
1740        configurationElement.appendChild(defaultBackupSecurityLevelElement);
1750 
1760        Element peerDownloadAuthenticationElement = document
1770                .createElement("PeerDownloadAuthentication");
1780        peerDownloadAuthenticationElement.setAttribute("value", ""
1790                + this.enabledPeerDownloadAuthentication);
1800        configurationElement.appendChild(peerDownloadAuthenticationElement);
181  
1820        Element networkConnectionTypeElement = document
183                 .createElement("NetworkConnectionType");
1840        networkConnectionTypeElement
185                 .setAttribute("value", NetworkConnectionType
1860                        .fromConnectionTypeToString(this.networkConnectionType));
1870        configurationElement.appendChild(networkConnectionTypeElement);
1880 
1890        Element keyRingLocationElement = document
1900                .createElement("KeyRingLocation");
1910        keyRingLocationElement.setAttribute("value", "" + this.keyRingLocation);
1920        configurationElement.appendChild(keyRingLocationElement);
1930 
1940        Element userLocationElement = document.createElement("UserLocation");
1950        userLocationElement.setAttribute("value", "" + this.userLocation);
1960        configurationElement.appendChild(userLocationElement);
1970 
1980        configurationElement.appendChild(this.repository.toXml(document));
1990 
2000        configurationElement.appendChild(this.user.toXml(document));
201  
2020        return configurationElement;
203  
204     }
205  
2060    public static void loadConfiguration() {
207         try {
2080            logger.debug("Reading JDBS's Configuration File...");
2090            DocumentBuilderFactory factory = DocumentBuilderFactory
210                     .newInstance();
2110            factory.setIgnoringComments(true);
2120            factory.setValidating(false);
2130            factory.setIgnoringElementContentWhitespace(true);
2140            DocumentBuilder docBuilder = factory.newDocumentBuilder();
2150            Document document = docBuilder.parse(new FileInputStream(new File(
216                     JDBSConstant.JDBS_CONFIGURATION_FILE)));
2170            ConfigurationManager.getInstance().fromXml(document);
2180        } catch (Exception ex) {
2190            ex.printStackTrace();
2200            logger.fatal(ex.getMessage());
2210            System.exit(-1);
2220        }
2230    }
224  
225     public static void saveConfiguration() {
226         try {
2270            logger.debug("Writing the JDBS's configuration file...");
2280            // Write It!
2290            Document doc = DocumentBuilderFactory.newInstance()
230                     .newDocumentBuilder().newDocument();
2310            doc.appendChild(ConfigurationManager.getInstance().toXml(doc));
2320            String fileName = new File(JDBSConstant.JDBS_CONFIGURATION_FILE)
233                     .getAbsolutePath();
2340            if (!fileName.endsWith(".xml"))
2350                fileName += ".xml";
2360            Transformer transformer = TransformerFactory.newInstance()
237                     .newTransformer();
2380            DOMSource source = new DOMSource(doc);
2390            StreamResult streamResult = new StreamResult(new PrintStream(
240                     fileName));
2410            transformer.transform(source, streamResult);
2420        } catch (Exception ex) {
2430            ex.printStackTrace();
2440            logger.fatal(ex.getMessage());
2450            System.exit(-1);
2460        }
2470    }
248  
2490    /**
250      * @return Returns the defaultBackupSecurityLevel.
251      */
252     public SecurityLevel getDefaultBackupSecurityLevel() {
2530        return defaultBackupSecurityLevel;
254     }
255  
2560    /**
2570     * @param defaultBackupSecurityLevel
258      * The defaultBackupSecurityLevel to set.
259      */
260     public void setDefaultBackupSecurityLevel(
261             SecurityLevel defaultBackupSecurityLevel) {
2620        this.defaultBackupSecurityLevel = defaultBackupSecurityLevel;
2630    }
264  
265     /**
266      * @return Returns the keyRingLocation.
267      */
268     public File getKeyRingLocation() {
2690        return keyRingLocation;
2700    }
2710 
272     /**
273      * @param keyRingLocation
274      * The keyRingLocation to set.
275      */
276     public void setKeyRingLocation(File keyRingLocation) {
2770        this.keyRingLocation = keyRingLocation;
2780    }
279  
280     /**
281      * @return Returns the userLocation.
282      */
283     public String getUserLocation() {
2840        return userLocation;
2850    }
286  
287     /**
288      * @param userLocation
289      * The userLocation to set.
290      */
291     public void setUserLocation(String userLocation) {
2920        this.userLocation = userLocation;
2930    }
294  
295     /**
296      * @return Returns the user.
297      */
298     public User getUser() {
2990        return user;
300     }
301  
302     /**
303      * @param user
304      * The user to set.
305      */
306     public void setUser(User user) {
3070        this.user = user;
3080    }
309  
310     /**
311      * @return Returns the enabledPeerDownloadAuthentication.
312      */
313     public boolean isEnabledPeerDownloadAuthentication() {
3140        return enabledPeerDownloadAuthentication;
315     }
316  
317     /**
318      * @param enabledPeerDownloadAuthentication
319      * The enabledPeerDownloadAuthentication to set.
320      */
321     public void setEnabledPeerDownloadAuthentication(
322             boolean enabledPeerDownloadAuthentication) {
3230        this.enabledPeerDownloadAuthentication = enabledPeerDownloadAuthentication;
3240    }
325  
326     /**
327      * @return Returns the networkConnectionType.
328      */
329     public NetworkConnectionType getNetworkConnectionType() {
3300        return networkConnectionType;
331     }
332  
333     /**
334      * @param networkConnectionType
335      * The networkConnectionType to set.
336      */
337     public void setNetworkConnectionType(
338             NetworkConnectionType networkConnectionType) {
3390        this.networkConnectionType = networkConnectionType;
3400    }
341  
342 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.