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    
005    package graphlab.plugins.automaticupdator.net.interdirected.autoupdate;
006    
007    import org.tmatesoft.svn.core.SVNURL;
008    import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
009    import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
010    import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
011    import org.tmatesoft.svn.core.io.SVNRepository;
012    import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
013    import org.tmatesoft.svn.core.wc.*;
014    
015    import java.io.*;
016    import java.util.prefs.Preferences;
017    
018    /**
019     * Downloaded from: http://sourceforge.net/projects/javaautoupdater/
020     *
021     * @author Michael Quattlebaum
022     */
023    public class AutomatedUpdate {
024    
025        public static Preferences prefs;
026        private static SVNClientManager ourClientManager;
027        private static UpdateEventHandler myEventHandler;
028        private static GuiStatusScreen gss;
029        private static boolean usegui;
030    
031        private static final AntLauncher ant = new AntLauncher();
032    
033        /**
034         * @param args Not currently used. All parameters are stored in the
035         *             conf/autoupdate.xml file.
036         */
037        public static void main(String[] args) {
038            System.out.println("Starting AutomatedUpdate.java");
039            // So that we can execute this by instantiating the class if we want
040            // later on.
041            run(args);
042        }
043    
044        /**
045         * @param args Not currently used. All parameters are stored in the
046         *             conf/autoupdate.xml file.
047         */
048        public static void run(String[] args) {
049            // Get all of our properties and prepare for our update.
050            LoadPrefsFromFile();
051            SoftwareUpdateable upd = null;
052            String url = "http://graphlab.sharif.edu/svn";
053    //        String url = prefs.get("repositoryurl", null);
054            String module_url = "head/binary";
055    //        String module_url = prefs.get("moduleurl", null);
056            String name = "";
057    //        String name = prefs.get("repositoryusername", null);
058            String password = "";
059    //        String password = prefs.get("repositorypassword", null);
060            String applicationdirectory = "";
061    //        String applicationdirectory = prefs.get("applicationdirectory", null);
062            String buildfile = prefs.get("antbuildfile", "build.xml");
063            String antlocation = prefs.get("antlocation", ".");
064            String updatecheckclass = prefs.get("updatecheckclass", ".");
065            usegui = prefs.getBoolean("usegui", true);
066            boolean autoclose = prefs.getBoolean("autoclose", false);
067            boolean showbutton = !autoclose;
068    //        try {
069    //            upd = getSoftwareUpdateObject(updatecheckclass);
070    //            System.out.println("Update Object:" + upd);
071    //        }
072    //        catch (Exception e) {
073    //            e.printStackTrace();
074    //        }
075            if (true) {
076    //        if (upd.isUpdatable()) {
077                System.out.println("isUpdatable is true");
078                // Display the GUI status screen
079                if (usegui) {
080                    gss = new GuiStatusScreen(prefs.get("windowtitle", ""), prefs.get(
081                            "instructions", ""), prefs.getInt("screenwidth", 500),
082                            prefs.getInt("screenheight", 600), prefs.get("buttontext",
083                            "Update is Complete"), showbutton, "", prefs.getInt("imagewidth", 0),
084                            prefs.getInt("imageheight", 0));
085                    gss.appendText("Checking for file update.");
086                } else System.out.println("Checking for file update.");
087                // Setup the respository connections.
088                DAVRepositoryFactory.setup();
089                SVNRepository repository = null;
090                ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
091    
092                try {
093                    SVNRepositoryFactoryImpl.setup();
094                    repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url + "/" + module_url));
095                    ISVNAuthenticationManager authManager = SVNWCUtil
096                            .createDefaultAuthenticationManager(name, password);
097                    repository.setAuthenticationManager(authManager);
098    
099                    ourClientManager = SVNClientManager.newInstance(options,
100                            authManager);
101                    SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
102                    updateClient.setIgnoreExternals(false);
103                    myEventHandler = new UpdateEventHandler();
104                    if (usegui) myEventHandler.setGuiStatusScreen(gss);
105                    updateClient.setEventHandler(myEventHandler);
106                    // Determine if we have a directory for our update as defined in the
107                    // properties
108                    File outdirectory = new File(applicationdirectory).getAbsoluteFile();
109                    if (!outdirectory.exists()) {
110                        File parentdir = outdirectory.getParentFile();
111                        if (parentdir.exists())
112                            outdirectory.mkdir();
113                        else {
114                            parentdir.mkdir();
115                            outdirectory.mkdir();
116                        }
117                    }
118                    // Actually check out or update the files from the SVN Server.
119                    // The UpdateEventHandler class will handle the status updates
120                    // during the download.
121                    updateClient.doCheckout(SVNURL.parseURIDecoded(url + "/"
122                            + module_url), outdirectory, SVNRevision.HEAD,
123                            SVNRevision.HEAD, true); // Recursive
124                } catch (org.tmatesoft.svn.core.SVNAuthenticationException ae) {
125                    System.out.println("Cannot authorize user login.");
126                    ShowException(ae);
127                } catch (org.tmatesoft.svn.core.SVNException e) {
128                    System.out.println(e.getLocalizedMessage());
129                    ShowException(e);
130                }
131                if (usegui) gss.appendText("File check and download is complete.");
132                else System.out.println("File check and download is complete.");
133    
134                if (prefs.getBoolean("launchant", false)) {
135                    if (usegui) gss.appendText("Running update scripts.");
136                    else System.out.println("Running update scripts.");
137                    try {
138                        ExecuteAntLauncher(antlocation, applicationdirectory, buildfile);
139                    }
140                    catch (Exception e) {
141                        ShowException(e);
142                    }
143                    if (usegui) gss.appendText("Update scripts complete.");
144                    else System.out.println("Update scripts complete.");
145                }
146                // When we're finished, either display the status button or exit.
147                if (usegui) gss.appendText("Update is now complete.");
148                else System.out.println("Update is now complete.");
149                if (usegui && showbutton)
150                    gss.enableButton();
151    //              else
152    //                      System.exit(0);
153            }
154        }
155    
156        /**
157         * @return Returns a GuiStatusScreen if "usegui" is true. Otherwise returns
158         *         null.
159         */
160        public static GuiStatusScreen getGuiStatusScreen() {
161            if (usegui)
162                return gss;
163            else
164                return null;
165        }
166    
167        /**
168         * Static method to load the preferences for the updater from the
169         * conf/autoupdate.xml file.
170         */
171        private static void LoadPrefsFromFile() {
172            prefs = null;
173            InputStream is = null;
174            try {
175                is = new BufferedInputStream(new FileInputStream(
176                        "prefs" + File.separator + "autoupdate.xml"));
177            } catch (FileNotFoundException e) {
178                System.out
179                        .println("You must have a conf/autoupdate.xml configuration file.");
180            }
181            try {
182                Preferences.importPreferences(is);
183            } catch (Exception e) {
184                e.printStackTrace();
185            }
186            prefs = Preferences.systemNodeForPackage(AutomatedUpdate.class);
187        }
188    
189        private static void ShowException(Exception e) {
190            if (usegui) gss.appendText("ERROR: " + e.getMessage());
191            else System.out.println("ERROR: " + e.getMessage());
192            StackTraceElement[] trace = e.getStackTrace();
193            gss.appendText("TRACE:");
194            for (int i = 0; i < trace.length; i++) {
195                StackTraceElement stacke = trace[i];
196                if (usegui)
197                    gss.appendText("   " + stacke.getClassName() + "(" + stacke.getMethodName() + ") line " + stacke.getLineNumber());
198                else
199                    System.out.println("   " + stacke.getClassName() + "(" + stacke.getMethodName() + ") line " + stacke.getLineNumber());
200            }
201        }
202    
203        private static void ExecuteAntLauncher(String antlocation, String run_dir, String buildfile) throws Exception {
204            String separator = File.separator;
205            String cpseparator = File.pathSeparator;
206            ant.SetAntLocation(antlocation);
207            ant.SetRunDirectory(run_dir);
208            ant.SetBuildFile(run_dir + separator + buildfile);
209            String cp = System.getenv("CLASSPATH");
210            cp = cp +
211                    cpseparator + antlocation + separator + "lib" + separator + "ant.jar" + cpseparator +
212                    cpseparator + antlocation + separator + "lib" + separator + "ant-launcher.jar";
213            ant.SetClassPath(cp);
214    
215            try {
216                BufferedReader inr = ant.run();
217                String line = "";
218                while ((line = inr.readLine()) != null) {
219                    if (usegui) {
220                        if (line.indexOf("tools.jar") < 0)
221                            gss.appendText(line);
222                    } else System.out.println(line);
223                }
224            } catch (java.io.IOException ioe) {
225                ioe.printStackTrace();
226                ShowException(ioe);
227            }
228        }
229    
230        private static SoftwareUpdateable getSoftwareUpdateObject(String classname) {
231            try {
232                Class cl = Class.forName(classname);
233                java.lang.reflect.Constructor co = cl.getConstructor(new Class[]{});
234                return (SoftwareUpdateable) co.newInstance(new Object[]{});
235            }
236            catch (ClassNotFoundException badClass) {
237                badClass.printStackTrace();
238            }
239            catch (NoSuchMethodException badConst) {
240                badConst.printStackTrace();
241            }
242            catch (IllegalAccessException badPerm) {
243                badPerm.printStackTrace();
244            }
245            catch (IllegalArgumentException badParms) {
246                badParms.printStackTrace();
247            }
248            catch (InstantiationException abstractClass) {
249                abstractClass.printStackTrace();
250            }
251            catch (java.lang.reflect.InvocationTargetException ite) {
252                ite.printStackTrace();
253            }
254            return null;
255        }
256    }