Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 838d9d52c93dbd23b7064fc8f08988eccd4e4a6b (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
/*******************************************************************************
 * Copyright (c) 2005, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.internal.loader.buddy;

import java.io.IOException;
import java.net.URL;
import java.util.*;
import org.eclipse.osgi.container.ModuleWire;
import org.eclipse.osgi.container.ModuleWiring;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.osgi.framework.namespace.BundleNamespace;
import org.osgi.framework.namespace.PackageNamespace;

/**
 * DependentPolicy is an implementation of a buddy policy. 
 * It is responsible for looking up a class in the dependents of the bundle
 * to which this policy is attached to.
 */
public class DependentPolicy implements IBuddyPolicy {
	BundleLoader buddyRequester;
	int lastDependentOfAdded = -1; //remember the index of the bundle for which we last added the dependent
	List<ModuleWiring> allDependents = null; //the list of all dependents known so far

	public DependentPolicy(BundleLoader requester) {
		buddyRequester = requester;

		//Initialize with the first level of dependent the list
		allDependents = new ArrayList<>();
		basicAddImmediateDependents(requester.getWiring());
		//If there is no dependent, reset to null
		if (allDependents.size() == 0)
			allDependents = null;
	}

	@Override
	public Class<?> loadClass(String name) {
		if (allDependents == null)
			return null;

		Class<?> result = null;
		//size may change, so we must check it every time
		for (int i = 0; i < allDependents.size() && result == null; i++) {
			ModuleWiring searchWiring = allDependents.get(i);
			BundleLoader searchLoader = (BundleLoader) searchWiring.getModuleLoader();
			if (searchLoader != null) {
				try {
					result = searchLoader.findClass(name);
				} catch (ClassNotFoundException e) {
					if (result == null)
						addDependent(i, searchWiring);
				}
			}
		}
		return result;
	}

	private synchronized void addDependent(int i, ModuleWiring searchedWiring) {
		if (i > lastDependentOfAdded) {
			lastDependentOfAdded = i;
			basicAddImmediateDependents(searchedWiring);
		}
	}

	@Override
	public URL loadResource(String name) {
		if (allDependents == null)
			return null;

		URL result = null;
		//size may change, so we must check it every time
		for (int i = 0; i < allDependents.size() && result == null; i++) {
			ModuleWiring searchWiring = allDependents.get(i);
			BundleLoader searchLoader = (BundleLoader) searchWiring.getModuleLoader();
			if (searchLoader != null) {
				result = searchLoader.findResource(name);
				if (result == null) {
					addDependent(i, searchWiring);
				}
			}
		}
		return result;
	}

	@Override
	public Enumeration<URL> loadResources(String name) {
		if (allDependents == null)
			return null;

		Enumeration<URL> results = null;
		//size may change, so we must check it every time
		for (int i = 0; i < allDependents.size(); i++) {
			ModuleWiring searchWiring = allDependents.get(i);
			BundleLoader searchLoader = (BundleLoader) searchWiring.getModuleLoader();
			if (searchLoader != null) {
				try {
					results = BundleLoader.compoundEnumerations(results, searchLoader.findResources(name));
					addDependent(i, searchWiring);
				} catch (IOException e) {
					//Ignore and keep looking
				}
			}
		}
		return results;
	}

	private void basicAddImmediateDependents(ModuleWiring wiring) {
		List<ModuleWire> providedWires = wiring.getProvidedModuleWires(null);
		if (providedWires != null) {
			for (ModuleWire wire : providedWires) {
				String namespace = wire.getRequirement().getNamespace();
				if (PackageNamespace.PACKAGE_NAMESPACE.equals(namespace) || BundleNamespace.BUNDLE_NAMESPACE.equals(namespace)) {
					ModuleWiring dependent = wire.getRequirerWiring();
					if (!allDependents.contains(dependent)) {
						allDependents.add(dependent);
					}
				}
			}
		}
	}
}

Back to the top