use modulemanagement instaed of pluginimporter nbi_check_updates_216192
authorJiri Rechtacek <jrechtacek@netbeans.org>
Tue, 28 Aug 2012 22:07:58 +0200
branchnbi_check_updates_216192
changeset 17866f63faeafba09
parent 17865 d064c45ccb10
child 17867 2d6a60e2df6f
use modulemanagement instaed of pluginimporter
modulemanagement/src/org/netbeans/modules/modulemanagement/ModuleOptions.java
modulemanagement/src/org/netbeans/modules/modulemanagement/Status.java
     1.1 --- a/modulemanagement/src/org/netbeans/modules/modulemanagement/ModuleOptions.java	Mon Aug 27 15:54:33 2012 +0200
     1.2 +++ b/modulemanagement/src/org/netbeans/modules/modulemanagement/ModuleOptions.java	Tue Aug 28 22:07:58 2012 +0200
     1.3 @@ -43,7 +43,6 @@
     1.4  
     1.5  import java.io.IOException;
     1.6  import java.io.PrintStream;
     1.7 -import java.text.MessageFormat;
     1.8  import java.util.Arrays;
     1.9  import java.util.Collections;
    1.10  import java.util.HashSet;
    1.11 @@ -51,11 +50,13 @@
    1.12  import java.util.Map;
    1.13  import java.util.Set;
    1.14  import java.util.logging.Logger;
    1.15 +import java.util.prefs.Preferences;
    1.16  import java.util.regex.Pattern;
    1.17  import java.util.regex.PatternSyntaxException;
    1.18  import org.netbeans.api.autoupdate.*;
    1.19  import org.netbeans.api.autoupdate.InstallSupport.Installer;
    1.20  import org.netbeans.api.autoupdate.InstallSupport.Validator;
    1.21 +import org.netbeans.api.autoupdate.OperationContainer.OperationInfo;
    1.22  import org.netbeans.api.autoupdate.OperationSupport.Restarter;
    1.23  import org.netbeans.api.sendopts.CommandException;
    1.24  import org.netbeans.spi.sendopts.Env;
    1.25 @@ -68,7 +69,7 @@
    1.26  
    1.27  /**
    1.28   *
    1.29 - * @author Jaroslav Tulach
    1.30 + * @author Jaroslav Tulach, Jiri Rechtacek
    1.31   */
    1.32  @org.openide.util.lookup.ServiceProvider(service=org.netbeans.spi.sendopts.OptionProcessor.class)
    1.33  public class ModuleOptions extends OptionProcessor {
    1.34 @@ -80,6 +81,7 @@
    1.35      private Option enable;
    1.36      private Option update;
    1.37      private Option refresh;
    1.38 +    private Option updateAll;
    1.39      private Option both;
    1.40      
    1.41      /** Creates a new instance of ModuleOptions */
    1.42 @@ -88,7 +90,8 @@
    1.43  
    1.44      @NbBundle.Messages({
    1.45          "MSG_UpdateModules=Updates all or specified modules",
    1.46 -        "MSG_RefreshModules=Refresh catalog of available modules"
    1.47 +        "MSG_UpdateAll=Updates all modules",
    1.48 +        "MSG_Refresh=Refresh all catalogs"
    1.49      })
    1.50      private Option init() {
    1.51          if (both != null) {
    1.52 @@ -107,9 +110,11 @@
    1.53          update = Option.shortDescription(
    1.54              Option.additionalArguments(Option.NO_SHORT_NAME, "update"), b, "MSG_UpdateModules"); // NOI18N
    1.55          refresh = Option.shortDescription(
    1.56 -            Option.additionalArguments(Option.NO_SHORT_NAME, "refresh"), b, "MSG_RefreshModules"); // NOI18N
    1.57 +            Option.withoutArgument(Option.NO_SHORT_NAME, "refresh"), b, "MSG_Refresh"); // NOI18N
    1.58 +        updateAll = Option.shortDescription(
    1.59 +            Option.withoutArgument(Option.NO_SHORT_NAME, "update-all"), b, "MSG_UpdateAll"); // NOI18N
    1.60          
    1.61 -        Option oper = OptionGroups.oneOf(list, install, disable, enable, update, refresh);
    1.62 +        Option oper = OptionGroups.someOf(refresh, list, install, disable, enable, update, updateAll);
    1.63          Option modules = Option.withoutArgument(Option.NO_SHORT_NAME, "modules");
    1.64          both = OptionGroups.allOf(modules, oper);
    1.65          return both;
    1.66 @@ -155,6 +160,10 @@
    1.67      }
    1.68  
    1.69      protected void process(Env env, Map<Option, String[]> optionValues) throws CommandException {
    1.70 +        if (optionValues.containsKey(refresh)) {
    1.71 +            refresh(env);
    1.72 +        }
    1.73 +        
    1.74          if (optionValues.containsKey(list)) {
    1.75              listAllModules(env.getOutputStream());
    1.76          }
    1.77 @@ -178,12 +187,12 @@
    1.78          } catch (OperationException ex) {
    1.79              throw initCause(new CommandException(4), ex);
    1.80          }
    1.81 +        if (optionValues.containsKey(updateAll)) {
    1.82 +            updateAll(env);
    1.83 +        }
    1.84          if (optionValues.containsKey(update)) {
    1.85              updateModules(env, optionValues.get(update));
    1.86          }
    1.87 -        if (optionValues.containsKey(refresh)) {
    1.88 -            refresh(env);
    1.89 -        }
    1.90      }
    1.91  
    1.92      private void changeModuleState(String[] cnbs, boolean enable) throws IOException, CommandException, InterruptedException, OperationException {
    1.93 @@ -211,8 +220,61 @@
    1.94          support.doOperation(null);
    1.95      }
    1.96  
    1.97 -    private void updateModules(Env env, String[] pattern) throws CommandException {
    1.98 -        installModules(env, pattern, true);
    1.99 +    @NbBundle.Messages({
   1.100 +        "MSG_UpdateNoMatch=Nothing to update. The pattern {0} has no match among available updates.",
   1.101 +        "MSG_Update=Will update {0}@{1} to version {2}"
   1.102 +    })
   1.103 +    private void updateModules(Env env, String... pattern) throws CommandException {
   1.104 +        if (! initialized()) {
   1.105 +            refresh(env);
   1.106 +        }
   1.107 +        Pattern[] pats = findMatcher(env, pattern);
   1.108 +        
   1.109 +        List<UpdateUnit> units = UpdateManager.getDefault().getUpdateUnits();
   1.110 +        OperationContainer<InstallSupport> operate = OperationContainer.createForInternalUpdate();
   1.111 +        for (UpdateUnit uu : units) {
   1.112 +            if (uu.getInstalled() == null) {
   1.113 +                continue;
   1.114 +            }
   1.115 +            if (! uu.getInstalled().isEnabled()) {
   1.116 +                continue;
   1.117 +            }
   1.118 +            final List<UpdateElement> updates = uu.getAvailableUpdates();
   1.119 +            if (updates.isEmpty()) {
   1.120 +                continue;
   1.121 +            }
   1.122 +            if (pattern.length > 0 && !matches(uu.getCodeName(), pats)) {
   1.123 +                continue;
   1.124 +            }
   1.125 +            final UpdateElement ue = updates.get(0);
   1.126 +            env.getOutputStream().println(
   1.127 +                Bundle.MSG_Update(uu.getCodeName(), uu.getInstalled().getSpecificationVersion(), ue.getSpecificationVersion()
   1.128 +            ));
   1.129 +            if (operate.canBeAdded(uu, ue)) {
   1.130 +                LOG.fine("  ... update " + uu.getInstalled() + " -> " + ue);
   1.131 +                OperationInfo<InstallSupport> info = operate.add(ue);
   1.132 +                if (info != null) {
   1.133 +                    Set<UpdateElement> requiredElements = info.getRequiredElements();
   1.134 +                    LOG.fine("      ... add required elements: " + requiredElements);
   1.135 +                    operate.add(requiredElements);
   1.136 +                }
   1.137 +            }
   1.138 +        }
   1.139 +        final InstallSupport support = operate.getSupport();
   1.140 +        if (support == null) {
   1.141 +            env.getOutputStream().println(Bundle.MSG_UpdateNoMatch(pats == null ? null : Arrays.asList(pats)));
   1.142 +            return;
   1.143 +        }
   1.144 +        try {
   1.145 +            final Validator res1 = support.doDownload(null, null, false);
   1.146 +            Installer res2 = support.doValidate(res1, null);
   1.147 +            Restarter res3 = support.doInstall(res2, null);
   1.148 +            if (res3 != null) {
   1.149 +                support.doRestart(res3, null);
   1.150 +            }
   1.151 +        } catch (OperationException ex) {
   1.152 +            throw (CommandException)new CommandException(33, ex.getMessage()).initCause(ex);
   1.153 +        }
   1.154      }
   1.155  
   1.156      @NbBundle.Messages({
   1.157 @@ -242,21 +304,19 @@
   1.158          return false;
   1.159      }
   1.160  
   1.161 -    private void install(Env env, String[] pattern) throws CommandException {
   1.162 -        installModules(env, pattern, false);
   1.163 -    }
   1.164 -    
   1.165      @NbBundle.Messages({
   1.166          "MSG_Installing=Installing {0}@{1}",
   1.167 -        "MSG_InstallNoMatch=Cannot install. No match for {0}.",
   1.168 -        "MSG_UpdateNoMatch=Nothing to update. The pattern {0} has no match among available updates.",
   1.169 -        "MSG_Update=Will update {0}@{1} to version {2}"
   1.170 +        "MSG_InstallNoMatch=Cannot install. No match for {0}."
   1.171      })
   1.172 -    private void installModules(Env env, String[] pattern, boolean update) throws CommandException {
   1.173 +    private void install(Env env, String... pattern) throws CommandException {
   1.174 +        if (! initialized()) {
   1.175 +            refresh(env);
   1.176 +        }
   1.177 +
   1.178          Pattern[] pats = findMatcher(env, pattern);
   1.179  
   1.180          List<UpdateUnit> units = UpdateManager.getDefault().getUpdateUnits();
   1.181 -        OperationContainer<OperationSupport> operate = OperationContainer.createForDirectInstall();
   1.182 +        OperationContainer<InstallSupport> operate = OperationContainer.createForInstall();
   1.183          for (UpdateUnit uu : units) {
   1.184              if (uu.getInstalled() != null) {
   1.185                  continue;
   1.186 @@ -268,34 +328,41 @@
   1.187                  continue;
   1.188              }
   1.189              UpdateElement ue = uu.getAvailableUpdates().get(0);
   1.190 -            if (update) {
   1.191 -                env.getOutputStream().println(
   1.192 -                    Bundle.MSG_Update(uu.getCodeName(), uu.getInstalled().getSpecificationVersion(), ue.getSpecificationVersion()));
   1.193 -            } else {
   1.194 -                env.getOutputStream().println(
   1.195 +            env.getOutputStream().println(
   1.196                      Bundle.MSG_Installing(uu.getCodeName(), ue.getSpecificationVersion()));
   1.197 -            }
   1.198              operate.add(ue);
   1.199          }
   1.200 -        final OperationSupport support = operate.getSupport();
   1.201 +        final InstallSupport support = operate.getSupport();
   1.202          if (support == null) {
   1.203 -            if (update) {
   1.204 -                env.getOutputStream().println(Bundle.MSG_UpdateNoMatch(Arrays.asList(pats)));
   1.205 -            } else {
   1.206 -                env.getOutputStream().println(Bundle.MSG_InstallNoMatch(Arrays.asList(pats)));
   1.207 -            }
   1.208 +            env.getOutputStream().println(Bundle.MSG_InstallNoMatch(Arrays.asList(pats)));
   1.209              return;
   1.210          }
   1.211          try {
   1.212 -            Restarter restarter = support.doOperation(null);
   1.213 -            assert restarter == null;
   1.214 -            if (restarter != null) {
   1.215 -                support.doRestart(restarter, null);
   1.216 +            final Validator res1 = support.doDownload(null, null, false);
   1.217 +            Installer res2 = support.doValidate(res1, null);
   1.218 +            Restarter res3 = support.doInstall(res2, null);
   1.219 +            if (res3 != null) {
   1.220 +                support.doRestart(res3, null);
   1.221              }
   1.222          } catch (OperationException ex) {
   1.223 -            throw (CommandException) new CommandException(33, ex.getMessage()).initCause(ex);
   1.224 +            // a hack
   1.225 +            if (OperationException.ERROR_TYPE.INSTALL.equals(ex.getErrorType())) {
   1.226 +                // probably timeout of loading, don't report now
   1.227 +                env.getOutputStream().println(ex.getLocalizedMessage());
   1.228 +            } else {
   1.229 +                throw (CommandException) new CommandException(33, ex.getMessage()).initCause(ex);
   1.230 +            }
   1.231          }
   1.232      }
   1.233 +    
   1.234 +    private void updateAll(Env env) throws CommandException {
   1.235 +        updateModules(env);
   1.236 +    }
   1.237  
   1.238 +    private boolean initialized() {
   1.239 +        Preferences pref = NbPreferences.root ().node ("/org/netbeans/modules/autoupdate");
   1.240 +        long last = pref.getLong("lastCheckTime", -1);
   1.241 +        return last != -1;
   1.242 +    }    
   1.243 +    
   1.244  }
   1.245 -
     2.1 --- a/modulemanagement/src/org/netbeans/modules/modulemanagement/Status.java	Mon Aug 27 15:54:33 2012 +0200
     2.2 +++ b/modulemanagement/src/org/netbeans/modules/modulemanagement/Status.java	Tue Aug 28 22:07:58 2012 +0200
     2.3 @@ -87,7 +87,8 @@
     2.4      @NbBundle.Messages({
     2.5          "MSG_Enabled=Enabled",
     2.6          "MSG_Disabled=Installed",
     2.7 -        "MSG_UpdateAvailable=Upgrade to {0}"
     2.8 +        "MSG_UpdateAvailable=Upgrade to {0}",
     2.9 +        "MSG_Unknown=Unknown state"
    2.10      })
    2.11      private static Status installed(UpdateUnit uu) {
    2.12          assert uu.getInstalled() != null;
    2.13 @@ -97,7 +98,11 @@
    2.14          if (!updates.isEmpty()) {
    2.15              install = Bundle.MSG_UpdateAvailable(updates.get(0).getSpecificationVersion());
    2.16          } else {
    2.17 -            install = mi.isEnabled() ? Bundle.MSG_Enabled() : Bundle.MSG_Disabled();
    2.18 +            if (mi == null) {
    2.19 +                install = Bundle.MSG_Unknown();
    2.20 +            } else {
    2.21 +                install = mi.isEnabled() ? Bundle.MSG_Enabled() : Bundle.MSG_Disabled();
    2.22 +            }
    2.23          }
    2.24          
    2.25          return new Status(install);