001    /*
002     Copyright (C) 2003 Adam Olsen
003     
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008     
009     This program is distributed in the hope that it will be useful,
010     but WITHOUT ANY WARRANTY; without even the implied warranty of
011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     GNU General Public License for more details.
013     
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    
019    package com.valhalla.jbother;
020    
021    import java.awt.AWTEvent;
022    import java.awt.EventQueue;
023    import java.awt.Toolkit;
024    import java.io.File;
025    import java.util.Locale;
026    import java.util.ResourceBundle;
027    import java.util.Timer;
028    import java.util.TimerTask;
029    
030    import javax.swing.JOptionPane;
031    
032    /**
033     * JBother is a groovy Jabber client
034     *
035     * @author Adam Olsen (arolsen@gmail.com)
036     * @version 1.0
037     */
038    public class JBother {
039        public static final String JBOTHER_VERSION = "0.9.0cvs";
040        
041        public static String settingsDir = System.getProperty("user.home")
042        + File.separatorChar + ".jbother";
043        
044        public static String profileDir = JBother.settingsDir + File.separatorChar
045                + "profiles";
046        
047        public static boolean kiosk_mode = false;
048        
049        public static boolean exceptionLock = false;
050        
051        /**
052         * This is the main class, it basically just provides a loading point for
053         * the login screen - and also allows arguments to be passed from the
054         * command line.
055         *
056         * It checks the java version and if it's not greater than 1.4, it exits.
057         *
058         * @see com.valhalla.settings.Arguments
059         * @param args
060         *            arguments passed via the command line
061         */
062        public static void main(String args[]) {
063            new EventProcessor();
064            String version = System.getProperty("java.version");
065            StringBuffer buf = new StringBuffer();
066            
067            // we have to check the version this way because java versions < 1.4
068            // didn't have regular expressions (what a bite)
069            int dots = 0;
070            for (int i = 0; i < version.length(); i++) {
071                if (version.charAt(i) == '.')
072                    dots++;
073                if (dots >= 2)
074                    break;
075                
076                buf.append(version.charAt(i));
077            }
078            
079            
080            try{
081                File currentdir = new File("." + File.separatorChar +  ".jbother");
082                if(currentdir.exists() && currentdir.isDirectory()){
083                    JBother.settingsDir = currentdir.getCanonicalPath();
084                    JBother.profileDir = JBother.settingsDir +  File.separatorChar + "profiles";
085                }
086            }catch(Exception ignore){
087                JBother.settingsDir = System.getProperty("user.home") +  File.separatorChar + ".jbother";
088                JBother.profileDir = JBother.settingsDir + File.separatorChar +  "profiles";
089            }
090            
091            if (Double.parseDouble(buf.toString()) < Double.parseDouble("1.4")) {
092                ResourceBundle resources = ResourceBundle.getBundle(
093                        "JBotherBundle", Locale.getDefault());
094                
095                JOptionPane
096                        .showMessageDialog(null,
097                        resources.getString("jdk14Needed"), resources
098                        .getString("javaVersionError"),
099                        JOptionPane.WARNING_MESSAGE);
100                
101                System.exit(1);
102            } else {
103                com.valhalla.Logger.debug("Java version " + version + " ok");
104                try {
105                    new JBotherLoader().startJBother(args);
106                } catch (Throwable e) {
107                    com.valhalla.Logger
108                            .debug("An uncaught exception has occurred.  Stacktrace is below.");
109                    com.valhalla.Logger
110                            .debug("---------------------------------------------------------");
111                    com.valhalla.Logger.debug(e.toString());
112                    StackTraceElement el[] = e.getStackTrace();
113                    for (int i = 0; i < el.length; i++) {
114                        com.valhalla.Logger.debug(el[i].toString());
115                    }
116                    com.valhalla.Logger
117                            .debug("---------------------------------------------------------");
118                }
119            }
120        }
121    }
122    
123    class EventProcessor extends EventQueue {
124        public EventProcessor() {
125            Toolkit.getDefaultToolkit().getSystemEventQueue().push(this);
126        }
127        
128        protected void dispatchEvent(AWTEvent es) {
129            try {
130                super.dispatchEvent(es);
131            } catch (Throwable e) {
132                if (!JBother.exceptionLock) {
133                    com.valhalla.Logger
134                            .debug("An uncaught exception has occurred.  Stacktrace is below.");
135                    com.valhalla.Logger
136                            .debug("---------------------------------------------------------");
137                    com.valhalla.Logger.debug(e.toString());
138                    StackTraceElement el[] = e.getStackTrace();
139                    for (int i = 0; i < el.length; i++) {
140                        com.valhalla.Logger.debug(el[i].toString());
141                    }
142                    com.valhalla.Logger
143                            .debug("---------------------------------------------------------");
144                    
145                    // Throttle the Debug Console.
146                    JBother.exceptionLock = true;
147                    Timer timer = new Timer();
148                    timer.schedule(new ExceptionLock(), 50000);
149                }
150            }
151        }
152    }
153    
154    class ExceptionLock extends TimerTask {
155        
156        public void run() {
157            JBother.exceptionLock = false;
158        }
159    }