Skip to main content
summaryrefslogtreecommitdiffstats
blob: d5a783767d98743cb4c442cb5d843b3f890770e0 (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
/*******************************************************************************
 * 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 13, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.jst.j2ee.application.operations;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.RuntimeClasspathEntry;
import org.eclipse.jst.j2ee.internal.common.ClasspathModel;
import org.eclipse.jst.j2ee.internal.earcreation.EAREditModel;
import org.eclipse.jst.j2ee.internal.earcreation.EARNatureRuntime;
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;


/**
 * @author schacher Helper class used to compute all the dependencies for a JAR being added to an
 *         EAR
 */
class JARDependencyTraverser {

	private IProject archiveProject;
	private IProject earProject;
	private Map result;
	private EAREditModel earEditModel;

	public JARDependencyTraverser(IProject anArchiveProject, IProject anEarProject) {
		super();
		this.archiveProject = anArchiveProject;
		this.earProject = anEarProject;
	}

	//Returns a map where the keys are instances of IResource, and the values
	// are uris within an EAR
	public Map run() {
		EARNatureRuntime[] earNatures = J2EEProjectUtilities.getReferencingEARProjects(archiveProject);
		if (earNatures == null || earNatures.length == 0 || (earNatures.length == 1 && earNatures[0].getProject() == earProject))
			return Collections.EMPTY_MAP;

		result = new HashMap();
		EARNatureRuntime runtime = EARNatureRuntime.getRuntime(earProject);
		if (runtime == null)
			return Collections.EMPTY_MAP;

		earEditModel = runtime.getEarEditModelForRead(this);
		try {
			for (int i = 0; i < earNatures.length; i++) {
				if (earNatures[i].getProject() != earProject) {
					traverseClasspath(earNatures[i]);
				}
			}
			return result;
		} finally {
			if (earEditModel != null)
				earEditModel.releaseAccess(this);
		}
	}

	private void traverseClasspath(EARNatureRuntime anEARNature) {
		ClasspathModel model = new ClasspathModel(null);
		model.setProject(archiveProject);
		model.setSelectedEARNature(anEARNature);
		ClassPathSelection cpSelection = model.getClassPathSelection();
		Archive anArchive = model.getArchive();
		RuntimeClasspathEntry[] entries = anArchive.getDependencyClassPath();
		Archive referencedArchive = null;
		for (int i = 0; i < entries.length; i++) {
			RuntimeClasspathEntry entry = entries[i];
			referencedArchive = entry.getReferencedArchive();
			if (entry.isWebLib() || referencedArchive == null)
				continue;
			String uri = referencedArchive.getURI();
			if (uri == null)
				continue;
			ClasspathElement elmt = cpSelection.getClasspathElement(uri);
			if (elmt == null)
				continue;
			IResource resource = elmt.getResource();
			if (resource != null)
				addResult(resource, uri);
		}
	}

	private void addResult(IResource res, String uri) {
		if (result.containsKey(res))
			return;
		boolean exists = false;
		switch (res.getType()) {
			case IResource.FILE :
				exists = containsJARFile((IFile) res, uri);
				break;
			case IResource.PROJECT :
				exists = earEditModel.hasMappingToProject((IProject) res);
				break;
			default :
				return;
		}
		if (!exists)
			result.put(res, uri);
	}


	/**
	 * @param file
	 * @param uri
	 * @return
	 */
	private boolean containsJARFile(IFile file, String uri) {
		IPath path = null;
		try {
			path = new Path(uri);
		} catch (IllegalArgumentException tooBad) {
			return true;
		}
		IFile existing = earProject.getFile(path);
		return (existing != null && existing.exists());

	}


}

Back to the top