Differenze tra le versioni di "Programmazione:Java/Eclipse RCP/Update by site"
Da WikiSitech.
Vai alla navigazioneVai alla ricerca (Nuova pagina: <code java>    try {       URL url = new URL("file:/D:/Work/workspace/desktop/org.rimondini.rcp.desktop.update/");       ISite site = org.eclipse.update.core.SiteManager.getSite(url, m...)  | 
				|||
| (5 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
| + | [[Programmazione:Java/Eclipse_RCP|<< Back to Eclipse RCP]]  | ||
| + | |||
| + | Questo esempio serve per mostrare come leggere le features e i plugin contenuti in esse da un Update Site e per aggiornare l'applicazione tramite questo site...  | ||
| + | |||
| + | '''NB: Questo codice è valido fino alla versione 3.3 di eclipse....dalla 3.4 la gestione dell'update è stata radicalmente cambiata'''  | ||
| + | |||
<code java>  | <code java>  | ||
| − |     try {  | + | public class Application extends DesktopApplication implements IRunnableWithProgress {  | 
| − |        URL url = new URL("file:/D:/Work/workspace/desktop/org.rimondini.rcp.desktop.update/");  | + |    protected boolean restart_application = false;  | 
| − | + | ||
| − | + |    @Override  | |
| − | + |     public Object start(IApplicationContext context) {  | |
| − | + |       restart_application = false;  | |
| − | + |       try {  | |
| − | + |          PlatformUI.createDisplay();  | |
| − | + |          ProgressManager.getInstance().run(false, true, this);  | |
| − | + |        } catch (InvocationTargetException e) {  | |
| − | + |          ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);  | |
| − | + |       } catch (InterruptedException e) {  | |
| − | + |          ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);  | |
| − | + |       }  | |
| − | + |       // se ho aggiornato qualche plugin riavvio l'applicazione  | |
| − | + |       return restart_application ? IApplication.EXIT_RESTART : super.start(context);  | |
| − | + |    }  | |
| − | + | ||
| − | + |    protected FWKApplicationWorkbenchAdvisor getApplicationWorkbenchAdvisor() {  | |
| − | + |       return new ApplicationWorkbenchAdvisor();  | |
| + |    }  | ||
| + | |||
| + |    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {  | ||
| + |       try {  | ||
| + |          URL url = new URL("file:/D:/Work/workspace/desktop/org.rimondini.rcp.desktop.update/");  | ||
| + |          // prendo il riferimento all'update site  | ||
| + |          ISite site = org.eclipse.update.core.SiteManager.getSite(url, monitor);  | ||
| + |          if (site != null) {  | ||
| + |             // leggo le features  | ||
| + |             ISiteFeatureReference[] r_features = site.getFeatureReferences();  | ||
| + |             if (r_features.length > 0) {  | ||
| + |                // prendo il riferimento al LocalSite (applicativo)  | ||
| + |                ILocalSite local = SiteManager.getLocalSite();  | ||
| + |                // scorro le configurazioni  | ||
| + |                for (int i = 0; i < local.getCurrentConfiguration().getConfiguredSites().length; i++) {  | ||
| + |                   IConfiguredSite c_site = local.getCurrentConfiguration().getConfiguredSites()[i];  | ||
| + |                   IFeatureReference[] c_features = c_site.getFeatureReferences();  | ||
| + |                   Map<String, IFeatureReference> m_features = new HashMap<String, IFeatureReference>();  | ||
| + |                   Map<String, IPluginEntry> m_plugins = new HashMap<String, IPluginEntry>();  | ||
| + |                   // estraggo l'elenco dei plugin dalle features  | ||
| + |                   for (int f = 0; f < c_features.length; f++) {  | ||
| + |                      IFeature feature = c_features[f].getFeature(new SubProgressMonitor(monitor, 3));  | ||
| + |                      InstallRegistry.registerFeature(feature);  | ||
| + |                      m_features.put(feature.getVersionedIdentifier().getIdentifier(), c_features[f]);  | ||
| + |                      IPluginEntry[] plugins = feature.getPluginEntries();  | ||
| + |                      for (int p = 0; p < plugins.length; p++) {  | ||
| + |                         InstallRegistry.registerPlugin(plugins[p]);  | ||
| + |                         m_plugins.put(plugins[p].getVersionedIdentifier().getIdentifier(), plugins[p]);  | ||
| + |                      }  | ||
                   }  |                    }  | ||
| − | + | ||
| − | + |                   // confronto le features locali con quelle dell'update site  | |
| − | + |                   for (int f = 0; f < r_features.length; f++) {  | |
| − | + |                      boolean install = false;  | |
| − | + |                      IFeature r_feature = r_features[f].getFeature(new SubProgressMonitor(monitor, 3));  | |
| − | + |                      if (m_features.containsKey(r_features[f].getVersionedIdentifier().getIdentifier())) {  | |
| − | + |                         IPluginEntry[] remote_plugins = r_feature.getPluginEntries();  | |
| − | + |                         for (int p = 0; p < remote_plugins.length; p++) {  | |
| − | + |                            // confronto i plugin delle features con quelli locali  | |
| − | + |                            IPluginEntry plugin = m_plugins.get(remote_plugins[p].getVersionedIdentifier().getIdentifier());  | |
| − | + |                            PluginVersionIdentifier remote_version = remote_plugins[p].getVersionedIdentifier().getVersion();  | |
| − | + |                            PluginVersionIdentifier plugin_version = plugingetVersionedIdentifier().getVersion();  | |
| − | + |                            if (plugin == null || remote_version.isGreaterThan(plugin_version)) {  | |
| − | + |                               // se il plugin in locale non esiste o ha una versione meno recente...lo installo  | |
| + |                               install = true;  | ||
| + |                               break;  | ||
| + |                            }  | ||
                         }  |                          }  | ||
| + |                      } else {  | ||
| + |                         // non esiste la feature  | ||
| + |                         install = true;  | ||
| + |                      }  | ||
| + |                      if (install) {  | ||
| + |                         // installo il plugin  | ||
| + |                         IOperationFactory o_fact = OperationsManager.getOperationFactory();  | ||
| + |                         IInstallFeatureOperation operation = o_fact.createInstallOperation(c_site,r_feature,null,null,null);  | ||
| + |                         operation.execute(new SubProgressMonitor(monitor, 3), null);  | ||
| + |                         OperationsManager.addPendingOperation(operation);  | ||
| + |                         restart_application = true;  | ||
                      }  |                       }  | ||
| − | |||
| − | |||
                   }  |                    }  | ||
| − | + |                }  | |
| − | + |                if (restart_application) {  | |
| − | + |                   // se ho installato almeno un plugin applico le modifiche e le salvo  | |
| − | + |                   OperationsManager.applyChangesNow();  | |
| − | + |                   if (!local.save()) {  | |
| − | + |                       ILoggerFactory.instance.getLogger(getClass()).error("Configuration saving failed");  | |
                   }  |                    }  | ||
                }  |                 }  | ||
             }  |              }  | ||
