Revision 2540

tmp/org.txm.rcp/src/main/java/org/txm/rcp/editors/TXMWebBrowserEditor.java (revision 2540)
19 19
import org.eclipse.ui.internal.browser.BrowserViewer;
20 20
import org.eclipse.ui.internal.browser.WebBrowserEditor;
21 21

  
22
@SuppressWarnings("restriction") //$NON-NLS-1$
22
@SuppressWarnings("restriction")
23 23
public class TXMWebBrowserEditor extends WebBrowserEditor {
24 24
	
25 25
	public static final String WEB_BROWSER_EDITOR_ID = "org.txm.rcp.editors.TXMWebBrowserEditor"; //$NON-NLS-1$
26 26
	
27
	@Override
27 28
	public void setTitleImage(Image img) {
28 29
		super.setTitleImage(img);
29 30
	}
30 31
	
32
	@Override
31 33
	public void setTitle(String title) {
32 34
		super.setTitle(title);
33 35
	}
......
35 37
	public void setTitleTooltip(String tooltip) {
36 38
		super.setTitleToolTip(tooltip);
37 39
	}
38

  
40
	
39 41
	public void focus() {
40 42
		webBrowser.getBrowser().forceFocus();
41 43
	}
......
48 50
	public void createPartControl(Composite parent) {
49 51
		super.createPartControl(parent);
50 52
		
51
		new CommandLink (this.webBrowser.getBrowser());
53
		new CommandLink(this.webBrowser.getBrowser());
52 54
	}
53 55
	
54 56
	// Called by JavaScript
55 57
	public class CommandLink extends BrowserFunction {
58
		
56 59
		String data = "";
60
		
57 61
		Browser browser;
58
		CommandLink (Browser browser) {
59
			super (browser, "txmcommand");
62
		
63
		CommandLink(Browser browser) {
64
			super(browser, "txmcommand");
60 65
			this.browser = browser;
61 66
		}
62
		public Object function (Object[] arguments) {
67
		
68
		@Override
69
		public Object function(Object[] arguments) {
63 70
			if (arguments.length == 0) return null;
64 71
			
65
			HashMap<String, String> params = new HashMap<String, String>();
66
			for (int i = 0 ; i < arguments.length ; i += 2) {
67
				params.put(arguments[i].toString(), arguments[i+1].toString());
72
			HashMap<String, String> params = new HashMap<>();
73
			for (int i = 0; i < arguments.length; i += 2) {
74
				params.put(arguments[i].toString(), arguments[i + 1].toString());
68 75
			}
69

  
76
			
70 77
			String id = params.get("id");
71 78
			if (id != null) {
72 79
				params.remove("id");
73
				//System.out.println("CALLING CMD with id="+id+" and parameters="+params);
74
				//get the command from plugin.xml
80
				// System.out.println("CALLING CMD with id="+id+" and parameters="+params);
81
				// get the command from plugin.xml
75 82
				IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
76
				ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
83
				ICommandService cmdService = window.getService(ICommandService.class);
77 84
				Command cmd = cmdService.getCommand(id);
78

  
79
				//get the parameter
80
				ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
85
				
86
				// get the parameter
87
				ArrayList<Parameterization> parameters = new ArrayList<>();
81 88
				for (String k : params.keySet()) {
82 89
					try {
83 90
						IParameter iparam = cmd.getParameter(k);
84 91
						Parameterization p = new Parameterization(iparam, params.get(k));
85 92
						parameters.add(p);
86
					} catch (NotDefinedException e) {
87
						System.out.println("warning: unknowned parameter id="+k);
88 93
					}
94
					catch (NotDefinedException e) {
95
						System.out.println("warning: unknown parameter id=" + k);
96
					}
89 97
				}
90

  
91
				//build the parameterized command
98
				
99
				// build the parameterized command
92 100
				ParameterizedCommand pc = new ParameterizedCommand(cmd, parameters.toArray(new Parameterization[parameters.size()]));
93

  
94
				//execute the command
95
				IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
101
				
102
				// execute the command
103
				IHandlerService handlerService = window.getService(IHandlerService.class);
96 104
				try {
97 105
					handlerService.executeCommand(pc, null);
98
				} catch (Exception e) {
106
				}
107
				catch (Exception e) {
99 108
					e.printStackTrace();
100 109
				}
101
			} else {
102
				System.out.println("Cannot call txmcommand without 'id' parameter: "+params);
103 110
			}
111
			else {
112
				System.out.println("Cannot call txmcommand without 'id' parameter: " + params);
113
			}
104 114
			return arguments;
105 115
		}
106 116
	}
tmp/org.txm.rcp/src/main/java/org/txm/rcp/p2/plugins/TXMUpdateHandler.java (revision 2540)
51 51
import org.txm.utils.zip.GZip;
52 52

  
53 53
public class TXMUpdateHandler extends UpdateHandler {
54

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

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

  
64
		
64 65
		String key = event.getParameter("org.txm.rcp.parameters.update"); //$NON-NLS-1$
65 66
		if ("true".equals(key)) {
66 67
			String path = BundleUtils.getInstallDirectory();
67 68
			if (path != null) {
68 69
				try {
69 70
					File installDirectory = new File(path);
70
					Log.fine("Testing install directory rights: "+installDirectory);
71
					Log.fine("Testing install directory rights: " + installDirectory);
71 72
					if (!installDirectory.canWrite() || !installDirectory.canExecute()) {
72 73
						Log.warning(NLS.bind("Warning: you need administrator privileges to fully update TXM (installed in {0}). Some updates might not be fully installed.", installDirectory));
74
						return null;
73 75
					}
74
				} catch (Exception e) {
76
				}
77
				catch (Exception e) {
75 78
					// TODO Auto-generated catch block
76 79
					e.printStackTrace();
77 80
				}
78 81
			}
79 82
		}
80

  
83
		
81 84
		// Hacking p2 profile to be able to update installation directory even if -configuration is set
82 85
		try {
83 86
			ProvisioningUI pui = ProvisioningUI.getDefaultUI();
84
		
87
			
85 88
			String profileId = pui.getProfileId();
86 89
			IProvisioningAgent agent = pui.getSession().getProvisioningAgent();
87

  
90
			
88 91
			if (agent != null) {
89 92
				IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
90

  
91

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

  
94
					SimpleProfileRegistry spr = (SimpleProfileRegistry)profileRegistry;
95

  
96
					
97
					SimpleProfileRegistry spr = (SimpleProfileRegistry) profileRegistry;
98
					
96 99
					IProfile iprofile = profileRegistry.getProfile(profileId);
97 100
					if (iprofile != null && iprofile instanceof Profile) {
98
						Profile profile = (Profile)iprofile;
101
						Profile profile = (Profile) iprofile;
99 102
						
100 103
						spr.lockProfile(profile);
101

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

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

  
118
						spr.unlockProfile(profile);									
120
						
121
						spr.unlockProfile(profile);
119 122
					}
120 123
				}
121 124
			}
122

  
125
			
123 126
			// this is the default repository
124
			URL baseURL = new URL(TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE)+"/index.html");
127
			URL baseURL = new URL(TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/index.html");
125 128
			try {
126 129
				InputStream s = baseURL.openStream();
127 130
				s.close();
128
			} catch(Exception e) {
129
				Log.severe("Update site is not reachable: "+baseURL+", aborting.");
131
			}
132
			catch (Exception e) {
133
				Log.severe("Update site is not reachable: " + baseURL + ", aborting.");
130 134
				return false;
131 135
			}
132

  
136
			
133 137
			addDefaultUpdateSites(agent);
134 138
			addDefaultPluginSites(agent);
135

  
139
			
136 140
			// add alpha and beta repos if needed
137 141
			// from bundle org.eclipse.equinox.p2.console
138

  
142
			
139 143
			Log.info("Looking for TXM updates...");
140 144
			Object ret = super.execute(event);
141 145
			Log.info("Done.");
142

  
146
			
143 147
			return ret;
144
		} catch(Exception e) {
145
			Log.severe("Could not update TXM: "+e + ".");
148
		}
149
		catch (Exception e) {
150
			Log.severe("Could not update TXM: " + e + ".");
146 151
			Log.printStackTrace(e);
147 152
		}
148 153
		return null;
149 154
	}
150

  
155
	
151 156
	/**
152 157
	 * Return a shell appropriate for parenting dialogs of this handler.
158
	 * 
153 159
	 * @return a Shell
154 160
	 */
161
	@Override
155 162
	protected Shell getShell() {
156
		//System.out.println("MY GETSHELL: "+Display.getCurrent().getActiveShell());
163
		// System.out.println("MY GETSHELL: "+Display.getCurrent().getActiveShell());
157 164
		return new Shell();
158 165
	}
159

  
166
	
160 167
	public static boolean patchGZProfile() {
161
		//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
162
		String path = BundleUtils.getInstallDirectory();//System.getProperty("osgi.instance.area"); //$NON-NLS-1$
168
		// 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
		String path = BundleUtils.getInstallDirectory();// System.getProperty("osgi.instance.area");
163 170
		URL location = null;
164 171
		try {
165
			location = new URL(path); //$NON-NLS-1$
166
		} catch (MalformedURLException e) {
172
			location = new URL(path);
173
		}
174
		catch (MalformedURLException e) {
167 175
			Log.severe(NLS.bind(TXMUIMessages.switchLanguageColonMalformedUrlColonP0, location));
168 176
			return false;
169 177
		}
170

  
178
		
171 179
		File txmConfigDir = new File(location.getFile());
172
		//File txmConfigDir = new File("/home/mdecorde/.txm"); // test only
180
		// File txmConfigDir = new File("/home/mdecorde/.txm"); // test only
173 181
		File profileDir = new File(txmConfigDir, "p2/org.eclipse.equinox.p2.engine/profileRegistry/profile.profile");
174

  
175
		Log.fine("Looking for profile in : "+txmConfigDir);
176
		//System.out.println(txmConfigDir);
177
		//System.out.println(profileDir);
178

  
182
		
183
		Log.fine("Looking for profile in : " + txmConfigDir);
184
		// System.out.println(txmConfigDir);
185
		// System.out.println(profileDir);
186
		
179 187
		if (!profileDir.exists()) {
180
			Log.severe("p2 profile directory does not exists: "+profileDir);
188
			Log.severe("p2 profile directory does not exists: " + profileDir);
181 189
			return false;
182 190
		}
183

  
184
		//1- find profile.gz
191
		
192
		// 1- find profile.gz
185 193
		File gzFile = findNewestGZFile(profileDir);
186 194
		if (gzFile == null) {
187 195
			Log.severe("No profile GZ file found");
188 196
			return false;
189
		} else {
197
		}
198
		else {
190 199
			try {
191
				//2- extract profile from gz profile
200
				// 2- extract profile from gz profile
192 201
				File profileXMLFile = GZip.uncompress(gzFile, profileDir);
193
				//3- patch xml by removing lock properties
202
				// 3- patch xml by removing lock properties
194 203
				patchProfile(profileXMLFile);
195
				//4- delete old gz profile
204
				// 4- delete old gz profile
196 205
				if (!gzFile.delete()) {
197
					Log.severe("Could not delete old profile GZfile: "+gzFile);
206
					Log.severe("Could not delete old profile GZfile: " + gzFile);
198 207
					return false;
199 208
				}
200

  
201
				//File lockFile = new File(profileDir, ".lock");
202
				//System.out.println("Delete .lock file: "+lockFile+" "+lockFile.delete());
203

  
204
				//5- compress new profile
209
				
210
				// File lockFile = new File(profileDir, ".lock");
211
				// System.out.println("Delete .lock file: "+lockFile+" "+lockFile.delete());
212
				
213
				// 5- compress new profile
205 214
				GZip.compress(profileXMLFile, gzFile);
206
				//6- clean files
215
				// 6- clean files
207 216
				if (!profileXMLFile.delete()) {
208
					Log.severe("Could not delete temporary profile XML file: "+profileXMLFile);
217
					Log.severe("Could not delete temporary profile XML file: " + profileXMLFile);
209 218
					return false;
210 219
				}
211
				Log.fine("Profile archive patched: "+gzFile);
220
				Log.fine("Profile archive patched: " + gzFile);
212 221
				return true;
213
			} catch (IOException e) {
214
				Log.severe("Failed to fix profile file: "+e);
222
			}
223
			catch (IOException e) {
224
				Log.severe("Failed to fix profile file: " + e);
215 225
				Log.printStackTrace(e);
216 226
				return false;
217 227
			}
218 228
		}
219 229
	}
220

  
230
	
221 231
	public static void addDefaultUpdateSites(IProvisioningAgent agent) {
222 232
		Version v = Activator.getDefault().getBundle().getVersion();
223
		String version =  ""+v.getMajor()+"."+v.getMinor()+"."+v.getMicro();
224
		String uriBase = TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE)+"/"+version+"/main";
225
		String uriDefault = uriBase+"/stable";
233
		String version = "" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro();
234
		String uriBase = TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/" + version + "/main";
235
		String uriDefault = uriBase + "/stable";
226 236
		try {
227 237
			URI repoUriDefault = new URI(uriDefault);
228 238
			addURL(agent, repoUriDefault);
229
			//URI repoKepler = new URI("http://download.eclipse.org/releases/kepler");
230
			//addURL(agent, repoKepler);			
231
		} catch( Exception e ) {
239
			// URI repoKepler = new URI("http://download.eclipse.org/releases/kepler");
240
			// addURL(agent, repoKepler);
241
		}
242
		catch (Exception e) {
232 243
			Log.severe("Could not add update default repository: " + uriDefault);
233 244
			Log.printStackTrace(e);
234 245
		}
235

  
246
		
236 247
		String updateLevel = RCPPreferences.getInstance().getString(RCPPreferences.UPDATE_LEVEL);
237 248
		boolean alphaMode = false;
238 249
		boolean betaMode = false;
239 250
		boolean devMode = false;
240 251
		if ("DEV".equals(updateLevel)) {//$NON-NLS-1$
241 252
			devMode = alphaMode = betaMode = true;
242
		} else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
253
		}
254
		else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
243 255
			alphaMode = betaMode = true;
244
		} else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
256
		}
