Révision 2815

tmp/org.txm.rcp/src/main/java/org/txm/rcp/p2/plugins/TXMUpdateHandler.java (revision 2815)
47 47
import org.txm.rcp.messages.TXMUIMessages;
48 48
import org.txm.rcp.preferences.RCPPreferences;
49 49
import org.txm.utils.BundleUtils;
50
import org.txm.utils.OSDetector;
50 51
import org.txm.utils.logger.Log;
51 52
import org.txm.utils.zip.GZip;
52 53

  
53 54
public class TXMUpdateHandler extends UpdateHandler {
54
	
55

  
55 56
	// public static final String UPDATESITE = "http://textometrie.ens-lyon.fr/dist"; //
56 57
	// "file:"+System.getProperty("user.home")+"/TEMP/updates"; // LOCAL TEST ONLY
57 58
	public static final String ID = "org.txm.rcp.p2.plugins.TXMUpdateHandler";
58
	
59

  
59 60
	/**
60 61
	 * Execute the command.
61 62
	 */
62 63
	@Override
63 64
	public Object execute(ExecutionEvent event) {
64
		
65

  
66
		if (OSDetector.isFamilyWindows() && !isWindowsAdministratorUser()) {
67
			Log.warning("Aborting update. you need to run TXM as administrator to do updates");
68
		}
69

  
65 70
		String key = event.getParameter("org.txm.rcp.parameters.update"); //$NON-NLS-1$
66 71
		if ("true".equals(key)) {
67 72
			String path = BundleUtils.getInstallDirectory();
......
80 85
				}
81 86
			}
82 87
		}
83
		
88

  
84 89
		// Hacking p2 profile to be able to update installation directory even if -configuration is set
85 90
		try {
86 91
			ProvisioningUI pui = ProvisioningUI.getDefaultUI();
87
			
92

  
88 93
			String profileId = pui.getProfileId();
89 94
			IProvisioningAgent agent = pui.getSession().getProvisioningAgent();
90
			
95

  
91 96
			if (agent != null) {
92 97
				IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
93
				
94
				
98

  
99

  
95 100
				if (profileRegistry != null && profileRegistry instanceof SimpleProfileRegistry) {
96
					
101

  
97 102
					SimpleProfileRegistry spr = (SimpleProfileRegistry) profileRegistry;
98
					
103

  
99 104
					IProfile iprofile = profileRegistry.getProfile(profileId);
100 105
					if (iprofile != null && iprofile instanceof Profile) {
101 106
						Profile profile = (Profile) iprofile;
102
						
107

  
103 108
						spr.lockProfile(profile);
104
						
109

  
105 110
						// System.out.println("Profile: "+profile.getClass()+" = "+profile);
106 111
						Iterator<IInstallableUnit> everything = profile.everything();
107 112
						boolean changed = false;
108 113
						while (everything.hasNext()) {
109 114
							IInstallableUnit iu = everything.next();
110 115
							String locked = profile.getInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU);
111
							
116

  
112 117
							if (locked != null && !("0".equals(locked))) {
113 118
								profile.setInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU, "0");
114 119
								changed = true;
......
117 122
						if (changed) {
118 123
							spr.updateProfile(profile);
119 124
						}
120
						
125

  
121 126
						spr.unlockProfile(profile);
122 127
					}
123 128
				}
124 129
			}
125
			
130

  
126 131
			// this is the default repository
127 132
			URL baseURL = new URL(TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/index.html");
128 133
			try {
......
133 138
				Log.severe("Update site is not reachable: " + baseURL + ", aborting.");
134 139
				return false;
135 140
			}
136
			
141

  
137 142
			addDefaultUpdateSites(agent);
138 143
			addDefaultPluginSites(agent);
139
			
144

  
140 145
			// add alpha and beta repos if needed
141 146
			// from bundle org.eclipse.equinox.p2.console
142
			
147

  
143 148
			Log.info("Looking for TXM updates...");
144 149
			Object ret = super.execute(event);
145 150
			Log.info("Done.");
146
			
151

  
147 152
			return ret;
148 153
		}
149 154
		catch (Exception e) {
......
152 157
		}
153 158
		return null;
154 159
	}
155
	
160

  
161
	private boolean isWindowsAdministratorUser() {
162
		String path = BundleUtils.getInstallDirectory();// System.getProperty("osgi.instance.area");
163
		if (!path.startsWith("C:/Program files/TXM")) {
164
			return true; // TXM was not installed in program files no need to test admin user
165
		}
166

  
167
		return isAdminCommandTest();
168
	}
