Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: d6a89e0a4e5abebfae6c2e623388c4f806a51b15 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/***************************************************************************************************
 * 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 Oct 27, 2003
 * 
 * To change the template for this generated file go to Window>Preferences>Java>Code
 * Generation>Code and Comments
 */
package org.eclipse.jst.j2ee.application.internal.operations;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jst.j2ee.application.Application;
import org.eclipse.jst.j2ee.application.ApplicationResource;
import org.eclipse.jst.j2ee.application.Module;
import org.eclipse.jst.j2ee.internal.application.impl.ApplicationResourceImpl;
import org.eclipse.jst.j2ee.internal.archive.operations.EJBArchiveOpsResourceHandler;
import org.eclipse.jst.j2ee.internal.earcreation.AddModuleToEARProjectCommand;
import org.eclipse.jst.j2ee.internal.earcreation.AddUtilityJARMapCommand;
import org.eclipse.jst.j2ee.internal.earcreation.EARNatureRuntime;
import org.eclipse.jst.j2ee.internal.earcreation.RemoveModuleFromEARProjectCommand;
import org.eclipse.jst.j2ee.internal.modulecore.util.EARArtifactEditOperation;
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
import org.eclipse.jst.j2ee.internal.servertarget.ServerTargetDataModel;
import org.eclipse.jst.j2ee.internal.servertarget.ServerTargetOperation;
import org.eclipse.wst.common.componentcore.internal.WorkbenchComponent;
import org.eclipse.wst.common.frameworks.internal.operations.WTPOperation;
import org.eclipse.wst.server.core.IRuntime;
import org.eclipse.wst.server.core.ServerCore;

public class AddArchiveProjectToEAROperation extends EARArtifactEditOperation {

	private IProgressMonitor monitor;

	private static class CopyJARFileCommand extends AbstractCommand implements Command {

		IProject earProject = null;
		IProgressMonitor monitor = null;
		IFile file = null;
		String uri = null;
		IFile target = null;