257
		else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
245 258
			betaMode = true;
246 259
		}
247
		Log.fine("Update levels: dev="+devMode+" alpha="+alphaMode+" beta="+betaMode);
248

  
249
		String uriDev = uriBase+"/dev";//$NON-NLS-1$
260
		Log.fine("Update levels: dev=" + devMode + " alpha=" + alphaMode + " beta=" + betaMode);
261
		
262
		String uriDev = uriBase + "/dev";//$NON-NLS-1$
250 263
		try {
251 264
			URI repoUriDev = new URI(uriDev);
252 265
			if (devMode) {
253 266
				addURL(agent, repoUriDev);
254
			} else {
267
			}
268
			else {
255 269
				removeURL(agent, repoUriDev);
256 270
			}
257
		} catch( Exception e ) {
271
		}
272
		catch (Exception e) {
258 273
			Log.severe("Could not add update DEV repository: " + uriDev);
259 274
			e.printStackTrace();
260 275
		}
261

  
262
		String uriAlpha = uriBase+"/alpha";//$NON-NLS-1$
276
		
277
		String uriAlpha = uriBase + "/alpha";//$NON-NLS-1$
263 278
		try {
264 279
			URI repoUriAlpha = new URI(uriAlpha);
265 280
			if (alphaMode) {
266 281
				addURL(agent, repoUriAlpha);
267
			} else {
282
			}
283
			else {
268 284
				removeURL(agent, repoUriAlpha);
269 285
			}
270
		} catch( Exception e ) {
286
		}
