Skip to main content
summaryrefslogtreecommitdiffstats
blob: da7dcfd251d7637374b90cc1e63a10124c1b30b8 (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
/*******************************************************************************
 * Copyright (c) 2001, 2006 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
 *******************************************************************************/
package org.eclipse.jst.j2ee.commonarchivecore.internal.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.RuntimeClasspathEntry;
import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.RuntimeClasspathEntryImpl;

import com.ibm.icu.util.StringTokenizer;


public class ClasspathUtil {

	public static void processManifest(String inPath, List rtClasspath, Set processedPaths) {
		ArrayList paths = getManifestPaths(inPath);
		for (int i = 0; i < paths.size(); i++) {
			RuntimeClasspathEntry entry = (RuntimeClasspathEntry) paths.get(i);
			if (!processedPaths.contains(entry)) {
				processedPaths.add(entry);
				rtClasspath.add(entry);
				processManifest(entry.getAbsolutePath(), rtClasspath, processedPaths);
			}
		}
	}

	protected static RuntimeClasspathEntry createRuntimeClasspathEntry(String absolutePath, String manifestValue) {
		RuntimeClasspathEntry entry = new RuntimeClasspathEntryImpl();
		entry.setAbsolutePath(absolutePath);
		entry.setManifestValue(manifestValue);
		return entry;
	}

	protected static ArrayList getManifestPaths(String path) {
		ArrayList manifestPaths = new ArrayList();
		File f = new File(path);
		if (f.exists()) {
			if (f.isDirectory()) {
				File manifestFile = new File(path + File.separator + "META-INF" + File.separator + "MANIFEST.MF"); //$NON-NLS-1$ //$NON-NLS-2$
				if (manifestFile.exists()) {
					FileInputStream inputStream = null;
					try {
						inputStream = new FileInputStream(manifestFile);
						Manifest manifest = new Manifest(inputStream);
						manifestPaths = getManifestClassPaths(manifest, f.getParent());
					} catch (IOException e) {
						//Ignore
					} finally {
						if (null != inputStream) {
							try {
								inputStream.close();
							} catch (IOException ex) {
								Logger.getLogger().logError(ex);
							}
						}
					}
				}
			} else {
				JarFile jarFile = null;
				try {
					jarFile = new JarFile(f);
					Manifest manifest = jarFile.getManifest();
					if (manifest != null)
						manifestPaths = getManifestClassPaths(manifest, f.getParent());
				} catch (IOException e) {
					//Ignore
				} finally {
					if (jarFile != null)
						try {
							jarFile.close();
						} catch (IOException ex) {
							//Ignore
						}
				}
			}
		}
		return manifestPaths;
	}

	protected static ArrayList getManifestClassPaths(Manifest manifest, String archivePath) {
		ArrayList pathList = new ArrayList();
		Attributes main = manifest.getMainAttributes();
		String classPath = main.getValue(Attributes.Name.CLASS_PATH);
		if (classPath != null) {
			StringTokenizer st = new StringTokenizer(classPath, " "); //$NON-NLS-1$
			while (st.hasMoreTokens()) {
				String token = st.nextToken();
				String path = archivePath + File.separator + token;
				path = normalizePath(path);
				if (new File(path).exists()) {
					pathList.add(createRuntimeClasspathEntry(path, token));
				}
			}
		}
		return pathList;
	}

	public static String normalizePath(String path) {
		File f = new File(path);
		try {
			f = f.getCanonicalFile();
		} catch (Exception ex) {
			//Ignore
		}
		return f.getPath();
	}
}

Back to the top