Skip to main content
summaryrefslogtreecommitdiffstats
blob: af1899973988ebfccd0b88fc2e4ca1b589276607 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Sep 27, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.eclipse.jst.j2ee.internal.rename;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jst.j2ee.application.Module;
import org.eclipse.jst.j2ee.application.WebModule;
import org.eclipse.jst.j2ee.internal.earcreation.EAREditModel;
import org.eclipse.jst.j2ee.internal.earcreation.EARNatureRuntime;
import org.eclipse.jst.j2ee.internal.earcreation.modulemap.ModuleMapping;
import org.eclipse.wst.common.frameworks.operations.WTPOperation;



/**
 * @author mdelder
 * 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java -
 * Code Style - Code Templates
 */
public class UpdateWebContextRootMetadataOperation extends WTPOperation {

	private IProject newWebProject;
	private String newContextRoot;

	public UpdateWebContextRootMetadataOperation(IProject newWebProject, String newContextRoot) {
		this.newWebProject = newWebProject;
		this.newContextRoot = newContextRoot;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.frameworks.internal.operation.WTPOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {

		if (this.newWebProject == null)
			return;

		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		EAREditModel model = null;
		if (this.newWebProject.isAccessible()) {

			IProject[] allProjects = root.getProjects();
			for (int i = 0; i < allProjects.length; i++) {
				EARNatureRuntime earNature = EARNatureRuntime.getRuntime(allProjects[i]);
				if (earNature != null) {
					model = earNature.getEarEditModelForWrite(this);
					try {
						ModuleMapping mapping = model.getModuleMapping(this.newWebProject);
						if (mapping != null) {
							Module module = mapping.getModule();
							if (module != null && module.isWebModule()) // should always be true
								((WebModule) module).setContextRoot(this.newContextRoot);
						}
					} finally {
						if (model != null) {
							model.saveIfNecessary(monitor, this);
							model.releaseAccess(this);
							model = null;
						}
					}
				}
			}
		}
	}

}

Back to the top