287
		catch (Exception e) {
271 288
			Log.severe("Could not add update ALPHA repository: " + uriAlpha);
272 289
			e.printStackTrace();
273 290
		}
274

  
275
		String uriBeta = uriBase+"/beta";//$NON-NLS-1$
291
		
292
		String uriBeta = uriBase + "/beta";//$NON-NLS-1$
276 293
		try {
277 294
			URI repoUriBeta = new URI(uriBeta);
278 295
			if (betaMode) {
279 296
				addURL(agent, repoUriBeta);
280
			} else {
297
			}
298
			else {
281 299
				removeURL(agent, repoUriBeta);
282 300
			}
283
		} catch( Exception e ) {
301
		}
302
		catch (Exception e) {
284 303
			Log.severe("Can not add update BETA repository: " + uriBeta);
285 304
		}
286 305
	}
287

  
306
	
288 307
	public static void addDefaultPluginSites(IProvisioningAgent agent) {
289 308
		Version v = Activator.getDefault().getBundle().getVersion();
290
		String version =  ""+v.getMajor()+"."+v.getMinor()+"."+v.getMicro();
291
		String uriBase = TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE)+"/"+version+"/ext";
292

  
293
		String uriDefault = uriBase+"/stable";
309
		String version = "" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro();
310
		String uriBase = TBXPreferences.getInstance().getString(TBXPreferences.UPDATESITE) + "/" + version + "/ext";
