2016-12-01 55 views
1

我在Eclipse E4應用程序中添加了更新功能。在此我使用Lars Vogel的源代碼和tutorial。當我測試我的應用程序時,ProvisioningJob始終爲空。它在運行到Eclipse時應該只有null。但是,當我嘗試更新導出的應用程序時,ProvisioningJob仍爲空。我做錯了什麼?使用p2更新Eclipse E4應用程序

public class UpdateHandler { 

private static final String REPOSITORY_LOC = System.getProperty("UpdateHandler.Repo", 
     "file:////updateServer/repository"); 

@Execute 
public void execute(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync, 
     final IWorkbench workbench) { 
    Job updateJob = new Job("Update Job") { 
     @Override 
     protected IStatus run(final IProgressMonitor monitor) { 
      return checkForUpdates(agent, shell, sync, workbench, monitor); 
     } 
    }; 
    updateJob.schedule(); 
} 

private IStatus checkForUpdates(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync, 
     final IWorkbench workbench, IProgressMonitor monitor) { 

    // configure update operation 
    final ProvisioningSession session = new ProvisioningSession(agent); 
    final UpdateOperation operation = new UpdateOperation(session); 
    configureUpdate(operation); 

    // check for updates, this causes I/O 
    final IStatus status = operation.resolveModal(monitor); 

    // failed to find updates (inform user and exit) 
    if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) { 
     LogModule.log(LogLevel.INFO, "No updated has been found"); 
     showMessage(shell, sync); 
     return Status.CANCEL_STATUS; 
    } 
    else 
    { 
     LogModule.log(LogLevel.INFO, "Updates are found"); 
    } 

    // run installation 
    final ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor); 

    // updates cannot run from within Eclipse IDE!!! 
    if (provisioningJob == null) { 
     System.err.println("Trying to update from the Eclipse IDE? This won't work!"); 
     LogModule.log(LogLevel.WARNING, "Trying to update from the Eclipse IDE? This won't work!"); 
     return Status.CANCEL_STATUS; 
    } 
    configureProvisioningJob(provisioningJob, shell, sync, workbench); 

    //provisioningJob.schedule(); 
    provisioningJob.run(monitor); 
    return Status.OK_STATUS; 

} 

private void configureProvisioningJob(ProvisioningJob provisioningJob, final Shell shell, final UISynchronize sync, 
     final IWorkbench workbench) { 

    // register a job change listener to track 
    // installation progress and notify user upon success 
    provisioningJob.addJobChangeListener(new JobChangeAdapter() { 
     @Override 
     public void done(IJobChangeEvent event) { 
      //if (event.getResult().isOK()) { 
       sync.syncExec(new Runnable() { 

        @Override 
        public void run() { 

         LogModule.log(LogLevel.INFO, "Update ready to install"); 

         boolean restart = MessageDialog.openQuestion(shell, "Updates installed, restart?", 
           "Updates have been installed. Do you want to restart?"); 
         if (restart) { 
          workbench.restart(); 
         } 
        } 
       }); 

     // } 
      super.done(event); 
     } 
    }); 

} 

private void showMessage(final Shell parent, final UISynchronize sync) { 
    sync.syncExec(new Runnable() { 

     @Override 
     public void run() { 
      MessageDialog.openWarning(parent, "No update", 
        "No updates for the current installation have been found."); 
     } 
    }); 
} 

private UpdateOperation configureUpdate(final UpdateOperation operation) { 
    // create uri and check for validity 
    URI uri = null; 
    try { 
     uri = new URI(REPOSITORY_LOC); 
    } catch (final URISyntaxException e) { 
     System.err.println(e.getMessage()); 
     LogModule.log(LogLevel.ERROR, e.getMessage()); 
     return null; 
    } 

    // set location of artifact and metadata repo 
    operation.getProvisioningContext().setArtifactRepositories(new URI[] { uri }); 
    operation.getProvisioningContext().setMetadataRepositories(new URI[] { uri }); 
    return operation; 
} 

}

+0

你的問題怎麼樣?我遇到了同樣的問題。代碼運行到這個分支:if(status.getCode()== UpdateOperation.STATUS_NOTHING_TO_UPDATE){ – aviit

+0

我停止看這個。做這項工作的努力很高。我們使用外部更新程序來創建我們在Inno setup中創建的應用程序。 – JimmyD

回答

1

P2在內部使用了大量的服務和那些沒有被明確作爲捆綁依賴引用。所以你可能會錯過那些額外需要的服務。通過PDE啓動中的「添加所需...」添加它們不起作用。 確保您的產品或產品確實包含所有要求。我將從org.eclipse.equinox.p2.sdk的內容開始。這應該肯定有效。之後,您可以嘗試將需求降至org.eclipse.equinox.p2.core.feature甚至更​​低。

+0

我沒有org.eclipse.equinox.p2.sdk。我只能找到org.eclipse.equinox.p2.ui.sdk。 – JimmyD

+0

您是否使用[Eclipse目標](http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fconcepts%2Ftarget.htm)? 如果是這樣,那麼您應該能夠從更新站點http://下載中選擇IU功能'org.eclipse.equinox.p2.sdk(.feature.group)'或名稱「Equinox p2,SDK」 .eclipse.org/releases/neon /' 確保取消選擇「按類別分組」以查看IU名稱,而不僅僅是p2類別。 –

相關問題