169

  
170
	public static  boolean isAdminCommandTest() {
171
		StringBuilder outputbuilder = new StringBuilder();
172
		try {
173
			ProcessBuilder builder = new ProcessBuilder(
174
					"cmd.exe","/c" ,"net user");
175
			builder.redirectErrorStream(true);
176
			Process p = builder.start();
177
			BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
178
			String line;
179
			while (true) {
180
				line = r.readLine();
181
				if (line == null) { break; }
182
				outputbuilder.append(line);
183
			}
184
		} catch (IOException e) {
185
			e.printStackTrace();
186
			return false;
187
		}
188
		System.out.println(outputbuilder.toString());
189
		return outputbuilder.toString().contains("Administrator");
190
	}
191

  
156 192
	/**
157 193
	 * Return a shell appropriate for parenting dialogs of this handler.
158 194
	 * 
......
163 199
		// System.out.println("MY GETSHELL: "+Display.getCurrent().getActiveShell());
164 200
		return new Shell();
165 201
	}
166
	
202

  
167 203
	public static boolean patchGZProfile() {
168 204
		// 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
169 205
		String path = BundleUtils.getInstallDirectory();// System.getProperty("osgi.instance.area");
......
175 211
			Log.severe(NLS.bind(TXMUIMessages.switchLanguageColonMalformedUrlColonP0, location));
176 212
			return false;
177 213
		}
178
		
214

  
179 215
		File txmConfigDir = new File(location.getFile());
180 216
		// File txmConfigDir = new File("/home/mdecorde/.txm"); // test only
181 217
		File profileDir = new File(txmConfigDir, "p2/org.eclipse.equinox.p2.engine/profileRegistry/profile.profile");
182
		
218

  
183 219
		Log.fine("Looking for profile in : " + txmConfigDir);
184 220
		// System.out.println(txmConfigDir);
185 221
		// System.out.println(profileDir);
186
		
222

  
187 223
		if (!profileDir.exists()) {
188 224
			Log.severe("p2 profile directory does not exists: " + profileDir);
189 225
			return false;
190 226
		}
191
		
227

  
192 228
		// 1- find profile.gz
193 229
		File gzFile = findNewestGZFile(profileDir);
194 230
		if (gzFile == null) {
......
206 242
					Log.severe("Could not delete old profile GZfile: " + gzFile);
207 243
					return false;
208 244
				}
209
				
245

  
210 246
				// File lockFile = new File(profileDir, ".lock");
211 247
				// System.out.println("Delete .lock file: "+lockFile+" "+lockFile.delete());
212
				
248

  
213 249
				// 5- compress new profile
214 250
				GZip.compress(profileXMLFile, gzFile);
215 251
				// 6- clean files
......
227 263
			}
228 264
		}
229 265
	}
230
	
266

  
231 267
	public static void addDefaultUpdateSites(IProvisioningAgent agent) {
232 268
		Version v = Activator.getDefault().getBundle().getVersion();
233 269
		String version = "" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro();
......
243 279
			Log.severe("Could not add update default repository: " + uriDefault);
244 280
			Log.printStackTrace(e);
245 281
		}
246
		
282

  
247 283
		String updateLevel = RCPPreferences.getInstance().getString(RCPPreferences.UPDATE_LEVEL);
248 284
		boolean alphaMode = false;
249 285
		boolean betaMode = false;
......
258 294
			betaMode = true;
259 295
		}
260 296
		Log.fine("Update levels: dev=" + devMode + " alpha=" + alphaMode + " beta=" + betaMode);
261
		
297

  
262 298
		String uriDev = uriBase + "/dev";//$NON-NLS-1$
263 299
		try {
264 300
			URI repoUriDev = new URI(uriDev);
......
273 309
			Log.severe("Could not add update DEV repository: " + uriDev);
274 310
			e.printStackTrace();
275 311
		}
276
		
312

  
277 313
		String uriAlpha = uriBase + "/alpha";//$NON-NLS-1$
278 314
		try {
279 315
			URI repoUriAlpha = new URI(uriAlpha);
......
288 324
			Log.severe("Could not add update ALPHA repository: " + uriAlpha);
289 325
			e.printStackTrace();
290 326
		}
291
		
327

  
292 328
		String uriBeta = uriBase + "/beta";//$NON-NLS-1$
293 329
		try {
294 330
			URI repoUriBeta = new URI(uriBeta);
......
303 339
			Log.severe("Can not add update BETA repository: " + uriBeta);
304 340
		}
305 341
	}
306
	
342

  
307 343
	public static void addDefaultPluginSites(IProvisioningAgent agent) {
308 344
		Version v = Activator.getDefault().getBundle().getVersion();
309 345
		String version = "" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro();
310 346
		String uriBase = TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/" + version + "/ext";
311
		
347

  
312 348
		String uriDefault = uriBase + "/stable";
313 349
		try {
314 350
			URI repoUriDefault = new URI(uriDefault);
......
320 356
			Log.severe("Could not add update default repository: " + uriDefault);
321 357
			Log.printStackTrace(e);
322 358
		}
323
		
359

  
324 360
		String updateLevel = RCPPreferences.getInstance().getString(RCPPreferences.UPDATE_LEVEL);
325 361
		boolean alphaMode = false;
326 362
		boolean betaMode = false;
......
335 371
			betaMode = true;
336 372
		}
337 373
		Log.fine("Update levels: dev=" + devMode + " alpha=" + alphaMode + " beta=" + betaMode);
338
		
374

  
339 375
		String uriDev = uriBase + "/dev";//$NON-NLS-1$
340 376
		try {
341 377
			URI repoUriDev = new URI(uriDev);
......
350 386
			Log.severe("Could not add update DEV repository: " + uriDev);
351 387
			e.printStackTrace();
352 388
		}
353
		
389

  
354 390
		String uriAlpha = uriBase + "/alpha";//$NON-NLS-1$
355 391
		try {
356 392
			URI repoUriAlpha = new URI(uriAlpha);
......
365 401
			Log.severe("Could not add update ALPHA repository: " + uriAlpha);
366 402
			e.printStackTrace();
367 403
		}
368
		
404

  
369 405
		String uriBeta = uriBase + "/beta";//$NON-NLS-1$
370 406
		try {
371 407
			URI repoUriBeta = new URI(uriBeta);
......
379 415
		catch (Exception e) {
380 416
			Log.severe("Can not add update BETA repository: " + uriBeta);
381 417
		}
382
		
418

  
383 419
	}
384
	
420

  
385 421
	public static boolean addURL(IProvisioningAgent agent, URI location) {
386 422
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
387 423
		IArtifactRepositoryManager manager2 = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
388
		
424

  
389 425
		if (manager == null) {
390 426
			System.out.println("No metadata repository manager found");
391 427
			return false;
......
394 430
			System.out.println("No artifact repository manager found");
395 431
			return false;
396 432
		}
397
		
433

  
398 434
		Log.fine("Adding update site URL: " + location + "...");
399
		
435

  
400 436
		try {
401 437
			// manager.removeRepository(location)
402 438
			manager.loadRepository(location, null);
......
414 450
				return false;
415 451
			}
416 452
		}
417
		
453

  
418 454
		try {
419 455
			// manager.removeRepository(location)
420 456
			manager2.loadRepository(location, null);
......
435 471
			}
436 472
		}
437 473
	}
438
	
474

  
439 475
	public static boolean removeURL(IProvisioningAgent agent, URI location) {
440 476
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
441 477
		IArtifactRepositoryManager manager2 = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
442
		
478

  
443 479
		if (manager == null) {
444 480
			Log.severe("No metadata repository manager found");
445 481
			return false;
......
448 484
			Log.severe("No artifact repository manager found");
449 485
			return false;
450 486
		}
451
		
487

  
452 488
		manager.removeRepository(location);
453 489
		manager2.removeRepository(location);
454
		
490

  
455 491
		return true;
456 492
	}
457
	
493

  
458 494
	private static void patchProfile(File profileXMLFile) throws IOException {
459
		
495

  
460 496
		File tmpFile = File.createTempFile("tmp", ".profile", profileXMLFile.getParentFile());
461
		
497

  
462 498
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(profileXMLFile), "UTF-8"));
463 499
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpFile), "UTF-8"));
464
		
500

  
465 501
		String line = br.readLine();
466 502
		boolean firstLine = true;
467 503
		while (line != null) {
468 504
			if (!firstLine) {
469 505
				writer.write("\n");
470 506
			}
471
			
507

  
472 508
			String trimedLine = line.trim();
473 509
			if (trimedLine.startsWith("<property name='org.eclipse.equinox.p2.type.lock' value='")) {
474 510
				writer.write("<property name='org.eclipse.equinox.p2.type.lock' value='0'/>");
......
481 517
		}
482 518
		writer.close();
483 519
		br.close();
484
		
520

  
485 521
		if (!(profileXMLFile.delete() && tmpFile.renameTo(profileXMLFile))) {
486 522
			throw new IOException("failed to rewrite profile file: " + profileXMLFile);
487 523
		}
488 524
	}
489
	
525

  
490 526
	private static File findNewestGZFile(File profileDir) {
491 527
		File[] files = profileDir.listFiles(new FilenameFilter() {
492
			
528

  
493 529
			@Override
494 530
			public boolean accept(File dir, String name) {
495 531
				return name.toLowerCase().endsWith(".gz");
496 532
			}
497 533
		});
498
		
534

  
499 535
		if (files == null || files.length == 0) return null;
500
		
536

  
501 537
		Arrays.sort(files);
502 538
		return files[files.length - 1];
503 539
	}
504
	
540

  
505 541
	// COPY FROM UpdateHandler class
506 542
	// boolean hasNoRepos = false;
507 543
	// UpdateOperation operation;
......
607 643
	// protected String getProgressTaskName() {
608 644
	// return ProvSDKMessages.UpdateHandler_ProgressTaskName;
609 645
	// }
610
	
611
	
612
	
646

  
647

  
648

  
613 649
}

Formats disponibles : Unified diff