Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 224110
Collapse All | Expand All

(-)src/org/eclipse/equinox/internal/p2/reconciler/dropins/ProfileSynchronizer.java (-74 / +45 lines)
Lines 27-39 Link Here
27
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
27
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
28
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
28
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
29
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
29
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
30
import org.eclipse.equinox.internal.provisional.p2.query.Query;
31
import org.osgi.framework.*;
30
import org.osgi.framework.*;
32
31
33
/**
32
/**
34
 * Synchronizes a profile with a set of repositories.
33
 * Synchronizes a profile with a set of repositories.
35
 */
34
 */
36
public class ProfileSynchronizer {
35
public class ProfileSynchronizer {
36
	private static final String SUPER_IU = "org.eclipse.equinox.p2.dropins"; //$NON-NLS-1$
37
37
38
	public class ListCollector extends Collector {
38
	public class ListCollector extends Collector {
39
		public List getList() {
39
		public List getList() {
Lines 41-47 Link Here
41
		}
41
		}
42
	}
42
	}
43
43
44
	private static final String SYNCH_REPOSITORY_ID = "synch.repository.id"; //$NON-NLS-1$
45
	private static final String CACHE_EXTENSIONS = "org.eclipse.equinox.p2.cache.extensions"; //$NON-NLS-1$
44
	private static final String CACHE_EXTENSIONS = "org.eclipse.equinox.p2.cache.extensions"; //$NON-NLS-1$
46
	private static final String PIPE = "|"; //$NON-NLS-1$
45
	private static final String PIPE = "|"; //$NON-NLS-1$
47
	final IProfile profile;
46
	final IProfile profile;
Lines 148-154 Link Here
148
147
149
	private IInstallableUnit createRootIU(List children) {
148
	private IInstallableUnit createRootIU(List children) {
150
		InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription();
149
		InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription();
151
		iu.setId("org.eclipse.equinox.p2.dropins");
150
		iu.setId(SUPER_IU);
152
		iu.setVersion(new Version("1.0.0.v" + System.currentTimeMillis()));
151
		iu.setVersion(new Version("1.0.0.v" + System.currentTimeMillis()));
153
		List required = new ArrayList();
152
		List required = new ArrayList();
154
		for (Iterator iter = children.iterator(); iter.hasNext();) {
153
		for (Iterator iter = children.iterator(); iter.hasNext();) {
Lines 161-221 Link Here
161
	}
160
	}
162
161
163
	private ProfileChangeRequest createProfileChangeRequest(ProvisioningContext context) {
162
	private ProfileChangeRequest createProfileChangeRequest(ProvisioningContext context) {
164
		boolean modified = false;
165
		Collection profileIUs = getProfileIUs();
166
		Collection toRemove = getStaleIUs();
167
		profileIUs.removeAll(toRemove);
168
169
		ProfileChangeRequest request = new ProfileChangeRequest(profile);
170
		List toAdd = new ArrayList();
163
		List toAdd = new ArrayList();
171
		List defaults = new ArrayList();
164
		List defaults = new ArrayList();
172
		for (Iterator it = repositoryMap.entrySet().iterator(); it.hasNext();) {
165
173
			Entry entry = (Entry) it.next();
166
		Collector allIUs = getAllIUsFromRepos();
174
			String repositoryId = (String) entry.getKey();
167
175
			IMetadataRepository repository = (IMetadataRepository) entry.getValue();
168
		//Nothing has changed
176
			Iterator repositoryIterator = repository.query(InstallableUnitQuery.ANY, new Collector(), null).iterator();
169
		IInstallableUnit previous = getIU(SUPER_IU);
177
			while (repositoryIterator.hasNext()) {
170
		//Empty repo
178
				IInstallableUnit iu = (IInstallableUnit) repositoryIterator.next();
171
179
				//the IU is present in the profile and in the repository, so it's in sync
172
		ProfileChangeRequest request = new ProfileChangeRequest(profile);
180
				if (profileIUs.contains(iu))
173
		if (allIUs.size() == 0) {
181
					continue;
174
			if (previous == null)
182
				if (toRemove.contains(iu)) {
175
				return null;
183
					//the IU has been removed from one repository, but it exists in another repository
176
184
					toRemove.remove(iu);
177
			//Request the removal of the super IU
185
				} else {
178
			request.removeInstallableUnits(new IInstallableUnit[] {previous});
186
					//the IU exists in the repository, but not in the profile, so it needs to be added
179
			return request;
187
					defaults.add(createDefaultIU(iu));
180
		}
188
					toAdd.add(createIncludedIU(iu));
181
189
					if (Boolean.valueOf(iu.getProperty(IInstallableUnit.PROP_TYPE_GROUP)).booleanValue())
182
		for (Iterator iterator = allIUs.iterator(); iterator.hasNext();) {
190
						request.setInstallableUnitProfileProperty(iu, IInstallableUnit.PROP_PROFILE_ROOT_IU, Boolean.TRUE.toString());
183
			IInstallableUnit iu = (IInstallableUnit) iterator.next();
191
				}
184
			defaults.add(createDefaultIU(iu));
192
				//always set this property because the IU may move to another repository
185
			toAdd.add(createIncludedIU(iu));
193
				request.setInstallableUnitProfileProperty(iu, SYNCH_REPOSITORY_ID, repositoryId);
186
			if (Boolean.valueOf(iu.getProperty(IInstallableUnit.PROP_TYPE_GROUP)).booleanValue())
194
				profileIUs.add(iu);
187
				request.setInstallableUnitProfileProperty(iu, IInstallableUnit.PROP_PROFILE_ROOT_IU, Boolean.TRUE.toString());
195
				modified = true;
196
			}
197
		}
188
		}
198
189
199
		List extra = new ArrayList();
190
		List extra = new ArrayList();
200
		extra.addAll(defaults);
191
		extra.addAll(defaults);
201
		extra.addAll(toAdd);
192
		extra.addAll(toAdd);
202
		context.setExtraIUs(extra);
193
		context.setExtraIUs(extra);
194
203
		// only add one IU to the request. it will contain all the other IUs we want to install
195
		// only add one IU to the request. it will contain all the other IUs we want to install
204
		IInstallableUnit rootIU = createRootIU(toAdd);
196
		IInstallableUnit rootIU = createRootIU(toAdd);
205
		request.addInstallableUnits(new IInstallableUnit[] {rootIU});
197
		request.addInstallableUnits(new IInstallableUnit[] {rootIU});
206
198
207
		//the remaining IUs in toRemove don't exist in any repository, so remove from profile
199
		//Request the removal of the previous super IU
208
		if (!toRemove.isEmpty()) {
200
		if (previous != null)
209
			request.removeInstallableUnits((IInstallableUnit[]) toRemove.toArray(new IInstallableUnit[0]));
201
			request.removeInstallableUnits(new IInstallableUnit[] {previous});
210
			modified = true;
211
		}
212
213
		if (!modified)
214
			return null;
215
216
		return request;
202
		return request;
217
	}
203
	}
218
204
205
	private Collector getAllIUsFromRepos() {
206
		Collector allRepos = new Collector();
207
		for (Iterator it = repositoryMap.entrySet().iterator(); it.hasNext();) {
208
			Entry entry = (Entry) it.next();
209
			IMetadataRepository repository = (IMetadataRepository) entry.getValue();
210
			repository.query(InstallableUnitQuery.ANY, allRepos, null).iterator();
211
		}
212
		return allRepos;
213
	}
214
219
	private IInstallableUnit createIncludedIU(IInstallableUnit iu) {
215
	private IInstallableUnit createIncludedIU(IInstallableUnit iu) {
220
		InstallableUnitDescription iud = new MetadataFactory.InstallableUnitDescription();
216
		InstallableUnitDescription iud = new MetadataFactory.InstallableUnitDescription();
221
		iud.setId(iu.getId());
217
		iud.setId(iu.getId());
Lines 233-269 Link Here
233
		return MetadataFactory.createInstallableUnit(iud);
229
		return MetadataFactory.createInstallableUnit(iud);
234
	}
230
	}
235
231
236
	/**
232
	private IInstallableUnit getIU(String iuId) {
237
	 * Returns all IUs that are no longer in the repository they were in last
233
		ListCollector collector = new ListCollector();
238
	 * time we synchronized.
234
		profile.query(new InstallableUnitQuery(iuId), collector, null);
239
	 */
235
		if (collector.size() >= 0)
240
	private Collection getStaleIUs() {
236
			return (IInstallableUnit) collector.iterator().next();
241
		Query removeQuery = new Query() {
237
		return null;
242
			public boolean isMatch(Object object) {
243
				IInstallableUnit iu = (IInstallableUnit) object;
244
				String repositoryId = profile.getInstallableUnitProperty(iu, SYNCH_REPOSITORY_ID);
245
				if (repositoryId == null)
246
					return false;
247
248
				IMetadataRepository repo = (IMetadataRepository) repositoryMap.get(repositoryId);
249
				Query iuQuery = new InstallableUnitQuery(iu.getId(), iu.getVersion());
250
				return (repo == null || repo.query(iuQuery, new Collector(), null).isEmpty());
251
			}
252
		};
253
		ListCollector listCollector = new ListCollector();
254
		profile.query(removeQuery, listCollector, null);
255
		List result = listCollector.getList();
256
		return (result != null) ? result : Collections.EMPTY_LIST;
257
	}
258
259
	/**
260
	 * Returns all the IUs that are currently in the profile
261
	 */
262
	private List getProfileIUs() {
263
		ListCollector listCollector = new ListCollector();
264
		profile.query(InstallableUnitQuery.ANY, listCollector, null);
265
		List result = listCollector.getList();
266
		return (result != null) ? result : Collections.EMPTY_LIST;
267
	}
238
	}
268
239
269
	private ProvisioningPlan createProvisioningPlan(ProfileChangeRequest request, ProvisioningContext provisioningContext, IProgressMonitor monitor) {
240
	private ProvisioningPlan createProvisioningPlan(ProfileChangeRequest request, ProvisioningContext provisioningContext, IProgressMonitor monitor) {

Return to bug 224110