Révision 2970

tmp/org.txm.rcp/src/main/java/org/txm/rcp/p2/plugins/TXMUpdateHandler.java (revision 2970)
62 62
import static java.util.prefs.Preferences.systemRoot;
63 63

  
64 64
public class TXMUpdateHandler extends UpdateHandler {
65

  
65
	
66 66
	public static final String ID = "org.txm.rcp.p2.plugins.TXMUpdateHandler";
67

  
67
	
68 68
	/**
69 69
	 * Execute the command.
70 70
	 */
71 71
	@Override
72 72
	public Object execute(ExecutionEvent event) {
73

  
73
		
74 74
		if (OSDetector.isFamilyWindows() && !isWindowsAdministratorUser()) {
75 75
			TXMMessageBox.show(HandlerUtil.getActiveShell(event), TXMUIMessages.updateWarning, TXMUIMessages.abortingWindowsUpdate, SWT.ICON_WARNING);
76 76
			Log.warning("Update canceled");
77 77
			return null;
78 78
		}
79

  
79
		
80 80
		String key = event.getParameter("org.txm.rcp.parameters.update"); //$NON-NLS-1$
81 81
		if ("true".equals(key)) {
82 82
			String path = BundleUtils.getInstallDirectory();
......
90 90
								installDirectory));
91 91
						return null;
92 92
					}
93
				} catch (Exception e) {
93
				}
94
				catch (Exception e) {
94 95
					// TODO Auto-generated catch block
95 96
					e.printStackTrace();
96 97
				}
97 98
			}
98 99
		}
99

  
100
		
100 101
		// Hacking p2 profile to be able to update installation directory even if
101 102
		// -configuration is set
102 103
		try {
103 104
			ProvisioningUI pui = ProvisioningUI.getDefaultUI();
104

  
105
			
105 106
			String profileId = pui.getProfileId();
106 107
			IProvisioningAgent agent = pui.getSession().getProvisioningAgent();
107

  
108
			
108 109
			if (agent != null) {
109 110
				IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
110

  
111
				
111 112
				if (profileRegistry != null && profileRegistry instanceof SimpleProfileRegistry) {
112

  
113
					
113 114
					SimpleProfileRegistry spr = (SimpleProfileRegistry) profileRegistry;
114

  
115
					
115 116
					IProfile iprofile = profileRegistry.getProfile(profileId);
116 117
					if (iprofile != null && iprofile instanceof Profile) {
117 118
						Profile profile = (Profile) iprofile;
118

  
119
						
119 120
						spr.lockProfile(profile);
120

  
121
						
121 122
						// System.out.println("Profile: "+profile.getClass()+" = "+profile);
122 123
						Iterator<IInstallableUnit> everything = profile.everything();
123 124
						boolean changed = false;
124 125
						while (everything.hasNext()) {
125 126
							IInstallableUnit iu = everything.next();
126 127
							String locked = profile.getInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU);
127

  
128
							
128 129
							if (locked != null && !("0".equals(locked))) {
129 130
								profile.setInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU, "0");
130 131
								changed = true;
......
133 134
						if (changed) {
134 135
							spr.updateProfile(profile);
135 136
						}
136

  
137
						
137 138
						spr.unlockProfile(profile);
138 139
					}
139 140
				}
140 141
			}
141

  
142
			
142 143
			// this is the default repository
143 144
			URL baseURL = new URL(TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/index.html");
144 145
			try {
145 146
				InputStream s = baseURL.openStream();
146 147
				s.close();
147
			} catch (Exception e) {
148
			}
149
			catch (Exception e) {
148 150
				Log.severe("Update site is not reachable: " + baseURL + ", aborting.");
149 151
				return false;
150 152
			}
151

  
153
			
152 154
			addDefaultUpdateSites(agent);
153 155
			addDefaultPluginSites(agent);
154

  
156
			
155 157
			// add alpha and beta repos if needed
156 158
			// from bundle org.eclipse.equinox.p2.console
157

  
159
			
158 160
			Log.info("Looking for TXM updates...");
159 161
			Object ret = super.execute(event);
160 162
			Log.info("Done.");
161

  
163
			
162 164
			return ret;
163
		} catch (Exception e) {
165
		}
166
		catch (Exception e) {
164 167
			Log.severe("Could not update TXM: " + e + ".");
165 168
			Log.printStackTrace(e);
166 169
		}
167 170
		return null;
168 171
	}
