Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a894f24fbc3d8cb0f1ed9f9048d0b929d2ee7428 (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
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.internal.module;

import java.util.ArrayList;
import org.eclipse.osgi.service.resolver.*;

public class ResolverImport {
	private ImportPackageSpecification importPackageSpecification;
	private ResolverExport matchingExport;
	private ResolverBundle bundle;
	// Wirings we know will cause the module to become unresolvable (due to grouping dependencies)
	private ArrayList unresolvableWirings = new ArrayList();
	private String name = null;

	ResolverImport(ResolverBundle bundle, ImportPackageSpecification ips) {
		this.bundle = bundle;
		importPackageSpecification = ips;
	}

	boolean isFromFragment() {
		return importPackageSpecification.getBundle().getHost() != null;
	}

	String getName() {
		if (name != null) {
			return name;
		} else {
			return importPackageSpecification.getName();
		}
	}

	void setName(String name) {
		this.name = name;
	}

	ResolverBundle getBundle() {
		return bundle;
	}

	BundleDescription getActualBundle() {
		return bundle.getBundle();
	}

	boolean isSatisfiedBy(ResolverExport re) {
		// first check permissions
		if (!bundle.getResolver().getPermissionChecker().checkImportPermission(importPackageSpecification, re.getExportPackageDescription()))
			return false;
		// See if the import is satisfied
		if (!importPackageSpecification.isSatisfiedBy(re.getExportPackageDescription())) {
			return false;
		}
		return true;
	}

	ResolverExport getMatchingExport() {
		return matchingExport;
	}

	void setMatchingExport(ResolverExport matchingExport) {
		this.matchingExport = matchingExport;
	}

	ResolverExport getRoot() {
		ResolverImport ri = this;
		ResolverExport re = ri.getMatchingExport();
		while (re != null && !re.getExportPackageDescription().isRoot()) {
			ResolverBundle reExporter = re.getExporter();
			ri = reExporter.getImport(re.getName());
			if (ri != null) {
				re = ri.getMatchingExport();
				continue;
			}
			// If there is no import then we need to try going thru the requires
			re = getRootRequires(re, reExporter);
		}
		return re;
	}

	// Recurse down the requires, until we find the root export
	private ResolverExport getRootRequires(ResolverExport re, ResolverBundle reExporter) {
		BundleConstraint[] requires = reExporter.getRequires();
		for (int i = 0; i < requires.length; i++) {
			if (requires[i].getMatchingBundle() == null)
				continue;
			ResolverExport[] exports = requires[i].getMatchingBundle().getExportPackages();
			for (int j = 0; j < exports.length; j++) {
				if (re.getName().equals(exports[j].getName())) {
					return exports[j];
				}
			}
			re = getRootRequires(re, requires[i].getMatchingBundle());
			if (re.getExportPackageDescription().isRoot())
				return re;
		}
		return re;
	}

	boolean isOnRootPath(ResolverBundle rb) {
		ResolverImport ri = this;
		ResolverExport re = ri.getMatchingExport();
		if (re.getExporter() == rb)
			return true;
		while (re != null && !re.getExportPackageDescription().isRoot()) {
			ResolverBundle reExporter = re.getExporter();
			ri = reExporter.getImport(re.getName());
			if (ri != null) {
				re = ri.getMatchingExport();
				if (re.getExporter() == rb)
					return true;
				continue;
			}
			re = getRootRequires(re, reExporter);
			if (re.getExporter() == rb)
				return true;
		}
		return false;
	}

	boolean isOnRootPathSplit(ResolverBundle bundle, ResolverBundle toFind) {
		if (bundle == null)
			return false;
		BundleConstraint[] requires = bundle.getRequires();
		for (int i = 0; i < requires.length; i++) {
			if (requires[i].getMatchingBundle() == toFind)
				return true;
			if (isOnRootPathSplit(requires[i].getMatchingBundle(), toFind))
				return true;
		}
		return false;
	}

	// Records an unresolvable wiring for this import (grouping dependencies)
	void addUnresolvableWiring(ResolverBundle module) {
		unresolvableWirings.add(module);
	}

	void removeUnresolvableWiring(ResolverBundle module) {
		unresolvableWirings.remove(module);
	}

	// Clear the list of all the unresovable wirings. This is called when we move
	// the module to UNRESOLVED so we can start from scratch if we ever try again
	void clearUnresolvableWirings() {
		unresolvableWirings = new ArrayList();
	}

	// Returns true if the supplied export has not been recorded as
	// an unresolvable wiring for this import
	boolean isNotAnUnresolvableWiring(ResolverExport exp) {
		return !unresolvableWirings.contains(exp.getExporter());
	}

	ImportPackageSpecification getImportPackageSpecification() {
		return importPackageSpecification;
	}

	boolean isOptional() {
		return (importPackageSpecification.getResolution() & ImportPackageSpecification.RESOLUTION_OPTIONAL) != 0;
	}

	boolean isDynamic() {
		return (importPackageSpecification.getResolution() & ImportPackageSpecification.RESOLUTION_DYNAMIC) != 0;
	}

	public String toString() {
		return importPackageSpecification.toString();
	}
}

Back to the top