Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d003ee71c0dc295a399cc3454b4c4580bc4b73c (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.equinox.internal.p2.tools;

import java.net.URI;
import java.util.Arrays;
import java.util.Comparator;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.internal.p2.metadata.repository.MetadataRepositoryManager;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.internal.provisional.p2.metadata.IRequiredCapability;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager;
import org.eclipse.equinox.internal.provisional.p2.query.Collector;

public class MetadataCompareApplication implements IApplication {

	private MetadataRepositoryManager repoManager = new MetadataRepositoryManager();
	private IMetadataRepository sourceRepo = null;
	private IMetadataRepository targetRepo = null;

	private static Comparator iuIdComparator = new Comparator() {
		public int compare(Object source, Object target) {
			IInstallableUnit sourceIU = (IInstallableUnit) source;
			IInstallableUnit targetIU = (IInstallableUnit) target;
			int id = sourceIU.getId().compareTo(targetIU.getId());
			if (id != 0)
				return id;
			return sourceIU.getVersion().compareTo(targetIU.getVersion());
		}
	};

	private URI sourceLocation;
	private URI targetLocation;
	private boolean compare = false;
	private boolean list = false;

	public Object start(IApplicationContext context) throws Exception {
		initializeFromArguments((String[]) context.getArguments().get("application.args")); //$NON-NLS-1$
		initRepositories();

		if (compare) {
			compareMetadataRepositories();
		} else if (list) {
			list(sourceLocation);
			list(targetLocation);
		}
		return IApplication.EXIT_OK;
	}

	private void list(URI location) throws ProvisionException {
		if (location == null)
			return;
		IMetadataRepository locationRepo = repoManager.getRepository(location);
		if (locationRepo == null)
			return;
		Collector sourceRoots = locationRepo.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor());
		IInstallableUnit[] sourceIUs = (IInstallableUnit[]) sourceRoots.toArray(IInstallableUnit.class);

		sourceIUs = sort(sourceIUs, true);
		for (int i = 0; i < sourceIUs.length; i++) {
			System.out.print(sourceIUs[i]);
			System.out.println(sourceIUs[i].isFragment() ? " (fragment)" : ""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		System.out.println("Total: " + sourceIUs.length); //$NON-NLS-1$
	}

	private void compareMetadataRepositories() throws ProvisionException {
		System.out.println("\n" + sourceLocation + " -> " + targetLocation); //$NON-NLS-1$ //$NON-NLS-2$
		compare(sourceRepo, targetRepo);
	}

	private void initRepositories() throws ProvisionException {
		if (targetLocation == null || sourceLocation == null)
			throw new IllegalStateException("Must specify a source and target"); //$NON-NLS-1$
		sourceRepo = repoManager.loadRepository(sourceLocation, null);
		targetRepo = initializeTarget();
	}

	private IMetadataRepository initializeTarget() throws ProvisionException {
		try {
			IMetadataRepository repository = repoManager.loadRepository(targetLocation, null);
			if (!repository.isModifiable())
				throw new IllegalArgumentException("Metadata repository not modifiable: " + targetLocation); //$NON-NLS-1$
			return repository;
		} catch (ProvisionException e) {
			//fall through and create repo
		}
		String repositoryName = targetLocation + " - metadata"; //$NON-NLS-1$
		return repoManager.createRepository(targetLocation, repositoryName, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
	}

	private void compare(IMetadataRepository sourceRepo, IMetadataRepository targetRepo) {
		Collector sourceRoots = sourceRepo.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor());
		Collector targetRoots = targetRepo.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor());
		IInstallableUnit[] sourceIUs = (IInstallableUnit[]) sourceRoots.toArray(IInstallableUnit.class);
		sourceIUs = sort(sourceIUs, true);
		IInstallableUnit[] targetIUs = (IInstallableUnit[]) targetRoots.toArray(IInstallableUnit.class);
		targetIUs = sort(targetIUs, true);

		int targetIndex = 0;
		for (int i = 0; i < sourceIUs.length; i++)
			targetIndex = compareUsingTargets(sourceIUs[i], targetIUs, targetIndex);
	}

	private int compareUsingTargets(IInstallableUnit sourceIU, IInstallableUnit[] targetIUs, int targetIndex) {
		while (targetIndex < targetIUs.length) {
			int difference = iuIdComparator.compare(sourceIU, targetIUs[targetIndex]);
			if (difference < 0) {
				System.out.println(sourceIU + " is not found in target repository"); //$NON-NLS-1$
				return targetIndex;
			} else if (difference == 0) {
				String comparison = compare(sourceIU, targetIUs[targetIndex]);
				if (comparison.length() > 0)
					System.out.println(sourceIU + comparison);
				return targetIndex + 1;
			} else {
				System.out.println(targetIUs[targetIndex++] + " is not found in source repository"); //$NON-NLS-1$
			}
		}
		System.out.println(sourceIU + " is not found in target repository"); //$NON-NLS-1$
		return targetIndex;
	}

	private boolean compare(Object a, Object b) {
		if (a == null)
			return b == null;
		return a.equals(b);
	}

	private boolean compare(Object[] a, Object b[]) {
		if (a == null)
			return b == null;
		return Arrays.equals(a, b);
	}

	private String compare(IInstallableUnit iu, IInstallableUnit next) {
		if (next == null)
			return " iu artifactLocators providedCapabilities requiredCapabilities touchpointType"; //$NON-NLS-1$
		String result = ""; //$NON-NLS-1$
		if (!iu.equals(next))
			result += " iu"; //$NON-NLS-1$
		//		if (!compare(iu.getApplicabilityFilter(), next.getApplicabilityFilter()))
		//			result += " applicabilityFilter";
		if (!compare(iu.getArtifacts(), next.getArtifacts()))
			result += " artifactLocators"; //$NON-NLS-1$
		if (!compare(iu.getProvidedCapabilities(), next.getProvidedCapabilities()))
			result += " providedCapabilities"; //$NON-NLS-1$
		if (!compareRequires(iu.getRequiredCapabilities(), next.getRequiredCapabilities()))
			result += " requiredCapabilities"; //$NON-NLS-1$
		if (!compare(iu.getTouchpointType(), next.getTouchpointType()))
			result += " touchpointType"; //$NON-NLS-1$

		if (iu.isFragment()) {
			//			if (((InstallableUnitFragment) iu).getHost() == null || ((InstallableUnitFragment) iu).getVersion() == null)
			//				return result;
			//			if (!((InstallableUnitFragment) iu).getHost().equals(((InstallableUnitFragment) next).getHost()))
			//				result += " hostid";
			//			if (!((InstallableUnitFragment) iu).getVersion().equals(((InstallableUnitFragment) next).getVersion()))
			//				result += " hostversionRange";
		}
		return result;
	}

	private boolean compareRequires(IRequiredCapability[] a, IRequiredCapability[] b) {
		if (a == null)
			return b == null;
		if (a.length != b.length)
			return false;
		if (a == b)
			return true;
		for (int i = 0; i < a.length; i++)
			if (findCapability(a[i], b) == null)
				return false;
		return true;
	}

	private IRequiredCapability findCapability(IRequiredCapability target, IRequiredCapability[] b) {
		for (int i = 0; i < b.length; i++) {
			IRequiredCapability capability = b[i];
			if (target.equals(capability))
				return capability;
		}
		return null;
	}

	private IInstallableUnit[] sort(IInstallableUnit[] ius, boolean clone) {
		IInstallableUnit[] result = ius;
		if (clone) {
			result = new InstallableUnit[ius.length];
			System.arraycopy(ius, 0, result, 0, ius.length);
		}
		Arrays.sort(result, iuIdComparator);
		return result;
	}

	public void initializeFromArguments(String[] args) throws Exception {
		if (args == null)
			return;
		for (int i = 0; i < args.length; i++) {
			if (args[i].equalsIgnoreCase("-compare")) //$NON-NLS-1$
				compare = true;
			if (args[i].equalsIgnoreCase("-list")) //$NON-NLS-1$
				list = true;

			// check for args with parameters. If we are at the last argument or
			// if the next one
			// has a '-' as the first character, then we can't have an arg with
			// a parm so continue.
			if (i == args.length - 1 || args[i + 1].startsWith("-")) //$NON-NLS-1$
				continue;

			String arg = args[++i];

			if (args[i - 1].equalsIgnoreCase("-source")) //$NON-NLS-1$
				sourceLocation = new URI(arg);
			if (args[i - 1].equalsIgnoreCase("-target")) //$NON-NLS-1$
				targetLocation = new URI(arg);
		}
	}

	public void stop() {
		//do nothing
	}
}

Back to the top