Home » Eclipse Projects » Equinox » Headless 'Install new Software'
| | | |
Re: Headless 'Install new Software' [message #509874 is a reply to message #507553] |
Mon, 25 January 2010 16:16 |
mirko fucci Messages: 10 Registered: July 2009 |
Junior Member |
|
|
i've found a solution, i think it's a very trivial code portion for p2 programmers but i've spent too much time to find needed informations and write it ... i'm surprised i didn't get any help.
I hope this code will be useful who need to migrate from old rcp features update to new one (p2).
package org.eclipse.equinox.p2.custom;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.eclipse.equinox.internal.p2.ui.PlanAnalyzer;
import org.eclipse.equinox.internal.p2.ui.ProvUIActivator;
import org.eclipse.equinox.internal.p2.ui.ProvUIMessages;
import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionEx ception;
import org.eclipse.equinox.internal.provisional.p2.director.Profile ChangeRequest;
import org.eclipse.equinox.internal.provisional.p2.engine.IProfile;
import org.eclipse.equinox.internal.provisional.p2.engine.IProfileR egistry;
import org.eclipse.equinox.internal.provisional.p2.engine.Provision ingContext;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstal lableUnit;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.I nstallableUnitQuery;
import org.eclipse.equinox.internal.provisional.p2.metadata.reposit ory.IMetadataRepository;
import org.eclipse.equinox.internal.provisional.p2.metadata.reposit ory.IMetadataRepositoryManager;
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
import org.eclipse.equinox.internal.provisional.p2.ui.IStatusCodes;
import org.eclipse.equinox.internal.provisional.p2.ui.ProvUI;
import org.eclipse.equinox.internal.provisional.p2.ui.ProvisioningO perationRunner;
import org.eclipse.equinox.internal.provisional.p2.ui.actions.Insta llAction;
import org.eclipse.equinox.internal.provisional.p2.ui.operations.Pl annerResolutionOperation;
import org.eclipse.equinox.internal.provisional.p2.ui.operations.Pr ofileModificationOperation;
import org.eclipse.equinox.p2.autoupdate.internal.Activator;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.statushandlers.StatusManager;
@SuppressWarnings({"restriction","unchecked"})
public class HeadlessInstallNew {
private String siteAddress = "";
private String profileId = "";
private IInstallableUnit[] localUnits = null;
private IInstallableUnit[] remoteUnits = null;
public HeadlessInstallNew(String siteAddress, String profileId){
this.siteAddress = siteAddress;
this.profileId = profileId;
}
public HeadlessInstallNew(String siteAddress){
this.siteAddress = siteAddress;
//this.profileId = profileId;
}
private boolean installedNew = false;
public boolean installNewFeatures(){
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
loadLocalUnits(monitor);
try {
loadRemoteUnits(monitor);
} catch (ProvisionException e1) {
//e1.printStackTrace();
return;
} catch (URISyntaxException e1) {
//e1.printStackTrace();
return;
}
List featToinstall = new ArrayList();
boolean foundNew = false;
//IPlanner planner = null;
for (int i=0;i<remoteUnits.length;i++){
if (!isInstalled(remoteUnits[i])){
//planner = (IPlanner) ServiceHelper.getService(Activator.getContext(), IPlanner.class.getName());
//if (planner == null)
// return;
featToinstall.add(remoteUnits[i]);
foundNew=true;
log("NEW : " + remoteUnits[i].getId());
}
}
if (foundNew){
log("SI PROCEDE ALL'INSTALLAZIONE");
PlannerResolutionOperation resolutionOperation = null;
MultiStatus status = PlanAnalyzer.getProfileChangeAlteredStatus();
log("STEP 1");
ProfileChangeRequest request = computeProfileChangeRequest(featToinstall.toArray(), status, monitor);
log("STEP 2"+request);
ProvisioningContext provisioningContext = new ProvisioningContext();
if (request != null) {
resolutionOperation = new PlannerResolutionOperation(ProvUIMessages.ProfileModificatio nWizardPage_ResolutionOperationLabel, profileId, request, provisioningContext, status, false);
log("STEP 3");
try {
resolutionOperation.execute(monitor);
log("STEP 4");
} catch (ProvisionException e) {
log(e);
ProvUI.handleException(e, null, StatusManager.SHOW | StatusManager.LOG);
resolutionOperation = null;
//couldNotResolve = true;
IStatus status1 = new MultiStatus(ProvUIActivator.PLUGIN_ID, IStatusCodes.UNEXPECTED_NOTHING_TO_DO, "Unresolved", null);
StatusManager.getManager().handle(status1, StatusManager.LOG);
}
ProfileModificationOperation op = createProfileModificationOperation(resolutionOperation);
log("STEP 5");
try{
ProvisioningOperationRunner.schedule(op, /*StatusManager.SHOW |*/ StatusManager.LOG);
log("STEP 6");
installedNew = true;
} catch (Exception e) {
log(e);
e.printStackTrace();
}
}
}
}
};
try {
new ProgressMonitorDialog(null).run(true, true, runnable);
} catch (InvocationTargetException e) {
e.printStackTrace();
log(e);
return installedNew;
} catch (InterruptedException e) {
log(e);
return installedNew;
}
return installedNew;
}
private ProfileModificationOperation createProfileModificationOperation(PlannerResolutionOperatio n op) {
return new ProfileModificationOperation(getOperationLabel(), profileId, op.getProvisioningPlan(), op.getProvisioningContext());
}
protected ProfileChangeRequest computeProfileChangeRequest(Object[] selectedElements, MultiStatus additionalStatus, IProgressMonitor monitor) {
IInstallableUnit[] selected = ElementUtils.elementsToIUs(selectedElements);
return InstallAction.computeProfileChangeRequest(selected, profileId, additionalStatus, monitor);
}
protected String getOperationLabel() {
return ProvUIMessages.InstallIUOperationLabel;
}
private boolean isInstalled(IInstallableUnit iInstallableUnit) {
boolean found = false;
for (int i=0;i<localUnits.length;i++){
if (localUnits[i].getId().equalsIgnoreCase(iInstallableUnit.get Id())){
found=true;
}
}
return found;
}
private void loadLocalUnits(IProgressMonitor monitor) {
monitor.beginTask("Analisi delle feature installate", 20);
IProfileRegistry profileRegistry = (IProfileRegistry) ServiceHelper.getService(Activator.getContext(), IProfileRegistry.class.getName());
if (profileRegistry == null)
return;
IProfile profs[] = (IProfile[]) profileRegistry.getProfiles();
IProfile profile = profs[0];
this.profileId = profile.getProfileId();
log("PROFILE ID : " + profileId);
//profileRegistry.getProfile("SDKProfile");
//getProfile(IProfileRegistry.SELF);
//profileId
//IProfile profile = profileRegistry.getProfile(IProfileRegistry.SELF);
if (profile == null)
return;
Collector localCollector = profile.query(InstallableUnitQuery.ANY, new Collector(), null);
if (localCollector.isEmpty())
return;
localUnits = new IInstallableUnit[localCollector.size()];
Iterator it = localCollector.iterator();
int i=0;
while (it.hasNext()){
IInstallableUnit unit = (IInstallableUnit) it.next();
localUnits[i++] = unit;
if (unit.getId().endsWith("feature.group"))
log("LOCAL : " + unit.getId()+ "-" + unit.getVersion());
}
}
private void loadRemoteUnits(IProgressMonitor monitor) throws ProvisionException, URISyntaxException {
monitor.beginTask("Analisi delle feature remote", 20);
IMetadataRepositoryManager metadataRepositoryManager = getMetadataRepositoryManager();
IMetadataRepository repo = metadataRepositoryManager.loadRepository(new URI(siteAddress), null);
List list = new ArrayList();
IInstallableUnit unit = null;
Collector collector = repo.query(InstallableUnitQuery.ANY, new Collector(), null);
Iterator it = collector.iterator();
int i = 0;
while (it.hasNext()){
unit = (IInstallableUnit) it.next();
if (unit.getId().endsWith("feature.group")){
log("REMOTE " + unit.getId() + "-" + unit.getVersion());
i++;
list.add(unit);
//remoteUnits[i++]=unit;
}
}
remoteUnits = new IInstallableUnit[i];
list.toArray(remoteUnits);
}
protected static IMetadataRepositoryManager getMetadataRepositoryManager() {
return (IMetadataRepositoryManager) ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName());
}
private void log (Exception e){
WorkbenchPlugin.log(
new Status(Status.ERROR,
Activator.getContext().getBundle().getSymbolicName(),
Status.ERROR, e.getMessage(), e));
}
private void log (String message){
WorkbenchPlugin.log(new Status(Status.OK,
Activator.getContext().getBundle().getSymbolicName(),
Status.OK, message, null));
}
}
|
|
| | | |
Goto Forum:
Current Time: Fri Nov 08 19:29:40 GMT 2024
Powered by FUDForum. Page generated in 0.04797 seconds
|