Révision 2218

tmp/org.txm.rcp/src/main/java/org/txm/rcp/p2/plugins/TXMUpdateHandler.java (revision 2218)
14 14
import java.net.URI;
15 15
import java.net.URL;
16 16
import java.util.Arrays;
17
import java.util.Iterator;
17 18

  
18 19
import org.eclipse.core.commands.ExecutionEvent;
19 20
import org.eclipse.core.runtime.Assert;
20 21
import org.eclipse.core.runtime.IProgressMonitor;
21 22
import org.eclipse.core.runtime.IStatus;
22 23
import org.eclipse.core.runtime.OperationCanceledException;
24
import org.eclipse.equinox.internal.p2.engine.Profile;
25
import org.eclipse.equinox.internal.p2.engine.SimpleProfileRegistry;
26
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
23 27
import org.eclipse.equinox.internal.p2.ui.sdk.ProvSDKMessages;
24 28
import org.eclipse.equinox.internal.p2.ui.sdk.UpdateHandler;
25 29
import org.eclipse.equinox.p2.core.IProvisioningAgent;
26 30
import org.eclipse.equinox.p2.core.ProvisionException;
31
import org.eclipse.equinox.p2.engine.IProfile;
27 32
import org.eclipse.equinox.p2.engine.IProfileRegistry;
33
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
28 34
import org.eclipse.equinox.p2.operations.ProfileChangeOperation;
29 35
import org.eclipse.equinox.p2.operations.RepositoryTracker;
30 36
import org.eclipse.equinox.p2.operations.UpdateOperation;
31 37
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
32 38
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
33 39
import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
40
import org.eclipse.equinox.p2.ui.ProvisioningUI;
34 41
import org.eclipse.osgi.util.NLS;
35 42
import org.eclipse.swt.widgets.Shell;
36 43
import org.eclipse.ui.statushandlers.StatusManager;
......
62 69
					File installDirectory = new File(path);
63 70
					Log.fine("Testing install directory rights: "+installDirectory);
64 71
					if (!installDirectory.canWrite() || !installDirectory.canExecute()) {
65
						Log.warning("Warning: you need administrator privileges to fully update TXM. Some updates might not be fully installed.");
72
						Log.warning(NLS.bind("Warning: you need administrator privileges to fully update TXM (installed in {0}). Some updates might not be fully installed.", installDirectory));
66 73
					}
67 74
				} catch (Exception e) {
68 75
					// TODO Auto-generated catch block
......
71 78
			}
72 79
		}
73 80

  
74
		// Look for a profile.  We may not immediately need it in the
75
		// handler, but if we don't have one, whatever we are trying to do
76
		// will ultimately fail in a more subtle/low-level way.  So determine
77
		// up front if the system is configured properly.
81
		// Hacking p2 profile to be able to update installation directory even if -configuration is set
78 82
		try {
79
			String profileId = getProvisioningUI().getProfileId();
80
			IProvisioningAgent agent = getProvisioningUI().getSession().getProvisioningAgent();
83
			ProvisioningUI pui = ProvisioningUI.getDefaultUI();
81 84

  
85
			String profileId = pui.getProfileId();
86
			IProvisioningAgent agent = pui.getSession().getProvisioningAgent();
87

  
82 88
			if (agent != null) {
83
				IProfileRegistry registry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
84
				if (registry != null) {
85
					registry.getProfile(profileId);
89
				IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
90

  
91

  
92
				if (profileRegistry != null && profileRegistry instanceof SimpleProfileRegistry) {
93

  
94
					SimpleProfileRegistry spr = (SimpleProfileRegistry)profileRegistry;
95

  
96
					IProfile iprofile = profileRegistry.getProfile(profileId);
97
					if (iprofile != null && iprofile instanceof Profile) {
98
						Profile profile = (Profile)iprofile;
99

  
100
						spr.lockProfile(profile);
101

  
102
						//System.out.println("Profile: "+profile.getClass()+" = "+profile);
103
						Iterator<IInstallableUnit> everything = profile.everything();
104
						boolean changed = false;
105
						while(everything.hasNext()) {
106
							IInstallableUnit iu = everything.next();
107

  
108
							String locked = profile.getInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU);
109

  
110
							if (locked != null && !("0".equals(locked))) {
111
								profile.setInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU, "0");
112
								changed = true;
113
							}
114
						}
115
						if (changed) {
116
							spr.updateProfile(profile);
117
						}
118

  
119
						spr.unlockProfile(profile);									
120
					}
86 121
				}
87 122
			}
88 123

  
......
105 140
			Log.info("Looking for TXM updates...");
106 141
			Object ret = super.execute(event);
107 142
			Log.info("Done.");
108
			
143

  
109 144
			return ret;
110 145
		} catch(Exception e) {
111 146
			Log.severe("Could not update TXM: "+e + ".");
......
124 159
	}
