alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.autoupdate;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.SwingUtilities;
import org.netbeans.updater.UpdateTracking;

import org.openide.ErrorManager;
import org.openide.LifecycleManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.Repository;
import org.openide.modules.InstalledFileLocator;
import org.openide.modules.ModuleInfo;
import org.openide.util.Lookup;
import org.openide.util.RequestProcessor;
import org.openide.util.SharedClassObject;
import org.openide.windows.WindowManager;


/** This singleton class is a skeleton for running autoupdate, it also
 * contains all communication with core of IDE implementation. This
 * communication is handled by org.netbeans.core.UpdateSupport
 * on the side of IDE Core implementation.
 *
 * @author  Petr Hrebejk
 */
class Autoupdater extends Object {

    /** Is the autoupdate running ? */
    private static boolean isRunning = false;

    /** Getter for static property isRunning */
    static boolean isRunning() {
        return isRunning;
    }

    /** Setter for static property isRunning */
    static void setRunning( boolean isRunning ) {
        Autoupdater.isRunning = isRunning;
    }

    /** Restarts IDE in order to run Updater */
    static void restart() {
        LifecycleManager.getDefault().exit();
    }

    /** Installs autoupdate checker */
    static void installUpdateChecker( final Runnable updateChecker ) {
        // XXX probably not ideal but should work for now...
        if (! Boolean.getBoolean("netbeans.full.hack") && ! Boolean.getBoolean("netbeans.close")) { // NOI18N
            // Just wait for GUI to pop up, whatever...
            l = new WindowListener () {
                public void windowOpened(WindowEvent e) {
                    doInstallUpdateChecker (updateChecker);
                }
                public void windowClosing(WindowEvent e) {}
                public void windowClosed(WindowEvent e) {}
                public void windowIconified(WindowEvent e) {}
                public void windowDeiconified(WindowEvent e) {}
                public void windowActivated(WindowEvent e) {}
                public void windowDeactivated(WindowEvent e) {}
            };
            // WindowsAPI is required to be called from AWT thread only
            SwingUtilities.invokeLater (new Runnable () {
                public void run () {
                    WindowManager.getDefault ().getMainWindow ().addWindowListener (l);
                }
            });
        }
    }
    
    private static WindowListener l;
    
    private static void doInstallUpdateChecker (final Runnable updateChecker) {
        // bugfix #43655, postpone the connect to AU till the main window is opened
        RequestProcessor.getDefault().post(updateChecker, 5000);
        WindowManager.getDefault ().getMainWindow ().removeWindowListener (l);
    }
    
    // Try to avoid referring directly to IDESettings.
    // If we can in fact find IDESettings and all appropriate methods, then we
    // use them. This means proxy config etc. will be properly persisted in
    // the system option. If something goes wrong, log it quietly and revert
    // to just setting the system properties (valid just for the session duration).
    
    private static Object settingsInstance;
   
    private static Method mGetUseProxy, mSetUseProxy, mGetProxyHost, mSetProxyHost, mGetProxyPort, mSetProxyPort;
    
    private static boolean useReflection() {
        initProxyMethodsMaybe();
        return mSetProxyPort != null;
    }

    private static boolean reflectionAlreadyTried = false;
    
    private static synchronized void initProxyMethodsMaybe() {
        if (reflectionAlreadyTried)
            return;
        
        reflectionAlreadyTried = true;
        
        try {
            ClassLoader l = (ClassLoader)Lookup.getDefault().lookup(ClassLoader.class);
            Class clazz = l.loadClass("org.netbeans.core.IDESettings"); // NOI18N
            settingsInstance = SharedClassObject.findObject(clazz, true);
            mGetUseProxy = clazz.getMethod("getUseProxy", null); // NOI18N
            mSetUseProxy = clazz.getMethod("setUseProxy", new Class[] {Boolean.TYPE}); // NOI18N
            mGetProxyHost = clazz.getMethod("getProxyHost", null); // NOI18N
            mSetProxyHost = clazz.getMethod("setProxyHost", new Class[] {String.class}); // NOI18N
            mGetProxyPort = clazz.getMethod("getProxyPort", null); // NOI18N
            mSetProxyPort = clazz.getMethod("setProxyPort", new Class[] {String.class}); // NOI18N
        } catch (Exception e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            // OK, use system properties rather than reflection.
        }
    }

