Skip to main content
summaryrefslogtreecommitdiffstats
blob: 81e6c8f56f7ca04e6105ada159b128a72a35eb59 (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
/*******************************************************************************
 * 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.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.ibm.icu.util.StringTokenizer;

/**
 * Insert the type's description here. Creation date: (4/11/2001 10:28:44 AM)
 * 
 * @author: Administrator
 */
public class FileDups {
	private static int sFiles = 0;
	private static int sDups = 0;
	private static int sJARs = 0;
	private static String[] excludedEntryNames = new String[]{"com/ibm/ivj/ejb/runtime/_CopyHelper_Stub.class", //$NON-NLS-1$
				"org/omg/stub/javax/ejb/_HomeHandle_Stub.class", //$NON-NLS-1$
				"org/omg/stub/javax/ejb/_Handle_Stub.class", //$NON-NLS-1$
				"org/omg/stub/javax/ejb/_EJBObject_Stub.class", //$NON-NLS-1$
				"org/omg/stub/javax/ejb/_EJBHome_Stub.class", //$NON-NLS-1$
				"org/omg/stub/java/lang/_Cloneable_Stub.class", //$NON-NLS-1$
				"com/ibm/websphere/csi/_TransactionalObject_Stub.class", //$NON-NLS-1$
				"com/ibm/websphere/csi/_CSIServant_Stub.class", //$NON-NLS-1$
				"com/ibm/ejs/container/_EJSWrapper_Tie.class", //$NON-NLS-1$
				"com/ibm/ejs/container/_EJSWrapper_Stub.class" //$NON-NLS-1$
	};
	private static HashSet excluded = new HashSet(Arrays.asList(excludedEntryNames));

	private static void addEntry(String entry, Map map, String jarName) {
		sFiles++;

		Object current = map.get(entry);
		if (current == null) {
			// This is the first time the entry is found
			map.put(entry, jarName);
		} else if (current instanceof String) {
			// There is one other entry
			map.remove(entry);

			String other = (String) current;
			LinkedList list = new LinkedList();
			list.addFirst(other);
			list.addFirst(jarName);
			map.put(entry, list);

			sDups++;
		} else {
			// There are more than one other instances
			LinkedList list = (LinkedList) current;
			list.addFirst(jarName);

			sDups++;
		}
	}

	/**
	 * Starts the application.
	 * 
	 * @param args
	 *            an array of command-line arguments
	 */
	public static void main(java.lang.String[] args) {
		if (args.length != 1) {
			StringBuffer sb = new StringBuffer();
			sb.append("FileDups utility\r\n\r\n"); //$NON-NLS-1$
			sb.append("Synopsis:\r\n"); //$NON-NLS-1$
			sb.append("Analyzes JAR and ZIP files in a directory for duplicate entries with the same name.  This excludes all entries in the \"META-INF\" directory of the archive.  This does not check timestamps or size.\r\n\r\n"); //$NON-NLS-1$
			sb.append("Usage: FileDups dirName\r\n"); //$NON-NLS-1$
			System.out.println(sb.toString());
			return;
		}

		TreeMap map = new TreeMap();

		try {
			for (int i = 0; i < args.length; i++) {
				File root = new File(args[i]);
				processDirectory(root, map);
			}

			TreeMap outputMap = new TreeMap();

			Set set = map.entrySet();
			Iterator it = set.iterator();
			while (it.hasNext()) {
				Map.Entry me = (Map.Entry) it.next();
				Object o = me.getValue();
				if (o instanceof LinkedList) {
					StringBuffer jars = new StringBuffer();

					LinkedList list = (LinkedList) o;
					Iterator entryIter = list.iterator();
					while (entryIter.hasNext()) {
						jars.append(entryIter.next());
						jars.append("\r\n"); //$NON-NLS-1$
					}

					LinkedList outputList = (LinkedList) outputMap.get(jars.toString());
					if (outputList == null) {
						outputList = new LinkedList();
						outputMap.put(jars.toString(), outputList);
					}
					outputList.addFirst(me.getKey());
				}
			}

			Set outSet = outputMap.entrySet();
			it = outSet.iterator();
			while (it.hasNext()) {
				Map.Entry me = (Map.Entry) it.next();
				String jars = (String) me.getKey();
				StringTokenizer tokens = new StringTokenizer(jars, "\r\n"); //$NON-NLS-1$

				while (tokens.hasMoreTokens()) {
					System.out.println(tokens.nextToken());
				}

				LinkedList list = (LinkedList) me.getValue();
				Iterator entryIter = list.iterator();
				while (entryIter.hasNext()) {
					System.out.println("    " + (String) entryIter.next()); //$NON-NLS-1$
				}
			}

			System.out.println("\r\nSearched " + sJARs + " jars, found " + sDups + " dups in " + sFiles + " files."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		} catch (Exception x) {
			x.printStackTrace();
		}
	}

	private static void processDirectory(File root, Map map) throws java.io.IOException {
		File[] files = root.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			if (f.isDirectory())
				processDirectory(f, map);
			else {
				String name = f.getName().toLowerCase();
				if (name.endsWith(".zip") || name.endsWith(".jar")) //$NON-NLS-1$ //$NON-NLS-2$
				{
					processFile(f, map);
				}
			}
		}
	}

	private static void processFile(File file, Map map) throws java.io.IOException {
		HashSet filesInFile = new HashSet();

		sJARs++;
		ZipFile zip = null;
		try {
			zip = org.eclipse.jst.jee.archive.internal.ArchiveUtil.newZipFile(file);
			Enumeration eNum = zip.entries();
			while (eNum.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) eNum.nextElement();
				String name = entry.getName();
				if (!name.startsWith("META-INF") && !excluded.contains(name)) //$NON-NLS-1$
					filesInFile.add(name);
			}
		} catch (Exception x) {
			System.out.println("*Error searching in " + file.getAbsolutePath()); //$NON-NLS-1$
		} finally {
			try {
				if (zip != null)
					zip.close();
			} catch (java.io.IOException ignored) {
				//Ignore
			}
		}

		// Now add the packages to the map
		String filename = file.getPath();

		Iterator it = filesInFile.iterator();
		while (it.hasNext()) {
			String entryName = (String) it.next();
			addEntry(entryName, map, filename);
		}
	}
}

Back to the top