125 160

  
126 161
	public static boolean patchGZProfile() {
127
		//TODO: is this still useful with TXM 0.7.7 ? 
128
		String path = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
162
		//TODO: is this still useful with TXM 0.7.7  -> yes&no : need to patch when --configuration is set, done by editing the Profile object directly
163
		String path = BundleUtils.getInstallDirectory();//System.getProperty("osgi.instance.area"); //$NON-NLS-1$
129 164
		URL location = null;
130 165
		try {
131 166
			location = new URL(path); //$NON-NLS-1$
......
154 189
			return false;
155 190
		} else {
156 191
			try {
157
				//2- extract profil from gz profile
192
				//2- extract profile from gz profile
158 193
				File profileXMLFile = GZip.uncompress(gzFile, profileDir);
159 194
				//3- patch xml by removing lock properties
160 195
				patchProfile(profileXMLFile);
......
332 367
			System.out.println("No artifact repository manager found");
333 368
			return false;
334 369
		}
335
		
370

  
336 371
		Log.fine("Adding update site URL: "+location + "...");
337 372

  
338 373
		try {
......
435 470
	}
436 471

  
437 472
	// COPY FROM UpdateHandler class
438
	boolean hasNoRepos = false;
439
	UpdateOperation operation;
473
	//	boolean hasNoRepos = false;
474
	//	UpdateOperation operation;
475
	//
476
	//	@Override
477
	//	protected void doExecute(LoadMetadataRepositoryJob job) {
478
	//		super.doExecute(job);
479
	////		//System.out.println("TXMUpdateHandler: doExecute(...)");
480
	////		if (hasNoRepos) {
481
	////			if (getProvisioningUI().getPolicy().getRepositoriesVisible()) {
482
	////				System.out.println(ProvSDKMessages.UpdateHandler_NoSitesMessage);
483
	////			}
484
	////			return;
485
	////		}
486
	////		// Report any missing repositories.
487
	////		job.reportAccumulatedStatus();
488
	////		if (continueWorkingWithOperation(operation, getShell())) {
489
	////			if (operation.getResolutionResult() == Status.OK_STATUS) {
490
	////				getProvisioningUI().openUpdateWizard(false, operation, job);
491
	////			} else {
492
	////
493
	////				final RemediationOperation remediationOperation = new RemediationOperation(getProvisioningUI().getSession(), operation.getProfileChangeRequest(), RemedyConfig.getCheckForUpdateRemedyConfigs());
494
	////				ProvisioningJob job2 = new ProvisioningJob(ProvSDKMessages.RemediationOperation_ResolveJobName, getProvisioningUI().getSession()) {
495
	////					@Override
496
	////					public IStatus runModal(IProgressMonitor monitor) {
497
	////						monitor.beginTask(ProvSDKMessages.RemediationOperation_ResolveJobTask, RemedyConfig.getAllRemedyConfigs().length);
498
	////						return remediationOperation.resolveModal(monitor);
499
	////					}
500
	////				};
501
	////				job2.addJobChangeListener(new JobChangeAdapter() {
502
	////					public void done(IJobChangeEvent event) {
503
	////						if (PlatformUI.isWorkbenchRunning()) {
504
	////							PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
505
	////								public void run() {
506
	////									getProvisioningUI().openUpdateWizard(true, operation, remediationOperation, null);
507
	////								}
508
	////							});
509
	////						}
510
	////					}
511
	////
512
	////				});
513
	////				getProvisioningUI().schedule(job2, StatusManager.SHOW | StatusManager.LOG);
514
	////			}
515
	////		}
516
	//	}
517
	//
518
	//	/**
519
	//	 * Answer a boolean indicating whether the caller should continue to work with the
520
	//	 * specified operation.  This method is used when an operation has been resolved, but
521
	//	 * the UI may have further restrictions on continuing with it.
522
	//	 * 
523
	//	 * @param operation the operation in question.  It must already be resolved.
524
	//	 * @param shell the shell to use for any interaction with the user
525
	//	 * @return <code>true</code> if processing of the operation should continue, <code>false</code> if
526
	//	 * not.  It is up to the implementor to report any errors to the user when answering <code>false</code>.
527
	//	 */
528
	//	public boolean continueWorkingWithOperation(ProfileChangeOperation operation, Shell shell) {
529
	//		//System.out.println("Test if any update available");
530
	//		Assert.isTrue(operation.getResolutionResult() != null);
531
	//		IStatus status = operation.getResolutionResult();
532
	//		// user cancelled
533
	//		if (status.getSeverity() == IStatus.CANCEL)
534
	//			return false;
535
	//
536
	//		// Special case those statuses where we would never want to open a wizard
537
	//		if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
538
	//			//ProvUI.reportStatus(status, StatusManager.BLOCK);
539
	//			//System.out.println("No update available.");
540
	//			return false;
541
	//		}
542
	//
543
	//		// there is no plan, so we can't continue.  Report any reason found
544
	//		if (operation.getProvisioningPlan() == null && !status.isOK()) {
545
	//			StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW);
546
	//
547
	//			return false;
548
	//		}
549
	//
550
	//		// Allow the wizard to open otherwise.
551
	//		return true;
552
	//	}
553
	//
554
	//	protected void doPostLoadBackgroundWork(IProgressMonitor monitor) throws OperationCanceledException {
555
	//		operation = getProvisioningUI().getUpdateOperation(null, null);
556
	//		// check for updates
557
	//		IStatus resolveStatus = operation.resolveModal(monitor);
558
	//		if (resolveStatus.getSeverity() == IStatus.CANCEL)
559
	//			throw new OperationCanceledException();
560
	//	}
561
	//
562
	//	protected boolean preloadRepositories() {
563
	//		hasNoRepos = false;
564
	//		RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
565
	//		if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
566
	//			hasNoRepos = true;
567
	//			return false;
568
	//		}
569
	//		return super.preloadRepositories();
570
	//	}
571
	//
572
	//	@Override
573
	//	protected String getProgressTaskName() {
574
	//		return ProvSDKMessages.UpdateHandler_ProgressTaskName;
575
	//	}
440 576

  
441
	@Override
442
	protected void doExecute(LoadMetadataRepositoryJob job) {
443
		super.doExecute(job);
444
//		//System.out.println("TXMUpdateHandler: doExecute(...)");
445
//		if (hasNoRepos) {
446
//			if (getProvisioningUI().getPolicy().getRepositoriesVisible()) {
447
//				System.out.println(ProvSDKMessages.UpdateHandler_NoSitesMessage);
448
//			}
449
//			return;
450
//		}
451
//		// Report any missing repositories.
452
//		job.reportAccumulatedStatus();
453
//		if (continueWorkingWithOperation(operation, getShell())) {
454
//			if (operation.getResolutionResult() == Status.OK_STATUS) {
455
//				getProvisioningUI().openUpdateWizard(false, operation, job);
456
//			} else {
457
//
458
//				final RemediationOperation remediationOperation = new RemediationOperation(getProvisioningUI().getSession(), operation.getProfileChangeRequest(), RemedyConfig.getCheckForUpdateRemedyConfigs());
459
//				ProvisioningJob job2 = new ProvisioningJob(ProvSDKMessages.RemediationOperation_ResolveJobName, getProvisioningUI().getSession()) {
460
//					@Override
461
//					public IStatus runModal(IProgressMonitor monitor) {
462
//						monitor.beginTask(ProvSDKMessages.RemediationOperation_ResolveJobTask, RemedyConfig.getAllRemedyConfigs().length);
463
//						return remediationOperation.resolveModal(monitor);
464
//					}
465
//				};
466
//				job2.addJobChangeListener(new JobChangeAdapter() {
467
//					public void done(IJobChangeEvent event) {
468
//						if (PlatformUI.isWorkbenchRunning()) {
469
//							PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
470
//								public void run() {
471
//									getProvisioningUI().openUpdateWizard(true, operation, remediationOperation, null);
472
//								}
473
//							});
474
//						}
475
//					}
476
//
477
//				});
478
//				getProvisioningUI().schedule(job2, StatusManager.SHOW | StatusManager.LOG);
479
//			}
480
//		}
481
	}
482 577

  
483
	/**
484
	 * Answer a boolean indicating whether the caller should continue to work with the
485
	 * specified operation.  This method is used when an operation has been resolved, but
486
	 * the UI may have further restrictions on continuing with it.
487
	 * 
488
	 * @param operation the operation in question.  It must already be resolved.
489
	 * @param shell the shell to use for any interaction with the user
490
	 * @return <code>true</code> if processing of the operation should continue, <code>false</code> if
491
	 * not.  It is up to the implementor to report any errors to the user when answering <code>false</code>.
492
	 */
493
	public boolean continueWorkingWithOperation(ProfileChangeOperation operation, Shell shell) {
494
		//System.out.println("Test if any update available");
495
		Assert.isTrue(operation.getResolutionResult() != null);
496
		IStatus status = operation.getResolutionResult();
497
		// user cancelled
498
		if (status.getSeverity() == IStatus.CANCEL)
499
			return false;
500 578

  
501
		// Special case those statuses where we would never want to open a wizard
502
		if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
503
			//ProvUI.reportStatus(status, StatusManager.BLOCK);
504
			//System.out.println("No update available.");
505
			return false;
506
		}
507

  
508
		// there is no plan, so we can't continue.  Report any reason found
509
		if (operation.getProvisioningPlan() == null && !status.isOK()) {
510
			StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW);
511

  
512
			return false;
513
		}
514

  
515
		// Allow the wizard to open otherwise.
516
		return true;
517
	}
518

  
519
	protected void doPostLoadBackgroundWork(IProgressMonitor monitor) throws OperationCanceledException {
520
		operation = getProvisioningUI().getUpdateOperation(null, null);
521
		// check for updates
522
		IStatus resolveStatus = operation.resolveModal(monitor);
523
		if (resolveStatus.getSeverity() == IStatus.CANCEL)
524
			throw new OperationCanceledException();
525
	}
526

  
527
	protected boolean preloadRepositories() {
528
		hasNoRepos = false;
529
		RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
530
		if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
531
			hasNoRepos = true;
532
			return false;
533
		}
534
		return super.preloadRepositories();
535
	}
536

  
537
	@Override
538
	protected String getProgressTaskName() {
539
		return ProvSDKMessages.UpdateHandler_ProgressTaskName;
540
	}
541

  
542

  
543

  
544 579
}

Formats disponibles : Unified diff