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  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;
46  import base.jdbs.JDBSConstant;
47  import base.jdbs.ui.dialog.RepositorySetupDialog;
48  import base.user.User;
49  
50  public class InternetCafe {
51  	private static final transient Logger logger = Logger
52  			.getLogger(InternetCafe.class.getName());
53  
54  	private static InternetCafe instance = null;
55  
56  	private static StatusBarPanel statusBar;
57  
58  	private static ICToolBar toolBar;
59  
60  	boolean packFrame = false;
61  
62  	private ICMainFrame frame;
63  
64  	// Construct the application
65  	public InternetCafe() {
66  		// Validate frames that have preset sizes
67  		// Pack frames that have useful preferred size info, e.g. from their
68  		// layout
69  		if (packFrame) {
70  			getMainFrame().pack();
71  		} else {
72  			getMainFrame().validate();
73  		}
74  
75  		// Set the application Look And Feel
76  		try {
77  			UIManager.setLookAndFeel(base.ConfigurationManager.getInstance().getInternetCafeLookAndFeel().getCanonicalName());
78  		} catch (Exception ex) {
79  			// TODO Auto-generated catch block
80  			logger.error(ex.getMessage());
81  			ex.printStackTrace();
82  		}
83  
84  		// Center the window
85  		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
86  		Dimension frameSize = getMainFrame().getSize();
87  
88  		frameSize.height = screenSize.height;
89  		frameSize.width = screenSize.width;
90  		getMainFrame().setLocation((screenSize.width - frameSize.width) / 2,
91  				(screenSize.height - frameSize.height) / 2);
92  		getMainFrame().setVisible(true);
93  	}
94  
95  	public static synchronized InternetCafe getInstance() {
96  		return (instance == null) ? (instance = new InternetCafe()) : instance;
97  	}
98  
99  	public ICMainFrame getMainFrame() {
100 		if (frame == null) {
101 			frame = new ICMainFrame();
102 		}
103 
104 		return frame;
105 	}
106 
107 	public static synchronized StatusBarPanel getStatusBar() {
108 		if (statusBar == null) {
109 			statusBar = new StatusBarPanel();
110 		}
111 
112 		return statusBar;
113 	}
114 
115 	public static ICToolBar getToolBar() {
116 		if (toolBar == null) {
117 			toolBar = new ICToolBar();
118 		}
119 
120 		return toolBar;
121 	}
122 	
123 	/***
124 	 * @param message
125 	 *            A new message to show on the status bar.
126 	 */
127 	public static synchronized void setStatusBarMessage(String message) {
128 		getStatusBar().setMessage(message);
129 	}
130 
131 	protected static void initInternetCafe() {
132 		try {
133 			// This will setUp the message logging... MUST BE PLACED HERE
134 			MessageFrame.getInstance();
135 			logger.info(ConfigurationManager.COPYRIGHT);
136 			logger.debug("Setting Up configuration...");
137 
138 			File resourcesDirectory = new File(
139 					base.ConfigurationManager.RESOURCES_DIRECTORY);
140 
141 			if (!resourcesDirectory.exists()
142 					|| !resourcesDirectory.isDirectory()) {
143 				resourcesDirectory.mkdir();
144 			}
145 
146 			File configurationFile = new File(
147 					base.ConfigurationManager.CONFIGURATION_FILE);
148 
149 			if (configurationFile.exists()) { // Read It!
150 				logger.debug("Reading Internet Cafe's Configuration File...");
151 
152 				DocumentBuilderFactory factory = DocumentBuilderFactory
153 						.newInstance();
154 
155 				factory.setIgnoringComments(true);
156 				factory.setValidating(false);
157 				factory.setIgnoringElementContentWhitespace(true);
158 
159 				DocumentBuilder docBuilder = factory.newDocumentBuilder();
160 
161 				Document document = docBuilder.parse(new FileInputStream(
162 						configurationFile));
163 
164 				base.ConfigurationManager.getInstance().fromXml(document);
165 			} else { // Write It!
166 
167 				Document doc = DocumentBuilderFactory.newInstance()
168 						.newDocumentBuilder().newDocument();
169 
170 				doc.appendChild(base.ConfigurationManager.getInstance().toXml(doc));
171 
172 				String fileName = configurationFile.getAbsolutePath();
173 
174 				if (!fileName.endsWith(".xml")) {
175 					fileName += ".xml";
176 				}
177 
178 				Transformer transformer = TransformerFactory.newInstance()
179 						.newTransformer();
180 				DOMSource source = new DOMSource(doc);
181 				StreamResult streamResult = new StreamResult(new PrintStream(
182 						fileName));
183 
184 				transformer.transform(source, streamResult);
185 			}
186 		} catch (Exception ex) {
187 			logger.error(ex.getMessage());
188 			ex.printStackTrace();
189 		}
190 	}
191 	
192 	protected static void initLogin(){
193 		try {
194 			Thread configurationThread = new Thread() {
195 				public void run() {
196 									
197 					InternetCafeManager.getInstance().retrieve();
198 
199 					// InternetCafeManager.getInstance().getServer().start();
200 					if (base.ConfigurationManager.getInstance().isPasswordProtect()) {
201 						base.ConfigurationManager.getInstance()
202 								.getServerLoginDialog().setVisible(true);
203 					}
204 
205 					if (base.ConfigurationManager.getInstance()
206 							.isAutomaticPasswordProtected()) {
207 						new LogoutDaemon(base.ConfigurationManager.getInstance()
208 								.getLogoutScheduleRate());
209 					}
210 					
211 				}
212 			};
213 
214 			configurationThread.start();
215 			configurationThread.join();
216 		} catch (InterruptedException e) {
217 			logger.error(e.getMessage());
218 			e.printStackTrace();
219 		}
220 	}
221 
222 	protected static void initJDBS(User user) {
223 		try {
224 			// First of all we must load the JDBS configurations.
225 			File configurationFile = new File(
226 					JDBSConstant.JDBS_CONFIGURATION_FILE);
227 
228 			if (configurationFile.exists()) { // Read It!
229 				logger.info("Configuration file : " + configurationFile
230 						+ " found. Loading configurations from it...");
231 				base.jdbs.ConfigurationManager.loadConfiguration();
232 			} else {
233 				logger.info("Setting up the JDBS's user...");
234 				
235 				base.jdbs.ConfigurationManager.getInstance().setUser(user);
236 
237 				logger.info("Setting up the JDBS's repository...");
238 				RepositorySetupDialog repositorySetupDialog = new RepositorySetupDialog();
239 				repositorySetupDialog.setModal(true);
240 				GeneralUtil.centerComponent(repositorySetupDialog);
241 				repositorySetupDialog.setVisible(true);
242 
243 				base.jdbs.ConfigurationManager.saveConfiguration();
244 			}
245 		} catch (Exception e) {
246 			logger.error(e.getMessage());
247 			e.printStackTrace();
248 		}
249 
250 		if (base.jdbs.ConfigurationManager.getInstance().getRepository().getLocation()
251 				.exists()) {
252 			logger.info("Indexing the JDBS's repository...");
253 			base.jdbs.ConfigurationManager.getInstance().getRepository()
254 					.indexRepository();
255 		}
256 	}
257 	
258 	//Main method
259 	public static void main(String[] arg) {
260 		//Log4j init
261 		DOMConfigurator.configure("log4jConfiguration.xml");
262 
263 		initInternetCafe();
264 		initLogin();
265 		if(ConfigurationManager.getInstance().isJDBSEnabled())initJDBS(base.ConfigurationManager.getInstance().getServerLoginDialog().getLoggedInUser());
266 		
267 		new InternetCafe();
268 	}
269 }