Coverage details for base.InternetCafe

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 package base;
21  
22 import java.awt.Dimension;
23 import java.awt.Toolkit;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.PrintStream;
27  
28 import javax.swing.UIManager;
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35  
36 import org.apache.log4j.Logger;
37 import org.apache.log4j.xml.DOMConfigurator;
38 import org.w3c.dom.Document;
39  
40 import ui.ICToolBar;
41 import ui.frame.ICMainFrame;
42 import ui.frame.MessageFrame;
43 import ui.panel.StatusBarPanel;
44 import ui.util.GeneralUtil;
45 import ui.util.LogoutDaemon;
460import base.jdbs.JDBSConstant;
470import base.jdbs.ui.dialog.RepositorySetupDialog;
480import base.user.User;
490 
500public class InternetCafe {
510    private static final transient Logger logger = Logger
520            .getLogger(InternetCafe.class.getName());
530 
540    private static InternetCafe instance = null;
550 
560    private static StatusBarPanel statusBar;
57  
580    private static ICToolBar toolBar;
59  
600    boolean packFrame = false;
610 
620    private ICMainFrame frame;
630 
640    // Construct the application
650    public InternetCafe() {
660        // Validate frames that have preset sizes
670        // Pack frames that have useful preferred size info, e.g. from their
680        // layout
690        if (packFrame) {
700            getMainFrame().pack();
710        } else {
720            getMainFrame().validate();
730        }
740 
750        // Set the application Look And Feel
76         try {
770            UIManager.setLookAndFeel(base.ConfigurationManager.getInstance().getInternetCafeLookAndFeel().getCanonicalName());
780        } catch (Exception ex) {
790            // TODO Auto-generated catch block
800            logger.error(ex.getMessage());
810            ex.printStackTrace();
820        }
830 
84         // Center the window
850        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
860        Dimension frameSize = getMainFrame().getSize();
870 
880        frameSize.height = screenSize.height;
890        frameSize.width = screenSize.width;
900        getMainFrame().setLocation((screenSize.width - frameSize.width) / 2,
910                (screenSize.height - frameSize.height) / 2);
920        getMainFrame().setVisible(true);
930    }
940 
950    public static synchronized InternetCafe getInstance() {
960        return (instance == null) ? (instance = new InternetCafe()) : instance;
970    }
980 
990    public ICMainFrame getMainFrame() {
1000        if (frame == null) {
1010            frame = new ICMainFrame();
1020        }
1030 
1040        return frame;
1050    }
1060 
1070    public static synchronized StatusBarPanel getStatusBar() {
1080        if (statusBar == null) {
1090            statusBar = new StatusBarPanel();
1100        }
1110 
1120        return statusBar;
1130    }
1140 
115     public static ICToolBar getToolBar() {
1160        if (toolBar == null) {
1170            toolBar = new ICToolBar();
1180        }
1190 
1200        return toolBar;
1210    }
1220    
1230    /**
1240     * @param message
1250     * A new message to show on the status bar.
1260     */
1270    public static synchronized void setStatusBarMessage(String message) {
1280        getStatusBar().setMessage(message);
1290    }
1300 
1310    protected static void initInternetCafe() {
1320        try {
1330            // This will setUp the message logging... MUST BE PLACED HERE
1340            MessageFrame.getInstance();
1350            logger.info(ConfigurationManager.COPYRIGHT);
1360            logger.debug("Setting Up configuration...");
1370 
1380            File resourcesDirectory = new File(
1390                    base.ConfigurationManager.RESOURCES_DIRECTORY);
1400 
1410            if (!resourcesDirectory.exists()
1420                    || !resourcesDirectory.isDirectory()) {
1430                resourcesDirectory.mkdir();
1440            }
1450 
1460            File configurationFile = new File(
1470                    base.ConfigurationManager.CONFIGURATION_FILE);
1480 
1490            if (configurationFile.exists()) { // Read It!
1500                logger.debug("Reading Internet Cafe's Configuration File...");
1510 
1520                DocumentBuilderFactory factory = DocumentBuilderFactory
1530                        .newInstance();
1540 
1550                factory.setIgnoringComments(true);
1560                factory.setValidating(false);
1570                factory.setIgnoringElementContentWhitespace(true);
1580 
1590                DocumentBuilder docBuilder = factory.newDocumentBuilder();
1600 
1610                Document document = docBuilder.parse(new FileInputStream(
1620                        configurationFile));
1630 
1640                base.ConfigurationManager.getInstance().fromXml(document);
1650            } else { // Write It!
1660 
1670                Document doc = DocumentBuilderFactory.newInstance()
1680                        .newDocumentBuilder().newDocument();
1690 
1700                doc.appendChild(base.ConfigurationManager.getInstance().toXml(doc));
1710 
1720                String fileName = configurationFile.getAbsolutePath();
1730 
1740                if (!fileName.endsWith(".xml")) {
1750                    fileName += ".xml";
1760                }
1770 
1780                Transformer transformer = TransformerFactory.newInstance()
1790                        .newTransformer();
1800                DOMSource source = new DOMSource(doc);
1810                StreamResult streamResult = new StreamResult(new PrintStream(
1820                        fileName));
1830 
1840                transformer.transform(source, streamResult);
1850            }
1860        } catch (Exception ex) {
1870            logger.error(ex.getMessage());
1880            ex.printStackTrace();
1890        }
1900    }
1910    
192     protected static void initLogin(){
193         try {
1940            Thread configurationThread = new Thread() {
1950                public void run() {
196                                     
1970                    InternetCafeManager.getInstance().retrieve();
1980 
199                     // InternetCafeManager.getInstance().getServer().start();
200                     if (base.ConfigurationManager.getInstance().isPasswordProtect()) {
201                         base.ConfigurationManager.getInstance()
202                                 .getServerLoginDialog().setVisible(true);
203                     }
204  
2050                    if (base.ConfigurationManager.getInstance()
2060                            .isAutomaticPasswordProtected()) {
2070                        new LogoutDaemon(base.ConfigurationManager.getInstance()
2080                                .getLogoutScheduleRate());
2090                    }
2100                    
2110                }
2120            };
2130 
2140            configurationThread.start();
2150            configurationThread.join();
2160        } catch (InterruptedException e) {
2170            logger.error(e.getMessage());
2180            e.printStackTrace();
2190        }
2200    }
2210 
222     protected static void initJDBS(User user) {
223         try {
2240            // First of all we must load the JDBS configurations.
2250            File configurationFile = new File(
2260                    JDBSConstant.JDBS_CONFIGURATION_FILE);
227  
2280            if (configurationFile.exists()) { // Read It!
2290                logger.info("Configuration file : " + configurationFile
2300                        + " found. Loading configurations from it...");
2310                base.jdbs.ConfigurationManager.loadConfiguration();
2320            } else {
2330                logger.info("Setting up the JDBS's user...");
2340                
2350                base.jdbs.ConfigurationManager.getInstance().setUser(user);
2360 
2370                logger.info("Setting up the JDBS's repository...");
2380                RepositorySetupDialog repositorySetupDialog = new RepositorySetupDialog();
2390                repositorySetupDialog.setModal(true);
2400                GeneralUtil.centerComponent(repositorySetupDialog);
2410                repositorySetupDialog.setVisible(true);
2420 
2430                base.jdbs.ConfigurationManager.saveConfiguration();
2440            }
2450        } catch (Exception e) {
2460            logger.error(e.getMessage());
2470            e.printStackTrace();
2480        }
2490 
2500        if (base.jdbs.ConfigurationManager.getInstance().getRepository().getLocation()
2510                .exists()) {
2520            logger.info("Indexing the JDBS's repository...");
2530            base.jdbs.ConfigurationManager.getInstance().getRepository()
2540                    .indexRepository();
255         }
2560    }
2570    
258     //Main method
259     public static void main(String[] arg) {
260         //Log4j init
2610        DOMConfigurator.configure("log4jConfiguration.xml");
2620 
2630        initInternetCafe();
2640        initLogin();
2650        if(ConfigurationManager.getInstance().isJDBSEnabled())initJDBS(base.ConfigurationManager.getInstance().getServerLoginDialog().getLoggedInUser());
2660        
2670        new InternetCafe();
2680    }
2690}

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.