001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.platform;
005    
006    import graphlab.platform.core.BlackBoard;
007    import graphlab.platform.core.exception.ExceptionHandler;
008    import graphlab.platform.extension.Extension;
009    import graphlab.platform.extension.ExtensionClassLoader;
010    import graphlab.platform.extension.ExtensionLoader;
011    import graphlab.platform.plugin.Plugger;
012    import graphlab.platform.preferences.Preferences;
013    import graphlab.platform.preferences.lastsettings.StorableOnExit;
014    
015    import java.io.File;
016    import java.net.URLClassLoader;
017    
018    
019    /**
020     * The Main runner of program
021     * Author: reza
022     */
023    public class Application implements StorableOnExit {
024    
025        public static final String APPLICATION_INSTANCE = "GraphLab.main";
026        public Plugger plugger = null;
027        public static final String POST_INIT_EVENT = "Post Initialization";
028    
029        /**
030         * @param blackboard
031         * @see graphlab.platform.Application#main(String[])
032         */
033        public void run(BlackBoard blackboard) {
034            try {
035                Preferences p = new Preferences(blackboard);
036                GSplash gs = new GSplash();
037                gs.showMessages();
038    //            Thread.sleep(3000);
039    //            Thread.sleep(1000);
040                loadPlugins();
041    //            Thread.sleep(2000);
042                loadExtensions(blackboard);
043                gs.setVisible(false);
044                gs.stopShowing();
045                blackboard.setData(POST_INIT_EVENT, "Pi");
046            } catch (Exception e) {
047                e.printStackTrace();
048            }
049    //        UI.getGFrame(blackboard).setTitle("GraphLab Graph Editor-1");
050        }
051    
052        private void loadPlugins() {
053            plugger.plug();
054    
055        }
056    
057        /**
058         * The default BlackBoard which plugins/extensions are connected to
059         */
060        public static BlackBoard blackboard;
061    
062        public static BlackBoard getBlackBoard() {
063            return blackboard;
064        }
065    
066        /**
067         * @return
068         * @see graphlab.platform.Application#main(String[])
069         */
070        public BlackBoard init() {
071            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(blackboard));
072            blackboard = new BlackBoard();
073            blackboard.setData(APPLICATION_INSTANCE, this);
074            blackboard.setData("SETTINGS", SETTINGS);
075            plugger = new Plugger(blackboard);
076            run(blackboard);
077            return blackboard;
078    
079        }
080    
081        /**
082         * load all extensions from /extensions directory
083         *
084         * @param blackboard
085         */
086        public void loadExtensions(BlackBoard blackboard) {
087            ExtensionClassLoader.cl = getExtensionsClassLoader();
088            ExtensionClassLoader e = new ExtensionClassLoader("extensions");
089            for (String c : e.classesData.keySet()) {
090                try {
091                    Class s = getExtensionsClassLoader().loadClass(c);
092                    Object extension = ExtensionLoader.loadExtension(s);
093                    if (extension != null) {
094                        SETTINGS.registerSetting(extension, "Extention Options");
095                        ExtensionLoader.handleExtension(blackboard, extension);
096                    }
097                } catch (ClassNotFoundException e1) {
098                    e1.printStackTrace();
099                }
100            }
101            for (File f : e.getUnknownFilesFound()) {
102                Extension extension = ExtensionLoader.loadUnknownExtension(f, blackboard);
103                ExtensionLoader.handleExtension(blackboard, extension);
104            }
105        }
106    
107        protected URLClassLoader getExtensionsClassLoader() {
108            return plugger.classLoader;
109        }
110    
111        /**
112         * Runs Application in these steps:
113         * 0- starts GraphLab exception handler, to catch uncaught exceptions<br>
114         * 1- shows a splash on the screen, and redirect System.out to the splash screen<br>
115         * 2- loads plugins which are jar files located in plugins directory<br>
116         * 3- loads extensions which are files located in extensions directory<br>
117         * 4- fires a <code>Application#POST_INIT_EVENT</code> on blackboard<br>
118         * 5- hides the splash screen<br>
119         *
120         * @param args
121         */
122        public static void main(String[] args) {
123            new Application().init();
124        }
125    
126    }