		/**
		 * Constructor
		 */
		public CopyJARFileCommand(IProject earProject, IProgressMonitor monitor, IFile file, String uri) {
			super();
			this.earProject = earProject;
			this.monitor = monitor;
			this.file = file;
			this.uri = uri;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.emf.common.command.Command#execute()
		 */
		public void execute() {
			IPath path = null;
			try {
				path = new Path(uri);
				target = earProject.getFile(path);
				mkdirs(path);
				file.copy(target.getFullPath(), true, new SubProgressMonitor(new NullProgressMonitor(), 1));
			} catch (Exception ex) {
				Logger.getLogger().logError(ex);
			}


		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.emf.common.command.Command#undo()
		 */
		public void undo() {
			try {
				if (target != null)
					target.delete(true, new NullProgressMonitor());
			} catch (CoreException e) {
				Logger.getLogger().logError(e);
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.emf.common.command.Command#redo()
		 */
		public void redo() {
			execute();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.emf.common.command.AbstractCommand#prepare()
		 */
		protected boolean prepare() {
			return true;
		}

		/**
		 * @param path
		 * @throws CoreException
		 */
		private void mkdirs(IPath path) throws CoreException {
			int segments = path.segmentCount();
			for (int i = segments - 1; i > 0; i--) {
				IFolder folder = earProject.getFolder(path.removeLastSegments(i));
				if (!folder.exists())
					folder.create(true, true, null);
			}
		}
	}

	public AddArchiveProjectToEAROperation(AddArchiveToEARDataModel dataModel) {
		super(dataModel);
	}

	protected void execute(IProgressMonitor aMonitor) throws CoreException, InvocationTargetException, InterruptedException {
		this.monitor = aMonitor;
		AddArchiveToEARDataModel dataModel = (AddArchiveToEARDataModel) operationDataModel;
		String originalID = null;
		ApplicationResource resource = null;
		String uri = dataModel.getStringProperty(AddArchiveToEARDataModel.ARCHIVE_URI);
		EARNatureRuntime nature = EARNatureRuntime.getRuntime(dataModel.getTargetProject());
		Application app = nature.getApplication();
		Module module = app.getFirstModule(uri);
		if (module != null) {
			resource = (ApplicationResource) module.eResource();
			originalID = resource.getID(module);
		}

		Command command = createCommand(dataModel);
		if (null != command) {
			getCommandStack().execute(command);
		}

		if (originalID != null) {
			module = app.getFirstModule(uri);
			resource = (ApplicationResourceImpl) module.eResource();
			resource.setID(module, originalID);
		}

		addServerTarget(aMonitor);
		if (!dataModel.isModuleArchive())
			updateUtilProject((AddUtilityProjectToEARDataModel) dataModel, aMonitor);
		addAllDependentJARs();

	}

	private void addAllDependentJARs() throws CoreException {
		Map dependents = computeAllDependents();
		if (dependents.isEmpty())
			return;
		Iterator iter = dependents.entrySet().iterator();
		Map.Entry entry = null;
		IResource resource = null;
		String uri = null;
		CompoundCommand compoundCommand = new CompoundCommand();
		compoundCommand.setLabel(EJBArchiveOpsResourceHandler.getString("Copy_Dep_JARS_UI")); //$NON-NLS-1$
		while (iter.hasNext()) {
			entry = (Map.Entry) iter.next();
			resource = (IResource) entry.getKey();
			uri = (String) entry.getValue();
			switch (resource.getType()) {
				case IResource.FILE :
					addDependentJARFile((IFile) resource, uri, compoundCommand);
					break;
				case IResource.PROJECT :
					addDependentJARProject((IProject) resource, uri, compoundCommand);
					break;
			}
		}
		getCommandStack().execute(compoundCommand);
	}

	/**
	 * @param project
	 * @param uri
	 * @param compoundCommand
	 */
	private void addDependentJARProject(IProject project, String uri, CompoundCommand compoundCommand) {
		AddUtilityJARMapCommand cmd = new AddUtilityJARMapCommand(getEARProject(), uri, project);
		compoundCommand.append(cmd);
	}

	/**
	 * @param file
	 * @param uri
	 * @param compoundCommand
	 * @throws CoreException
	 */
	private void addDependentJARFile(IFile file, String uri, CompoundCommand compoundCommand) throws CoreException {
		CopyJARFileCommand cmd = new CopyJARFileCommand(getEARProject(), monitor, file, uri);
		compoundCommand.append(cmd);
	}

	protected void addServerTarget(IProgressMonitor aMonitor) throws CoreException, InvocationTargetException, InterruptedException {
		if (operationDataModel.getBooleanProperty(AddArchiveToEARDataModel.SYNC_TARGET_RUNTIME)) {
			ServerTargetDataModel model = ((AddArchiveToEARDataModel) operationDataModel).getServerTargetDataModel();
			ServerTargetOperation serverTargetOperation = new ServerTargetOperation(model);
			serverTargetOperation.doRun(aMonitor);
		}
	}

	/**
	 * @param model
	 */
	private void updateUtilProject(AddUtilityProjectToEARDataModel model, IProgressMonitor aMonitor) {
		WorkbenchComponent wbComp = (WorkbenchComponent) model.getProperty(AddArchiveToEARDataModel.ARCHIVE_MODULE);
		IProject proj = ((AddArchiveToEARDataModel)operationDataModel).getProjectForGivenComponent(wbComp);
		if (proj != null && J2EEProjectUtilities.getFirstReferencingEARProject(proj) == null) {
			createManifest(model, aMonitor);
			IRuntime runtime = ServerCore.getProjectProperties(proj).getRuntimeTarget();
			if (runtime == null)
				addRuntimeTarget(model, aMonitor);
		}
	}

	/**
	 * @param model
	 */
	private void addRuntimeTarget(AddUtilityProjectToEARDataModel model, IProgressMonitor aMonitor) {
		ServerTargetOperation op = new ServerTargetOperation(model.getServerTargetModel());
		runNestedOperation(op, aMonitor);
	}

	/**
	 * @param model
	 */
	private void createManifest(AddUtilityProjectToEARDataModel model, IProgressMonitor aMonitor) {
		UpdateManifestOperation op = new UpdateManifestOperation(model.getManifestModel());
		runNestedOperation(op, aMonitor);
	}

	private void runNestedOperation(WTPOperation op, IProgressMonitor aMonitor) {
		try {
			op.doRun(aMonitor);
		} catch (CoreException e) {
			Logger.getLogger().logError(e);
		} catch (InvocationTargetException e) {
			Logger.getLogger().logError(e);
		} catch (InterruptedException e) {
			Logger.getLogger().logError(e);
		}
	}

	/**
	 * @param dataModel
	 * @return
	 */
	private Command createCommand(AddArchiveToEARDataModel dataModel) {
		IProject project = getArchiveProject();
		String uri = dataModel.getStringProperty(AddArchiveToEARDataModel.ARCHIVE_URI);
		EARNatureRuntime nature = EARNatureRuntime.getRuntime(dataModel.getTargetProject());
		if (project == nature.getMappedProject(uri)) {
			return null;
		}

		Module m = null;
		//TODO fix up ((EAREditModel) editModel).getApplication().getFirstModule(uri);
		RemoveModuleFromEARProjectCommand removeCommand = null;
		if (null != m) {
			removeCommand = new RemoveModuleFromEARProjectCommand(m, dataModel.getTargetProject());
		}
		Command addCommand = null;
		if (dataModel.isModuleArchive()) {
			String contextRoot = null;
			/*if (((AddModuleToEARDataModel) dataModel).isWebModuleArchive())
				contextRoot = dataModel.getStringProperty(iAddWebModuleToEARDataModel.CONTEXT_ROOT);
			*/addCommand = new AddModuleToEARProjectCommand(project, dataModel.getTargetProject(), uri, contextRoot, null);
		} else
			addCommand = new AddUtilityJARMapCommand(dataModel.getTargetProject(), uri, project);

		if (null != removeCommand) {
			return removeCommand.chain(addCommand);
		}
		return addCommand;
	}

	private IProject getArchiveProject() {
	    WorkbenchComponent wbComp = (WorkbenchComponent) operationDataModel.getProperty(AddArchiveToEARDataModel.ARCHIVE_MODULE);
		return ((AddArchiveToEARDataModel)operationDataModel).getProjectForGivenComponent(wbComp);
	}

	private IProject getEARProject() {
		return ((AddArchiveToEARDataModel)operationDataModel).getTargetProject();
	}

	private Map computeAllDependents() {
		IProject archiveProject = getArchiveProject();
		IProject earProject = getEARProject();
		if (archiveProject == null || earProject == null)
			return Collections.EMPTY_MAP;
		return new JARDependencyTraverser(archiveProject, earProject).run();
	}
}

Back to the top