| − | + |          } else {  | |
| − | + |             ILoggerFactory.instance.getLogger(getClass()).error("Update site not found");  | |
| − | |||
| − | |||
| − | |||
| − | |||
          }  |           }  | ||
| − | + |        } catch (MalformedURLException e) {  | |
| − | + |          ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);  | |
| − | + |       } catch (CoreException e) {  | |
| − | + |          ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);  | |
| − | + |      }  | |
| − | + | ||
| − | + | }  | |
| − |     }  | + | |
</code>  | </code>  | ||
Versione attuale delle 09:28, 14 apr 2008
Questo esempio serve per mostrare come leggere le features e i plugin contenuti in esse da un Update Site e per aggiornare l'applicazione tramite questo site...
NB: Questo codice è valido fino alla versione 3.3 di eclipse....dalla 3.4 la gestione dell'update è stata radicalmente cambiata
public class Application extends DesktopApplication implements IRunnableWithProgress {
  protected boolean restart_application = false;
  @Override
  public Object start(IApplicationContext context) {
     restart_application = false;
     try {
        PlatformUI.createDisplay();
        ProgressManager.getInstance().run(false, true, this);
     } catch (InvocationTargetException e) {
        ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);
     } catch (InterruptedException e) {
        ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);
     }
     // se ho aggiornato qualche plugin riavvio l'applicazione
     return restart_application ? IApplication.EXIT_RESTART : super.start(context);
  }
  protected FWKApplicationWorkbenchAdvisor getApplicationWorkbenchAdvisor() {
     return new ApplicationWorkbenchAdvisor();
  }
  public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
     try {
        URL url = new URL("file:/D:/Work/workspace/desktop/org.rimondini.rcp.desktop.update/");
        // prendo il riferimento all'update site
        ISite site = org.eclipse.update.core.SiteManager.getSite(url, monitor);
        if (site != null) {
           // leggo le features
           ISiteFeatureReference[] r_features = site.getFeatureReferences();
           if (r_features.length > 0) {
              // prendo il riferimento al LocalSite (applicativo)
              ILocalSite local = SiteManager.getLocalSite();
              // scorro le configurazioni
              for (int i = 0; i < local.getCurrentConfiguration().getConfiguredSites().length; i++) {
                 IConfiguredSite c_site = local.getCurrentConfiguration().getConfiguredSites()[i];
                 IFeatureReference[] c_features = c_site.getFeatureReferences();
                 Map<String, IFeatureReference> m_features = new HashMap<String, IFeatureReference>();
                 Map<String, IPluginEntry> m_plugins = new HashMap<String, IPluginEntry>();
                 // estraggo l'elenco dei plugin dalle features
                 for (int f = 0; f < c_features.length; f++) {
                    IFeature feature = c_features[f].getFeature(new SubProgressMonitor(monitor, 3));
                    InstallRegistry.registerFeature(feature);
                    m_features.put(feature.getVersionedIdentifier().getIdentifier(), c_features[f]);
                    IPluginEntry[] plugins = feature.getPluginEntries();
                    for (int p = 0; p < plugins.length; p++) {
                       InstallRegistry.registerPlugin(plugins[p]);
                       m_plugins.put(plugins[p].getVersionedIdentifier().getIdentifier(), plugins[p]);
                    }
                 }
                 
                 // confronto le features locali con quelle dell'update site
                 for (int f = 0; f < r_features.length; f++) {
                    boolean install = false;
                    IFeature r_feature = r_features[f].getFeature(new SubProgressMonitor(monitor, 3));
                    if (m_features.containsKey(r_features[f].getVersionedIdentifier().getIdentifier())) {
                       IPluginEntry[] remote_plugins = r_feature.getPluginEntries();
                       for (int p = 0; p < remote_plugins.length; p++) {
                          // confronto i plugin delle features con quelli locali
                          IPluginEntry plugin = m_plugins.get(remote_plugins[p].getVersionedIdentifier().getIdentifier());
                          PluginVersionIdentifier remote_version = remote_plugins[p].getVersionedIdentifier().getVersion();
                          PluginVersionIdentifier plugin_version = plugingetVersionedIdentifier().getVersion();
                          if (plugin == null || remote_version.isGreaterThan(plugin_version)) {
                             // se il plugin in locale non esiste o ha una versione meno recente...lo installo
                             install = true;
                             break;
                          }
                       }
                    } else {
                       // non esiste la feature
                       install = true;
                    }
                    if (install) {
                       // installo il plugin
                       IOperationFactory o_fact = OperationsManager.getOperationFactory();
                       IInstallFeatureOperation operation = o_fact.createInstallOperation(c_site,r_feature,null,null,null);
                       operation.execute(new SubProgressMonitor(monitor, 3), null);
                       OperationsManager.addPendingOperation(operation);
                       restart_application = true;
                    }
                 }
              }
              if (restart_application) {
                 // se ho installato almeno un plugin applico le modifiche e le salvo
                 OperationsManager.applyChangesNow();
                 if (!local.save()) {
                    ILoggerFactory.instance.getLogger(getClass()).error("Configuration saving failed");
                 }
              }
           }
        } else {
           ILoggerFactory.instance.getLogger(getClass()).error("Update site not found");
        }
     } catch (MalformedURLException e) {
        ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);
     } catch (CoreException e) {
        ILoggerFactory.instance.getLogger(getClass()).error("Update error", e);
    }
  
}