Skip to main content
summaryrefslogtreecommitdiffstats
blob: d9afe1cfad0c607f603d75f469bab87db63aeb53 (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) 2007 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.prov.resolution;

import java.util.*;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.prov.metadata.MetadataActivator;
import org.eclipse.equinox.prov.core.helpers.LogHelper;
import org.eclipse.equinox.prov.metadata.*;
import org.eclipse.osgi.service.resolver.*;
import org.osgi.framework.InvalidSyntaxException;

public class Transformer implements IMetadataVisitor {
	private static final byte IU_KIND = 0;
	private static final String IU_NAMESPACE = "iu.namespace";
	static long iuInternalId = 0;

	private Dictionary context = null;
	private StateObjectFactory factory = null;
	private ArrayList iuCapabilities;
	private Map iuDependencies;
	private byte kind = IU_KIND;

	private BundleDescription result = null;

	private RecommendationDescriptor recommendations = null;

	public Transformer(StateObjectFactory factory) {
		this(factory, null, null);
	}

	public Transformer(StateObjectFactory factory, Dictionary context, RecommendationDescriptor recommendations) {
		this.factory = factory;
		this.context = context;
		this.recommendations = recommendations;
	}

	private String getNamespace() {
		switch (kind) {
			case IU_KIND :
				return IU_NAMESPACE;
			default :
				throw new IllegalStateException("unknown kind"); //This should not happen
		}
	}

	public BundleDescription getResult() {
		return result;
	}

	private boolean isEnabled(RequiredCapability capability) {
		// If there is no context then be optimistic
		if (context == null)
			return true;
		String filter = capability.getFilter();
		if (filter == null)
			return true;
		try {
			return MetadataActivator.getContext().createFilter(filter).match(context);
		} catch (InvalidSyntaxException e) {
			// If we fail to parse the filter treat it as invalid and be optimistic
			return true;
		}
	}

	private String toFilter(VersionRange range) {
		if (range == null)
			return null;
		StringBuffer buf = new StringBuffer();
		buf.append("(&"); //$NON-NLS-1$
		buf.append("(version>=").append(range.getMinimum().toString()).append(')'); //$NON-NLS-1$
		if (!range.getIncludeMinimum())
			buf.append("(!(version=").append(range.getMinimum().toString()).append("))");
		buf.append("(version<=").append(range.getMaximum().toString()).append(')'); //$NON-NLS-1$
		if (!range.getIncludeMaximum())
			buf.append("(!(version=").append(range.getMaximum().toString()).append("))");
		buf.append(')');
		return buf.toString();
	}

	public void visitCapability(ProvidedCapability capability) {
		iuCapabilities.add(factory.createGenericDescription(capability.getName(), capability.getNamespace(), capability.getVersion(), null));
	}

	public void visitInstallableUnit(IInstallableUnit toTransform) {
		kind = IU_KIND;

		//Start with the dependencies
		RequiredCapability[] requires = toTransform.getRequiredCapabilities();
		iuDependencies = new HashMap(requires.length);
		for (int i = 0; i < requires.length; i++) {
			requires[i].accept(this);
		}

		//Do the capabilities
		ProvidedCapability[] capabilities = toTransform.getProvidedCapabilities();
		iuCapabilities = new ArrayList(requires.length + 1);
		for (int i = 0; i < capabilities.length; i++) {
			capabilities[i].accept(this);
		}

		//Add a capability representing the IU itself
		iuCapabilities.add(factory.createGenericDescription(toTransform.getId(), getNamespace(), toTransform.getVersion(), null));

		GenericSpecification[] genericSpecifications = new GenericSpecification[iuDependencies.size()];
		iuDependencies.keySet().toArray(genericSpecifications);

		GenericDescription[] genericDescriptions = new GenericDescription[iuCapabilities.size()];
		iuCapabilities.toArray(genericDescriptions);

		//Finally create the bundle description
		//TODO Need to create the filter for the IU itself
		result = factory.createBundleDescription(iuInternalId++, toTransform.getId(), toTransform.getVersion(), (String) null, (BundleSpecification[]) null, (HostSpecification) null, (ImportPackageSpecification[]) null, (ExportPackageDescription[]) null, toTransform.isSingleton(), true, true, toTransform.getFilter(), (String[]) null, genericSpecifications, genericDescriptions);
		result.setUserObject(new StateMetadataMap(toTransform, iuDependencies));
	}

	public void visitRequiredCapability(RequiredCapability capability) {
		try {
			if (isEnabled(capability)) {
				capability = rewrite(capability);
				iuDependencies.put(factory.createGenericSpecification(capability.getName(), capability.getNamespace(), toFilter(capability.getRange()), capability.isOptional(), capability.isMultiple()), capability);
			}
		} catch (InvalidSyntaxException e) {
			LogHelper.log(new Status(IStatus.ERROR, MetadataActivator.PI_METADATA, "Invalid filter: " + e.getFilter(), e)); //$NON-NLS-1$
		}
	}

	private RequiredCapability rewrite(RequiredCapability match) {
		if (recommendations == null)
			return match;
		Recommendation foundRecommendation = recommendations.findRecommendation(match);
		return foundRecommendation != null ? foundRecommendation.newValue() : match;
	}
}

Back to the top