|
From: fredrik <fre...@us...> - 2005-04-28 11:51:14
|
Update of /cvsroot/test-manager/main/src/testmanager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14164/src/testmanager Modified Files: Application.java Log Message: Reorganizing servlets & adding bug tracking Index: Application.java =================================================================== RCS file: /cvsroot/test-manager/main/src/testmanager/Application.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Application.java 24 Apr 2005 02:10:35 -0000 1.5 --- Application.java 28 Apr 2005 11:51:05 -0000 1.6 *************** *** 2,5 **** --- 2,7 ---- import java.io.File; + import java.io.FilenameFilter; + import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Logger; *************** *** 15,205 **** public class Application { ! private Logger logger; ! private int state = STATE_STARTING; ! /** ! * A version which is shown to the user at various places such as the main window title. Be ! * sure to update this field when a new version is released. ! */ ! public static final String VERSION = "0.2"; ! /** ! * A state where the application is starting up. Requests by the user to shut down the ! * application in this state should be ignored in order not to risk data damage. If all goes ! * well the application should enter the state STATE_RUNNING. If any problem is encountered, ! * it should go into STATE_ERROR. ! */ ! public static final int STATE_STARTING = 0; ! /** ! * A state where the application is running normally. If a fatal error occurs, the application ! * will be put into STATE_ERROR. If the user decides to shut down the application, it will be ! * put into STATE_SHUTTING_DOWN. ! */ ! public static final int STATE_RUNNING = 1; ! /** ! * A state where the application is shutting down as fast as possible. Further requests from ! * the user to shut down the application should be ignored in order not to risk damaging data. ! * If all goes well the application will be exited as by System.exit(0). If a fatal error ! * occurs it will be put into STATE_ERROR. ! */ ! public static final int STATE_SHUTTING_DOWN = 2; ! /** ! * A state where the application has encountered a fatal error. No proper shutdown can longer ! * be performed. The application should terminate upon request from the user. ! */ ! public static final int STATE_ERROR = 3; ! /** ! * The main() method of the application. The entry point to the whole program. ! * ! * @param args ! * The arguments supplied to the application, either through the command line or ! * or through the java web start (.jnlp) file. The only supported option at this ! * point is "-h" to indicate that the application should be running headless, ! * without any graphical user interface. ! */ ! public static void main(String[] args) { ! boolean headless = false; ! for (int i = 0; i < args.length; i++) { ! if (args[i].equals("-h")) { ! headless = true; ! } ! } ! instance = new Application(); ! instance.startUp(headless); ! } ! private static Application instance; ! private File applicationDirectory; ! public File getApplicationDirectory() { ! return applicationDirectory; ! } ! private void initializeApplicationDirectory() { ! applicationDirectory = new File(System.getProperty("user.home") + File.separator ! + "Open Test Manager"); ! if (applicationDirectory.exists()) { ! logger.info("Using existing application directory \"" + applicationDirectory.getPath() ! + "\""); ! } else { ! if (applicationDirectory.mkdir()) { ! logger.info("Application directory \"" + applicationDirectory.getAbsolutePath() ! + "\" created"); ! } else { ! logger.severe("Error: Could not create application directory \"" ! + applicationDirectory.getPath() + "\" - startup halted!"); ! applicationDirectory = null; ! } ! } ! } ! /** ! * Application is a Singleton, meaning that only one instance of the class will ever be ! * created. ! * ! * @return The Application singleton. ! */ ! public static Application getInstance() { ! return instance; ! } ! private MainWindow mainWindow; ! private Database database; ! private WebServer webServer; ! private FileHandler logFileHandler; ! /** ! * Startup the application. (1) Shows the main window. (2) Starts up the database. (3) Starts up ! * the web server. ! * ! * @param headLess ! * True if the application is to run headless (without any main window). ! */ ! public void startUp(boolean headLess) { ! logger = Logger.getLogger("Open Test Manager"); ! if (!headLess) { ! mainWindow = createWindow(); ! mainWindow.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE); ! mainWindow.pack(); ! mainWindow.setExtendedState(MainWindow.MAXIMIZED_BOTH); ! mainWindow.setVisible(true); ! logger.addHandler(new MainWindowLogHandler(mainWindow)); ! } ! initializeApplicationDirectory(); ! database = createDatabase(); ! webServer = createWebServer(); ! try { ! database.startUp(getApplicationDirectory(), logger); ! } catch (Exception e) { ! e.printStackTrace(); ! logger.severe("Error starting database server: \"" + e.getMessage() + "\" - aborting"); ! state = STATE_ERROR; ! return; ! } ! try { ! webServer.startUp(getApplicationDirectory(), logger, 5050); ! if (mainWindow != null) { ! mainWindow.setStatusLabel("Running at " + webServer.getServerURL()); ! } ! } catch (Exception e) { ! e.printStackTrace(); ! state = STATE_ERROR; ! logger.severe("Error starting web server: \"" + e.getMessage() + "\" - aborting!"); ! return; ! } ! logger.info("Setup done - application started!"); ! state = STATE_RUNNING; ! } ! /** ! * Shut down the application. ! */ ! public void shutDown() { ! state = STATE_SHUTTING_DOWN; ! try { ! webServer.shutDown(); ! database.shutDown(); ! System.exit(0); ! } catch (Exception e) { ! logger.severe("Error shutting down: " + e.getMessage()); ! e.printStackTrace(); ! } ! } ! protected MainWindow createWindow() { ! return new MainWindow(); ! } ! protected Database createDatabase() { ! return new Database(); ! } ! protected WebServer createWebServer() { ! return new WebServer(); ! } ! /** ! * @return Returns the current state of the application. ! */ ! public int getState() { ! return state; ! } } \ No newline at end of file --- 17,261 ---- public class Application { ! private Logger logger; ! private int state = STATE_STARTING; ! /** ! * A version which is shown to the user at various places such as the main ! * window title. Be sure to update this field when a new version is ! * released. ! */ ! public static final String VERSION = "0.3.1"; ! /** ! * A state where the application is starting up. Requests by the user to ! * shut down the application in this state should be ignored in order not to ! * risk data damage. If all goes well the application should enter the state ! * STATE_RUNNING. If any problem is encountered, it should go into ! * STATE_ERROR. ! */ ! public static final int STATE_STARTING = 0; ! /** ! * A state where the application is running normally. If a fatal error ! * occurs, the application will be put into STATE_ERROR. If the user decides ! * to shut down the application, it will be put into STATE_SHUTTING_DOWN. ! */ ! public static final int STATE_RUNNING = 1; ! /** ! * A state where the application is shutting down as fast as possible. ! * Further requests from the user to shut down the application should be ! * ignored in order not to risk damaging data. If all goes well the ! * application will be exited as by System.exit(0). If a fatal error occurs ! * it will be put into STATE_ERROR. ! */ ! public static final int STATE_SHUTTING_DOWN = 2; ! /** ! * A state where the application has encountered a fatal error. No proper ! * shutdown can longer be performed. The application should terminate upon ! * request from the user. ! */ ! public static final int STATE_ERROR = 3; ! /** ! * The main() method of the application. The entry point to the whole ! * program. ! * ! * @param args ! * The arguments supplied to the application, either through the ! * command line or or through the java web start (.jnlp) file. ! * The only supported option at this point is "-h" to indicate ! * that the application should be running headless, without any ! * graphical user interface. ! */ ! public static void main(String[] args) { ! boolean headless = false; ! for (int i = 0; i < args.length; i++) { ! if (args[i].equals("-h")) { ! headless = true; ! } ! } ! instance = new Application(); ! instance.startUp(headless); ! } ! private static Application instance; ! private File applicationDirectory; ! public File getApplicationDirectory() { ! return applicationDirectory; ! } ! /** ! * Helper method for deleting a directory and all files contained in the ! * directory. ! * ! * @param dir ! * The directory to be deleted. ! */ ! private static void deleteDir(File dir) { ! File[] contents = dir.listFiles(); ! for (int i = 0; i < contents.length; i++) { ! if (contents[i].isDirectory()) { ! deleteDir(contents[i]); ! } else if (contents[i].isFile()) { ! contents[i].delete(); ! } ! } ! dir.delete(); ! } ! /* ! * @return True if a new application directory has been created. ! */ ! private boolean initializeApplicationDirectory() { ! applicationDirectory = new File(System.getProperty("user.home") ! + File.separator + "Open Test Manager"); ! if (applicationDirectory.exists()) { ! if (applicationDirectory.listFiles(new FilenameFilter() { ! public boolean accept(File dir, String name) { ! return name.equals("version" + Application.VERSION); ! } ! }).length == 0) { ! deleteDir(applicationDirectory); ! } else { ! logger.info("Using existing application directory \"" ! + applicationDirectory.getPath() + "\""); ! return false; ! } ! } ! if (applicationDirectory.mkdir()) { ! logger.info("Application directory \"" ! + applicationDirectory.getAbsolutePath() + "\" created"); ! logger.info("Deleting old application directory as it is of an incompatible development version (this is " + Application.VERSION + ")"); ! try { ! new File(applicationDirectory.getAbsolutePath() + "/" + "version" + Application.VERSION).createNewFile(); ! } catch (IOException e) { ! logger.severe("Could not create version file: " + File.separatorChar + e.getMessage()); ! } ! return true; ! } else { ! logger.severe("Error: Could not create application directory \"" ! + applicationDirectory.getPath() + "\" - startup halted!"); ! applicationDirectory = null; ! } ! return false; ! } ! /** ! * Application is a Singleton, meaning that only one instance of the class ! * will ever be created. ! * ! * @return The Application singleton. ! */ ! public static Application getInstance() { ! return instance; ! } ! private MainWindow mainWindow; ! private Database database; ! private WebServer webServer; ! private FileHandler logFileHandler; ! /** ! * Startup the application. (1) Shows the main window. (2) Starts up the ! * database. (3) Starts up the web server. ! * ! * @param headLess ! * True if the application is to run headless (without any main ! * window). ! */ ! public void startUp(boolean headLess) { ! logger = Logger.getLogger("Open Test Manager"); ! if (!headLess) { ! mainWindow = createWindow(); ! mainWindow ! .setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE); ! mainWindow.pack(); ! mainWindow.setExtendedState(MainWindow.MAXIMIZED_BOTH); ! mainWindow.setVisible(true); ! logger.addHandler(new MainWindowLogHandler(mainWindow)); ! } ! boolean newDirectory = initializeApplicationDirectory(); ! /* ! * String adminPassword; if (newDirectory) { adminPassword = ! * mainWindow.getInitialAdminPassword(); } ! */ ! database = createDatabase(); ! webServer = createWebServer(); ! try { ! database.startUp(getApplicationDirectory(), logger); ! } catch (Exception e) { ! e.printStackTrace(); ! logger.severe("Error starting database server: \"" + e.getMessage() ! + "\" - aborting"); ! state = STATE_ERROR; ! return; ! } ! try { ! webServer.startUp(getApplicationDirectory(), logger, 5050); ! if (mainWindow != null) { ! mainWindow.setStatusLabel("Running at " ! + webServer.getServerURL()); ! } ! } catch (Exception e) { ! e.printStackTrace(); ! state = STATE_ERROR; ! logger.severe("Error starting web server: \"" + e.getMessage() ! + "\" - aborting!"); ! return; ! } ! logger.info("Setup done - application started!"); ! state = STATE_RUNNING; ! } ! /** ! * Shut down the application. ! */ ! public void shutDown() { ! state = STATE_SHUTTING_DOWN; ! try { ! webServer.shutDown(); ! database.shutDown(); ! System.exit(0); ! } catch (Exception e) { ! logger.severe("Error shutting down: " + e.getMessage()); ! e.printStackTrace(); ! } ! } ! protected MainWindow createWindow() { ! return new MainWindow(); ! } ! protected Database createDatabase() { ! return new Database(); ! } ! ! protected WebServer createWebServer() { ! return new WebServer(); ! } ! ! /** ! * @return Returns the current state of the application. ! */ ! public int getState() { ! return state; ! } } \ No newline at end of file |