169

  
172
	
170 173
	private boolean isWindowsAdministratorUser() {
171 174
		String path = BundleUtils.getInstallDirectory();// System.getProperty("osgi.instance.area");
172 175
		if (!path.startsWith("C:/Program Files/TXM")) {
173 176
			return true; // TXM was not installed in program files no need to test admin user
174 177
		}
175

  
178
		
176 179
		return isWindowsAdminCommandTest();
177 180
	}
178

  
179
	private static boolean isWindowsAdminCommandTest()
180
    {
181
        Preferences preferences = systemRoot();
182

  
183
        synchronized (System.err)
184
        {
185
            setErr(new PrintStream(new OutputStream() {
181
	
182
	private static boolean isWindowsAdminCommandTest() {
183
		Preferences preferences = systemRoot();
184
		
185
		synchronized (System.err) {
186
			setErr(new PrintStream(new OutputStream() {
187
				
186 188
				@Override
187
				public void write(int b) throws IOException { }
189
				public void write(int b) throws IOException {}
188 190
			}));
189

  
190
            try
191
            {
192
                preferences.put("TxmAdminAccessTestProperty", "TxmAdminAccessTestValue"); // SecurityException on Windows
193
                preferences.remove("TxmAdminAccessTestProperty");
194
                preferences.flush(); // BackingStoreException on Linux
195
                return true;
196
            } catch (Exception exception)
197
            {
198
                return false;
199
            } finally
200
            {
201
                setErr(System.err);
202
            }
203
        }
204
    }
205

  
191
			
192
			try {
193
				preferences.put("TxmAdminAccessTestProperty", "TxmAdminAccessTestValue"); // SecurityException on Windows
194
				preferences.remove("TxmAdminAccessTestProperty");
195
				preferences.flush(); // BackingStoreException on Linux
196
				return true;
197
			}
198
			catch (Exception exception) {
199
				return false;
200
			}
201
			finally {
202
				setErr(System.err);
203
			}
204
		}
205
	}
206
	
206 207
	/**
207 208
	 * Return a shell appropriate for parenting dialogs of this handler.
208 209
	 * 
......
213 214
		// System.out.println("MY GETSHELL: "+Display.getCurrent().getActiveShell());
214 215
		return new Shell();
215 216
	}
216

  
217
	
217 218
	public static boolean patchGZProfile() {
218 219
		// TODO: is this still useful with TXM 0.7.7 -> yes&no : need to patch when
219 220
		// --configuration is set, done by editing the Profile object directly
......
221 222
		URL location = null;
222 223
		try {
223 224
			location = new URL(path);
224
		} catch (MalformedURLException e) {
225
		}
226
		catch (MalformedURLException e) {
225 227
			Log.severe(NLS.bind(TXMUIMessages.switchLanguageColonMalformedUrlColonP0, location));
226 228
			return false;
227 229
		}
228

  
230
		
229 231
		File txmConfigDir = new File(location.getFile());
230 232
		// File txmConfigDir = new File("/home/mdecorde/.txm"); // test only
231 233
		File profileDir = new File(txmConfigDir, "p2/org.eclipse.equinox.p2.engine/profileRegistry/profile.profile");
232

  
234
		
233 235
		Log.fine("Looking for profile in : " + txmConfigDir);
234 236
		// System.out.println(txmConfigDir);
235 237
		// System.out.println(profileDir);
236

  
238
		
237 239
		if (!profileDir.exists()) {
238 240
			Log.severe("p2 profile directory does not exists: " + profileDir);
239 241
			return false;
240 242
		}
241

  
243
		
242 244
		// 1- find profile.gz
243 245
		File gzFile = findNewestGZFile(profileDir);
244 246
		if (gzFile == null) {
245 247
			Log.severe("No profile GZ file found");
246 248
			return false;
247
		} else {
249
		}
250
		else {
248 251
			try {
249 252
				// 2- extract profile from gz profile
250 253
				File profileXMLFile = GZip.uncompress(gzFile, profileDir);
......
255 258
					Log.severe("Could not delete old profile GZfile: " + gzFile);
256 259
					return false;
257 260
				}
258

  
261
				
259 262
				// File lockFile = new File(profileDir, ".lock");
260 263
				// System.out.println("Delete .lock file: "+lockFile+" "+lockFile.delete());
261

  
264
				
262 265
				// 5- compress new profile
263 266
				GZip.compress(profileXMLFile, gzFile);
264 267
				// 6- clean files
......
268 271
				}
269 272
				Log.fine("Profile archive patched: " + gzFile);
270 273
				return true;
271
			} catch (IOException e) {
274
			}
275
			catch (IOException e) {
272 276
				Log.severe("Failed to fix profile file: " + e);
273 277
				Log.printStackTrace(e);
274 278
				return false;
275 279
			}
276 280
		}
277 281
	}
278

  
282
	
279 283
	public static void addDefaultUpdateSites(IProvisioningAgent agent) {
280 284
		Version v = Activator.getDefault().getBundle().getVersion();
281 285
		String version = "" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro();
......
283 287
		String uriDefault = uriBase + "/stable";
284 288
		try {
285 289
			URI repoUriDefault = new URI(uriDefault);
286
			addURL(agent, repoUriDefault);
290
			addURL(agent, repoUriDefault, "stable");
287 291
			// URI repoKepler = new URI("http://download.eclipse.org/releases/kepler");
288 292
			// addURL(agent, repoKepler);
289
		} catch (Exception e) {
293
		}
294
		catch (Exception e) {
290 295
			Log.severe("Could not add update default repository: " + uriDefault);
291 296
			Log.printStackTrace(e);
292 297
		}
293

  
298
		
294 299
		String updateLevel = RCPPreferences.getInstance().getString(RCPPreferences.UPDATE_LEVEL);
295 300
		boolean alphaMode = false;
296 301
		boolean betaMode = false;
297 302
		boolean devMode = false;
298 303
		if ("DEV".equals(updateLevel)) {//$NON-NLS-1$
299 304
			devMode = alphaMode = betaMode = true;
300
		} else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
305
		}
306
		else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
301 307
			alphaMode = betaMode = true;
302
		} else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
308
		}
309
		else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
303 310
			betaMode = true;
304 311
		}
305 312
		Log.fine("Update levels: dev=" + devMode + " alpha=" + alphaMode + " beta=" + betaMode);
306

  
313
		
307 314
		String uriDev = uriBase + "/dev";//$NON-NLS-1$
308 315
		try {
309 316
			URI repoUriDev = new URI(uriDev);
310 317
			if (devMode) {
311
				addURL(agent, repoUriDev);
312
			} else {
318
				addURL(agent, repoUriDev, "dev");
319
			}
320
			else {
313 321
				removeURL(agent, repoUriDev);
314 322
			}
315
		} catch (Exception e) {
323
		}
324
		catch (Exception e) {
316 325
			Log.severe("Could not add update DEV repository: " + uriDev);
317 326
			e.printStackTrace();
318 327
		}
319

  
328
		
320 329
		String uriAlpha = uriBase + "/alpha";//$NON-NLS-1$
321 330
		try {
322 331
			URI repoUriAlpha = new URI(uriAlpha);
323 332
			if (alphaMode) {
324
				addURL(agent, repoUriAlpha);
325
			} else {
333
				addURL(agent, repoUriAlpha, "alpha");
334
			}
335
			else {
326 336
				removeURL(agent, repoUriAlpha);
327 337
			}
328
		} catch (Exception e) {
338
		}
339
		catch (Exception e) {
329 340
			Log.severe("Could not add update ALPHA repository: " + uriAlpha);
330 341
			e.printStackTrace();
331 342
		}
332

  
343
		
333 344
		String uriBeta = uriBase + "/beta";//$NON-NLS-1$
334 345
		try {
335 346
			URI repoUriBeta = new URI(uriBeta);
336 347
			if (betaMode) {
337
				addURL(agent, repoUriBeta);
338
			} else {
348
				addURL(agent, repoUriBeta, "beta");
349
			}
350
			else {
339 351
				removeURL(agent, repoUriBeta);
340 352
			}
341
		} catch (Exception e) {
353
		}
354
		catch (Exception e) {
342 355
			Log.severe("Can not add update BETA repository: " + uriBeta);
343 356
		}
344 357
	}
345

  
358
	
346 359
	public static void addDefaultPluginSites(IProvisioningAgent agent) {
347 360
		Version v = Activator.getDefault().getBundle().getVersion();
348 361
		String version = "" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro();
349 362
		String uriBase = TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/" + version + "/ext";
350

  
363
		
351 364
		String uriDefault = uriBase + "/stable";
352 365
		try {
353 366
			URI repoUriDefault = new URI(uriDefault);
354
			addURL(agent, repoUriDefault);
367
			addURL(agent, repoUriDefault, "stable");
355 368
			// URI repoKepler = new URI("http://download.eclipse.org/releases/kepler");
356 369
			// addURL(agent, repoKepler);
357
		} catch (Exception e) {
370
		}
371
		catch (Exception e) {
358 372
			Log.severe("Could not add update default repository: " + uriDefault);
359 373
			Log.printStackTrace(e);
360 374
		}
361

  
375
		
362 376
		String updateLevel = RCPPreferences.getInstance().getString(RCPPreferences.UPDATE_LEVEL);
363 377
		boolean alphaMode = false;
364 378
		boolean betaMode = false;
365 379
		boolean devMode = false;
366 380
		if ("DEV".equals(updateLevel)) {//$NON-NLS-1$
367 381
			devMode = alphaMode = betaMode = true;
368
		} else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
382
		}
383
		else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
369 384
			alphaMode = betaMode = true;
370
		} else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
385
		}
386
		else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
371 387
			betaMode = true;
372 388
		}
373 389
		Log.fine("Update levels: dev=" + devMode + " alpha=" + alphaMode + " beta=" + betaMode);
374

  
390
		
375 391
		String uriDev = uriBase + "/dev";//$NON-NLS-1$
376 392
		try {
377 393
			URI repoUriDev = new URI(uriDev);
378 394
			if (devMode) {
379
				addURL(agent, repoUriDev);
380
			} else {
395
				addURL(agent, repoUriDev, "dev");
396
			}
397
			else {
381 398
				removeURL(agent, repoUriDev);
382 399
			}
383
		} catch (Exception e) {
400
		}
401
		catch (Exception e) {
384 402
			Log.severe("Could not add update DEV repository: " + uriDev);
385 403
			e.printStackTrace();
386 404
		}
387

  
405
		
388 406
		String uriAlpha = uriBase + "/alpha";//$NON-NLS-1$
389 407
		try {
390 408
			URI repoUriAlpha = new URI(uriAlpha);
391 409
			if (alphaMode) {
392
				addURL(agent, repoUriAlpha);
393
			} else {
410
				addURL(agent, repoUriAlpha, "alpha");
411
			}
412
			else {
394 413
				removeURL(agent, repoUriAlpha);
395 414
			}
396
		} catch (Exception e) {
415
		}
416
		catch (Exception e) {
397 417
			Log.severe("Could not add update ALPHA repository: " + uriAlpha);
398 418
			e.printStackTrace();
399 419
		}
400

  
420
		
401 421
		String uriBeta = uriBase + "/beta";//$NON-NLS-1$
402 422
		try {
403 423
			URI repoUriBeta = new URI(uriBeta);
404 424
			if (betaMode) {
405
				addURL(agent, repoUriBeta);
406
			} else {
425
				addURL(agent, repoUriBeta, "beta");
426
			}
427
			else {
407 428
				removeURL(agent, repoUriBeta);
408 429
			}
409
		} catch (Exception e) {
430
		}
431
		catch (Exception e) {
410 432
			Log.severe("Can not add update BETA repository: " + uriBeta);
411 433
		}
412

  
434
		
413 435
	}
414

  
415
	public static boolean addURL(IProvisioningAgent agent, URI location) {
436
	
437
	public static boolean addURL(IProvisioningAgent agent, URI location, String level) {
416 438
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent
417 439
				.getService(IMetadataRepositoryManager.SERVICE_NAME);
418 440
		IArtifactRepositoryManager manager2 = (IArtifactRepositoryManager) agent
419 441
				.getService(IArtifactRepositoryManager.SERVICE_NAME);
420

  
442
		
421 443
		if (manager == null) {
422 444
			System.out.println("No metadata repository manager found");
423 445
			return false;
......
426 448
			System.out.println("No artifact repository manager found");
427 449
			return false;
428 450
		}
429

  
451
		
430 452
		Log.fine("Adding update site URL: " + location + "...");
431

  
453
		
432 454
		try {
433 455
			// manager.removeRepository(location)
434 456
			manager.loadRepository(location, null);
435
		} catch (Exception e) {
457
		}
458
		catch (Exception e) {
436 459
			Log.severe("No repo - create a newmetadata repo");
437 460
			// for convenience create and add a repository here
438 461
			String repositoryName = location + " - metadata";
439 462
			try {
440 463
				manager.createRepository(location, repositoryName, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY,
441 464
						null);
442
			} catch (ProvisionException e2) {
465
			}
466
			catch (ProvisionException e2) {
443 467
				Log.severe("Error while adding metadata repo: " + location);
444 468
				Log.printStackTrace(e);
445 469
				return false;
446 470
			}
447 471
		}
448

  
472
		
449 473
		try {
450 474
			// manager.removeRepository(location)
451 475
			manager2.loadRepository(location, null);
452 476
			return true;
453
		} catch (Exception e) {
477
		}
478
		catch (Exception e) {
454 479
			// could not load a repo at that location so create one as a convenience
455 480
			String repositoryName = location + " - artifacts";
456 481
			try {
457
				Log.severe("No repo - create a new artifact repo");
458
				manager.createRepository(location, repositoryName, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY,
459
						null);
482
				Log.severe(NLS.bind("No {0} level update available.", level)); // Aucune mise à jour de niveau {0} n'est disponible.
483
				manager.createRepository(location, repositoryName, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
460 484
				return true;
461
			} catch (Exception e2) {
485
			}
486
			catch (Exception e2) {
462 487
				Log.severe("Error while adding artifact repo: " + location);
463 488
				Log.printStackTrace(e);
464 489
				return false;
465 490
			}
466 491
		}
467 492
	}
468

  
493
	
469 494
	public static boolean removeURL(IProvisioningAgent agent, URI location) {
470 495
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent
471 496
				.getService(IMetadataRepositoryManager.SERVICE_NAME);
472 497
		IArtifactRepositoryManager manager2 = (IArtifactRepositoryManager) agent
473 498
				.getService(IArtifactRepositoryManager.SERVICE_NAME);
474

  
499
		
475 500
		if (manager == null) {
476 501
			Log.severe("No metadata repository manager found");
477 502
			return false;
......
480 505
			Log.severe("No artifact repository manager found");
481 506
			return false;
482 507
		}
483

  
508
		
484 509
		manager.removeRepository(location);
485 510
		manager2.removeRepository(location);
486

  
511
		
487 512
		return true;
488 513
	}
489

  
514
	
490 515
	private static void patchProfile(File profileXMLFile) throws IOException {
491

  
516
		
492 517
		File tmpFile = File.createTempFile("tmp", ".profile", profileXMLFile.getParentFile());
493

  
518
		
494 519
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(profileXMLFile), "UTF-8"));
495 520
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpFile), "UTF-8"));
496

  
521
		
497 522
		String line = br.readLine();
498 523
		boolean firstLine = true;
499 524
		while (line != null) {
500 525
			if (!firstLine) {
501 526
				writer.write("\n");
502 527
			}
503

  
528
			
504 529
			String trimedLine = line.trim();
505 530
			if (trimedLine.startsWith("<property name='org.eclipse.equinox.p2.type.lock' value='")) {
506 531
				writer.write("<property name='org.eclipse.equinox.p2.type.lock' value='0'/>");
507
			} else {
532
			}
533
			else {
508 534
				writer.write(line);
509 535
			}
510 536
			firstLine = false;
......
512 538
		}
513 539
		writer.close();
514 540
		br.close();
515

  
541
		
516 542
		if (!(profileXMLFile.delete() && tmpFile.renameTo(profileXMLFile))) {
517 543
			throw new IOException("failed to rewrite profile file: " + profileXMLFile);
518 544
		}
519 545
	}
520

  
546
	
521 547
	private static File findNewestGZFile(File profileDir) {
522 548
		File[] files = profileDir.listFiles(new FilenameFilter() {
523

  
549
			
524 550
			@Override
525 551
			public boolean accept(File dir, String name) {
526 552
				return name.toLowerCase().endsWith(".gz");
527 553
			}
528 554
		});
529

  
555
		
530 556
		if (files == null || files.length == 0)
531 557
			return null;
532

  
558
		
533 559
		Arrays.sort(files);
534 560
		return files[files.length - 1];
535 561
	}
536

  
562
	
537 563
	// COPY FROM UpdateHandler class
538 564
	// boolean hasNoRepos = false;
539 565
	// UpdateOperation operation;
......
653 679
	// protected String getProgressTaskName() {
654 680
	// return ProvSDKMessages.UpdateHandler_ProgressTaskName;
655 681
	// }
656

  
682
	
657 683
}

Formats disponibles : Unified diff