    private static final String PROXY_HOST = "http.proxyHost"; // NOI18N
    private static final String PROXY_PORT = "http.proxyPort"; // NOI18N

    /** Gets proxy usage */
    static boolean isUseProxy() {
        if (useReflection()) {
            try {
                return ((Boolean)mGetUseProxy.invoke(settingsInstance, new Object[0])).booleanValue();
            } catch (Exception e) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
                return false;
            }
        } else {
            return System.getProperty(PROXY_HOST) != null;
        }
    }

    /** Gets Proxy Host */
    static String getProxyHost() {
        if (useReflection()) {
            try {
                return (String)mGetProxyHost.invoke(settingsInstance, new Object[0]);
            } catch (Exception e) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
                return null;
            }
        } else {
            return System.getProperty(PROXY_HOST);
        }
    }

    /** Gets Proxy Port */
    static String getProxyPort() {
        if (useReflection()) {
            try {
                return (String)mGetProxyPort.invoke(settingsInstance, new Object[0]);
            } catch (Exception e) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
                return null;
            }
        } else {
            return System.getProperty(PROXY_PORT);
        }
    }

    /** Sets the whole proxy configuration */
    static void setProxyConfiguration( boolean use, String host, String port ) {
        if (useReflection()) {
            try {
                mSetUseProxy.invoke(settingsInstance, new Object[] {use ? Boolean.TRUE : Boolean.FALSE});
                mSetProxyHost.invoke(settingsInstance, new Object[] {host});
                mSetProxyPort.invoke(settingsInstance, new Object[] {port});
            } catch (Exception e) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            }
        } else {
            if (use) {
                System.setProperty(PROXY_HOST, host);
                System.setProperty(PROXY_PORT, port);
            } else {
                System.setProperty(PROXY_HOST, ""); // NOI18N
                System.setProperty(PROXY_PORT, ""); // NOI18N
            }
        }
    }

    /** Singleton support class for getting directories */
    static class Support extends Object {

        /** Platform dependent file name separator */
        private static final String FILE_SEPARATOR = System.getProperty ("file.separator");
        
        /** Relative name of update directory */
        private static final String UPDATE_DIR = "update"; // NOI18N
        
        /** Relative name of directory where the .NBM files are downloaded */
        private static final String DOWNLOAD_DIR =UPDATE_DIR + FILE_SEPARATOR + "download"; // NOI18N
        
        /** Relative name of directory where the patch files are stored */
        private static final String PATCH_DIR = "lib" + FILE_SEPARATOR + "patches"; // NOI18N

        /** Relative name of key store file */
        private static final String KS_FILE = "core" + FILE_SEPARATOR + "ide.ks"; // NOI18N        
        
        /** The name of the log file */
        public static final String LOG_FILE_NAME = "update.log"; // NOI18N
        
        /** The name of the install_later file */
        public static final String LATER_FILE_NAME = "install_later.xml"; // NOI18N
        
        /** Relative name of temp directory */
        private static final String UPDATE_TEMP = "autoupdateTemp";
        
        /** File representing the download directory */
        private static File downloadDirectory = null;

        /** File representing the update directory */
        private static File updateDirectory = null;
        
        private static File nbDirectory = null;

        /** File representing the central patch directory */
        private static File centralPatchDirectory = null;

        /** File representing the users patch directory */
        private static File userPatchDirectory = null;

        /** The file with cenral KeySotre */
        private static File centralKSFile = null;

        /** The file with users KeySotre */
        private static File userKSFile = null;
        
        /** File object representing the temp directory */
        private static FileObject tempDir = null;

        /** Disable instantiation */
        private Support() {}

        /** Retruns array of module descriptions of installed modules */
        public static ModuleInfo[] getModuleDescriptions() {
            Collection descs = Lookup.getDefault().lookup(new Lookup.Template(ModuleInfo.class)).allInstances();
            return (ModuleInfo[])descs.toArray(new ModuleInfo[descs.size()]);
        }        

        /** The directory where to download the distribution files of modules */
        public static File getDownloadDirectory (File base) {
            if (base == null) {
                base = new File (System.getProperty ("netbeans.user"));
            }
            File downloadDirectory = new File (base, DOWNLOAD_DIR);
            if ( !downloadDirectory.isDirectory() ) {
                downloadDirectory.mkdirs();
            }

            return downloadDirectory;
        }
        
        /** Gets the central directory of patches */
        public static List/**/ getPatchDirectories () {
            List patches = new ArrayList ();
            List/**/ clusters = UpdateTracking.clusters (true);
            assert clusters != null : "Clusters cannot be empty."; // NOI18N
            Iterator it =  clusters.iterator ();
            while (it.hasNext ()) {
                File dir = (File)it.next ();
                assert dir.isDirectory () : "Cluster " + dir + " is directory."; // NOI18N
                patches.add (new File (dir.getPath () + FILE_SEPARATOR + PATCH_DIR));
            }
            return patches;
        }

        /** Gets the central keystore */
        static File getKSFile () {
//            List ksList = new ArrayList ();
//            List/**/ clusters = UpdateTracking.clusters (true);
//            assert clusters != null : "Clusters cannot be empty."; // NOI18N
//            Iterator it =  clusters.iterator ();
//            while (it.hasNext ()) {
//                File dir = (File)it.next ();
//                assert dir.isDirectory () : "Cluster " + dir + " is directory."; // NOI18N
//                try {
//                    FileObject foDir = FileUtil.toFileObject (FileUtil.normalizeFile (dir));
//                    FileObject ksFO = foDir.getFileObject (KS_FILE);
//                    if (ksFO != null) {
//                        ksList.add (FileUtil.toFile (ksFO));
//                    }
//                } catch (IllegalArgumentException iae) {
//                    assert false : iae;
//                }
//            }
//            File[] res = new File[ksList.size ()];
//            Iterator it2 = ksList.iterator ();
//            int i = 0;
//            while (it2.hasNext ()) {
//                res[i] = (File)it2.next ();
//                i++;
//            }
            File ksFileLocated = InstalledFileLocator.getDefault ().locate (KS_FILE, null, true);
            return ksFileLocated;
        }

        static File getInstall_Later (File root) {
            String dir;
            File file = new File (root.getPath () + FILE_SEPARATOR + DOWNLOAD_DIR + FILE_SEPARATOR + LATER_FILE_NAME);
            return file;
        }
        
        static File getTempCopyFile(FileObject original) {
            File f = null;
            if ( tempDir == null ) {
                FileSystem fs = Repository.getDefault().getDefaultFileSystem();
                tempDir = fs.getRoot().getFileObject( UPDATE_TEMP );
                try {
                    if ( tempDir == null )
                        tempDir = fs.getRoot().createFolder( UPDATE_TEMP );
                } catch ( java.io.IOException ioe ) {
                }
            }
            if ( tempDir != null )
                try {
                    FileObject fo = tempDir.getFileObject( original.getName(), "nbm" ); // NOI18N
                    if ( fo == null )
                        fo = FileUtil.copyFile( original, tempDir, original.getName() );
                    f = FileUtil.toFile( fo );
                } catch ( java.io.IOException ioe ) {
                }
            return f;
        }
        
        static void deleteTempDir() {
            if ( tempDir == null ) {
                FileSystem fs = Repository.getDefault().getDefaultFileSystem();
                tempDir = fs.getRoot().getFileObject( UPDATE_TEMP );
            }
            if ( tempDir != null ) {
                try {
                    tempDir.delete();
                } catch ( java.io.IOException ioe ) {
                }
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.