001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-04-23 16:56:59 +0000 (Thu, 23 Apr 2015) $'
007 * '$Revision: 33366 $'
008 *
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.modulemanager.gui.patch;
031
032import java.io.IOException;
033import java.net.URL;
034import java.util.ArrayList;
035import java.util.List;
036
037import javax.swing.JDialog;
038import javax.swing.JFrame;
039import javax.swing.JOptionPane;
040
041import org.kepler.build.modules.Module;
042import org.kepler.build.modules.ModuleTree;
043import org.kepler.configuration.ConfigurationManager;
044import org.kepler.configuration.ConfigurationProperty;
045import org.kepler.modulemanager.RepositoryLocations;
046
047/**
048 * Created by David Welker.
049 * Date: Oct 26, 2009
050 * Time: 1:59:02 AM
051 */
052public class PatchChecker
053{
054    /** Check for and install any patches. */
055    public static void check(boolean batchMode)
056    {
057        check(false, batchMode, false);
058    }
059
060    /** Check for and install any patches for modules.
061     *  @param ignoreConfiguration If true, always check and install patches.
062     *  If false, only check and install patches if check-for-patches is true in
063     *  the module-manager configuration file. 
064     *  @param batchMode If true, automatically install all available patches.
065     *  If false and patches are available, show a dialog that asks the user to
066     *  install patches.
067     *  @param displayNoPatches If true, display a message if no patches are available.
068     */
069    public static void check(boolean ignoreConfiguration, boolean batchMode, boolean displayNoPatches)
070    {
071        if( !ignoreConfiguration )
072        {
073            ConfigurationProperty mm = ConfigurationManager.getInstance().getProperty(ConfigurationManager.getModule("module-manager"));
074            ConfigurationProperty checkForPatches = mm.getProperty("check-for-patches");
075            boolean shouldCheckForPatches = checkForPatches.getValue().trim().equals("true") ? true : false;
076            if( !shouldCheckForPatches )
077            {
078                System.out.println("Not checking for patches: check-for-patches is false.");
079                return;
080            }
081        }
082        try
083        {
084            URL url = new URL( RepositoryLocations.getReleaseLocation() );
085            if(url.openConnection().getContentType() == null) {
086                    return;
087            }
088        }
089        catch(IOException e)
090        {
091            System.out.println("Internet connection is down. Not checking for patches.");
092            return;
093        }
094
095        System.out.println("Checking for patches...");
096
097        List<String> released = Module.readReleased();
098
099        final List<ModulePair> upgradeList = new ArrayList<ModulePair>();
100        StringBuffer patchCandidateList = new StringBuffer();
101        for( Module m : ModuleTree.instance() )
102        {
103            if( m.hasUpArrow() )
104            {
105                String mostRecentPatch = m.transformNameWithList(m.getName(), released);
106                Module patchCandidate = Module.make(mostRecentPatch);
107                if( patchCandidate.getPatch() > m.getPatch() ) {
108                    upgradeList.add(new ModulePair(m, patchCandidate));
109                    patchCandidateList.append(patchCandidate.getName());
110                    patchCandidateList.append(", ");
111                }
112            }
113        }
114
115        if(!upgradeList.isEmpty()) {
116            if(batchMode) {
117                System.out.println("Installing available patches:\n" +
118                        patchCandidateList.substring(0, patchCandidateList.lastIndexOf(", ")));
119                
120                UpgradeDialogPanel.downloadModules(upgradeList, null);
121                                
122            } else {
123                JDialog dialog = new JDialog();
124                dialog.setModal(true);
125                dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
126                dialog.add(new UpgradeDialogPanel(upgradeList));
127                dialog.pack();
128                dialog.setSize(800,500);
129                dialog.setLocationRelativeTo(null);
130                dialog.setVisible(true);
131            }
132        } else if(displayNoPatches) {
133            if(batchMode) {
134                System.out.println("No new patches.");
135            } else {
136                JOptionPane.showMessageDialog(null, "No new patches.");
137            }
138        }
139    }
140}