311
		
312
		String uriDefault = uriBase + "/stable";
294 313
		try {
295 314
			URI repoUriDefault = new URI(uriDefault);
296 315
			addURL(agent, repoUriDefault);
297
			//URI repoKepler = new URI("http://download.eclipse.org/releases/kepler");
298
			//addURL(agent, repoKepler);			
299
		} catch( Exception e ) {
316
			// URI repoKepler = new URI("http://download.eclipse.org/releases/kepler");
317
			// addURL(agent, repoKepler);
318
		}
319
		catch (Exception e) {
300 320
			Log.severe("Could not add update default repository: " + uriDefault);
301 321
			Log.printStackTrace(e);
302 322
		}
303

  
323
		
304 324
		String updateLevel = RCPPreferences.getInstance().getString(RCPPreferences.UPDATE_LEVEL);
305 325
		boolean alphaMode = false;
306 326
		boolean betaMode = false;
307 327
		boolean devMode = false;
308 328
		if ("DEV".equals(updateLevel)) {//$NON-NLS-1$
309 329
			devMode = alphaMode = betaMode = true;
310
		} else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
330
		}
331
		else if ("ALPHA".equals(updateLevel)) {//$NON-NLS-1$
311 332
			alphaMode = betaMode = true;
312
		} else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
333
		}
