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: a448a4a93dc02c5d59e92c0a5734268e30d0dfdc (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
/*******************************************************************************
 * 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 Dec 5, 2003
 * 
 * To change the template for this generated file go to Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.jst.j2ee.internal.web.archive.operations;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

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.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jst.j2ee.application.operations.J2EEImportDataModel;
import org.eclipse.jst.j2ee.application.operations.J2EEProjectCreationDataModel;
import org.eclipse.jst.j2ee.application.operations.J2EEUtilityJarImportDataModel;
import org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.SaveStrategy;
import org.eclipse.jst.j2ee.internal.archive.operations.BinaryProjectHelper;
import org.eclipse.jst.j2ee.internal.archive.operations.J2EEImportOperation;
import org.eclipse.jst.j2ee.internal.project.J2EENature;
import org.eclipse.jst.j2ee.internal.web.operations.J2EEWebNatureRuntime;
import org.eclipse.jst.j2ee.internal.web.operations.J2EEWebNatureRuntimeUtilities;
import org.eclipse.jst.j2ee.internal.web.operations.WebSettingsMigrator;
import org.eclipse.wst.common.frameworks.internal.operations.WTPOperation;
import org.eclipse.wst.web.internal.operation.LibModule;

import com.ibm.wtp.common.logger.proxy.Logger;

/**
 * @author cbridgha
 * 
 * To change the template for this generated type comment go to Window>Preferences>Java>Code
 * Generation>Code and Comments
 */
public class WARImportOperation extends J2EEImportOperation {
	/**
	 * @param model
	 */
	public WARImportOperation(WARImportDataModel model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibm.etools.archive.j2ee.operations.J2EEImportOperationNEW#createModuleProject(org.eclipse.jst.j2ee.internal.internal.application.operations.J2EEProjectCreationDataModel,
	 *      org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected void createModuleProject(J2EEProjectCreationDataModel model, IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
		WebProjectCreationOperation op = new WebProjectCreationOperation((WebProjectCreationDataModel) model);
		op.run(monitor);
	}

	protected void modifyStrategy(SaveStrategy saveStrat) {
		WTProjectSaveStrategyImpl strategy = (WTProjectSaveStrategyImpl) saveStrat;
		if (null != strategy.getOverwriteHandler()) {
			strategy.getOverwriteHandler().setWarSaveStrategy(strategy);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibm.etools.archive.j2ee.operations.J2EEImportOperationNEW#createSaveStrategy(org.eclipse.core.resources.IProject)
	 */
	protected SaveStrategy createSaveStrategy(IProject project) {
		WTProjectSaveStrategyImpl saveStrat = new WTProjectSaveStrategyImpl(project);
		saveStrat.setDataModel((WARImportDataModel) operationDataModel);
		return saveStrat;
	}

	protected void doExecute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
		super.doExecute(monitor);
		WARImportDataModel model = (WARImportDataModel) operationDataModel;
		J2EEWebNatureRuntime nature = J2EEWebNatureRuntimeUtilities.getJ2EERuntime(model.getProject());
		if (!model.getBooleanProperty(J2EEImportDataModel.PRESERVE_PROJECT_METADATA)) {
			IFolder folder = model.getProject().getFolder(nature.getLibraryFolder().getFullPath().removeFirstSegments(1));
			if (!folder.exists()) {
				folder.create(true, true, new NullProgressMonitor());
			}
		}

		addExtraClasspathEntries(monitor, model);
		IProject project = model.getProject();
		if (model.getBooleanProperty(J2EEImportDataModel.PRESERVE_PROJECT_METADATA)) {
			BinaryProjectHelper.removeImportedClassesFromClasspathIfNecessary(project);
		}
		//J2EEWebNatureRuntime webNature = J2EEWebNatureRuntime.getRuntime(project);
		//webNature.getWebSettings().write();
		//project.getFile(webNature.getWebSettingsPath()).refreshLocal(0, monitor);
		WebSettingsMigrator migrator = new WebSettingsMigrator();
		migrator.migrate(project);
		if (!model.getJ2eeProjectCreationDataModel().getBooleanProperty(J2EEProjectCreationDataModel.ADD_SERVER_TARGET))
			addServerTarget(monitor);
	}

	private void addExtraClasspathEntries(IProgressMonitor monitor, WARImportDataModel model) throws InvocationTargetException, InterruptedException, CoreException, JavaModelException {
		boolean preserveMetadata = model.getBooleanProperty(J2EEImportDataModel.PRESERVE_PROJECT_METADATA);
		List extraEntries = null;
		IJavaProject javaProject = JavaCore.create(model.getProject());
		if (!preserveMetadata)
			extraEntries = new ArrayList();
		importWebLibraryProjects(monitor, extraEntries, javaProject);

		if (!preserveMetadata) {
			IResource[] libs = J2EEWebNatureRuntimeUtilities.getJ2EERuntime(model.getProject()).getLibraryFolder().members();
			for (int i = 0; i < libs.length; i++) {
				if (!javaProject.isOnClasspath(libs[i]))
					extraEntries.add(JavaCore.newLibraryEntry(libs[i].getFullPath(), libs[i].getFullPath(), null));
			}
			addToClasspath(model, extraEntries);
		}
	}

	private void importWebLibraryProjects(IProgressMonitor monitor, List extraEntries, IJavaProject javaProject) throws InvocationTargetException, InterruptedException {
		boolean preserveMetadata = operationDataModel.getBooleanProperty(J2EEImportDataModel.PRESERVE_PROJECT_METADATA);
		List libProjects = (List) operationDataModel.getProperty(WARImportDataModel.HANDLED_ARCHIVES);
		J2EEUtilityJarImportDataModel importModel = null;
		WTPOperation importOperation = null;
		ArrayList libModules = new ArrayList();
		for (int i = 0; null != libProjects && i < libProjects.size(); i++) {
			importModel = (J2EEUtilityJarImportDataModel) libProjects.get(i);
			libModules.add(new LibModule(importModel.getArchiveFile().getName(), importModel.getProject().getName()));
			importOperation = importModel.getDefaultOperation();
			importOperation.run(monitor);
			if (extraEntries != null) {
				if (!javaProject.isOnClasspath(importModel.getProject())) {
					if (preserveMetadata) {
						extraEntries.add(JavaCore.newLibraryEntry(importModel.getProject().getFullPath(), importModel.getProject().getFullPath(), null));
					} else {
						extraEntries.add(JavaCore.newProjectEntry(importModel.getProject().getFullPath()));
					}
				}
			}
		}
		LibModule[] libModulesArray = new LibModule[libModules.size()];
		for (int i = 0; i < libModules.size(); i++) {
			libModulesArray[i] = (LibModule) libModules.get(i);
		}
		J2EEWebNatureRuntime webNature = (J2EEWebNatureRuntime) J2EENature.getRegisteredRuntime(((WARImportDataModel) operationDataModel).getProject());
		try {
			webNature.setLibModules(libModulesArray);
		} catch (CoreException e) {
			Logger.getLogger().logError(e);
		}
	}
}

Back to the top