334
		else if ("BETA".equals(updateLevel)) {//$NON-NLS-1$
313 335
			betaMode = true;
314 336
		}
315
		Log.fine("Update levels: dev="+devMode+" alpha="+alphaMode+" beta="+betaMode);
316

  
317
		String uriDev = uriBase+"/dev";//$NON-NLS-1$
337
		Log.fine("Update levels: dev=" + devMode + " alpha=" + alphaMode + " beta=" + betaMode);
338
		
339
		String uriDev = uriBase + "/dev";//$NON-NLS-1$
318 340
		try {
319 341
			URI repoUriDev = new URI(uriDev);
320 342
			if (devMode) {
321 343
				addURL(agent, repoUriDev);
322
			} else {
344
			}
345
			else {
323 346
				removeURL(agent, repoUriDev);
324 347
			}
325
		} catch( Exception e ) {
348
		}
349
		catch (Exception e) {
326 350
			Log.severe("Could not add update DEV repository: " + uriDev);
327 351
			e.printStackTrace();
328 352
		}
329

  
330
		String uriAlpha = uriBase+"/alpha";//$NON-NLS-1$
353
		
354
		String uriAlpha = uriBase + "/alpha";//$NON-NLS-1$
331 355
		try {
332 356
			URI repoUriAlpha = new URI(uriAlpha);
333 357
			if (alphaMode) {
334 358
				addURL(agent, repoUriAlpha);
335
			} else {
359
			}
360
			else {
336 361
				removeURL(agent, repoUriAlpha);
337 362
			}
338
		} catch( Exception e ) {
363
		}
364
		catch (Exception e) {
339 365
			Log.severe("Could not add update ALPHA repository: " + uriAlpha);
340 366
			e.printStackTrace();
341 367
		}
342

  
343
		String uriBeta = uriBase+"/beta";//$NON-NLS-1$
368
		
369
		String uriBeta = uriBase + "/beta";//$NON-NLS-1$
344 370
		try {
345 371
			URI repoUriBeta = new URI(uriBeta);
346 372
			if (betaMode) {
347 373
				addURL(agent, repoUriBeta);
348
			} else {
374
			}
375
			else {
349 376
				removeURL(agent, repoUriBeta);
350 377
			}
351
		} catch( Exception e ) {
378
		}
379
		catch (Exception e) {
352 380
			Log.severe("Can not add update BETA repository: " + uriBeta);
353 381
		}
354

  
382
		
355 383
	}
356

  
384
	
357 385
	public static boolean addURL(IProvisioningAgent agent, URI location) {
358 386
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
359 387
		IArtifactRepositoryManager manager2 = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
360

  
388
		
361 389
		if (manager == null) {
362 390
			System.out.println("No metadata repository manager found");
363 391
			return false;
......
366 394
			System.out.println("No artifact repository manager found");
367 395
			return false;
368 396
		}
369

  
370
		Log.fine("Adding update site URL: "+location + "...");
371

  
397
		
398
		Log.fine("Adding update site URL: " + location + "...");
399
		
372 400
		try {
373
			//manager.removeRepository(location)
401
			// manager.removeRepository(location)
374 402
			manager.loadRepository(location, null);
375
		} catch (Exception e) {
403
		}
404
		catch (Exception e) {
376 405
			Log.severe("No repo - create a newmetadata repo");
377 406
			// for convenience create and add a repository here
378 407
			String repositoryName = location + " - metadata";
379 408
			try {
380 409
				manager.createRepository(location, repositoryName, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
381
			} catch (ProvisionException e2) {
382
				Log.severe("Error while adding metadata repo: "+location);
410
			}
411
			catch (ProvisionException e2) {
412
				Log.severe("Error while adding metadata repo: " + location);
383 413
				Log.printStackTrace(e);
384 414
				return false;
385 415
			}
386 416
		}
387

  
417
		
388 418
		try {
389
			//manager.removeRepository(location)
419
			// manager.removeRepository(location)
390 420
			manager2.loadRepository(location, null);
391 421
			return true;
392
		} catch (Exception e) {
422
		}
423
		catch (Exception e) {
393 424
			// could not load a repo at that location so create one as a convenience
394 425
			String repositoryName = location + " - artifacts";
395 426
			try {
396 427
				Log.severe("No repo - create a new artifact repo");
397 428
				manager.createRepository(location, repositoryName, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
398 429
				return true;
399
			} catch (Exception e2) {
400
				Log.severe("Error while adding artifact repo: "+location);
430
			}
431
			catch (Exception e2) {
432
				Log.severe("Error while adding artifact repo: " + location);
401 433
				Log.printStackTrace(e);
402 434
				return false;
403 435
			}
404 436
		}
405 437
	}
406

  
438
	
407 439
	public static boolean removeURL(IProvisioningAgent agent, URI location) {
408 440
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
409 441
		IArtifactRepositoryManager manager2 = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
410

  
442
		
411 443
		if (manager == null) {
412 444
			Log.severe("No metadata repository manager found");
413 445
			return false;
......
416 448
			Log.severe("No artifact repository manager found");
417 449
			return false;
418 450
		}
419

  
451
		
420 452
		manager.removeRepository(location);
421 453
		manager2.removeRepository(location);
422

  
454
		
423 455
		return true;
424 456
	}
425

  
457
	
426 458
	private static void patchProfile(File profileXMLFile) throws IOException {
427

  
459
		
428 460
		File tmpFile = File.createTempFile("tmp", ".profile", profileXMLFile.getParentFile());
429

  
461
		
430 462
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(profileXMLFile), "UTF-8"));
431 463
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpFile), "UTF-8"));
432

  
464
		
433 465
		String line = br.readLine();
434 466
		boolean firstLine = true;
435 467
		while (line != null) {
436 468
			if (!firstLine) {
437 469
				writer.write("\n");
438 470
			}
439

  
471
			
440 472
			String trimedLine = line.trim();
441 473
			if (trimedLine.startsWith("<property name='org.eclipse.equinox.p2.type.lock' value='")) {
442 474
				writer.write("<property name='org.eclipse.equinox.p2.type.lock' value='0'/>");
443
			} else {
475
			}
476
			else {
444 477
				writer.write(line);
445 478
			}
446
			firstLine= false;
479
			firstLine = false;
447 480
			line = br.readLine();
448 481
		}
449 482
		writer.close();
450 483
		br.close();
451

  
484
		
452 485
		if (!(profileXMLFile.delete() && tmpFile.renameTo(profileXMLFile))) {
453
			throw new IOException("failed to rewrite profile file: "+profileXMLFile);
486
			throw new IOException("failed to rewrite profile file: " + profileXMLFile);
454 487
		}
455 488
	}
456

  
489
	
457 490
	private static File findNewestGZFile(File profileDir) {
458 491
		File[] files = profileDir.listFiles(new FilenameFilter() {
492
			
459 493
			@Override
460 494
			public boolean accept(File dir, String name) {
461 495
				return name.toLowerCase().endsWith(".gz");
462 496
			}
463 497
		});
464

  
498
		
465 499
		if (files == null || files.length == 0) return null;
466

  
500
		
467 501
		Arrays.sort(files);
468
		return files[files.length-1];
502
		return files[files.length - 1];
469 503
	}
470

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

  
576

  
577

  
578
}
606
	// @Override
607
	// protected String getProgressTaskName() {
608
	// return ProvSDKMessages.UpdateHandler_ProgressTaskName;
609
	// }
610
	
611
	
612
	
613
}

Also available in: Unified diff