Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9623081d220258bf1e6ab459d68da1d152dcdc90 (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
/*******************************************************************************
 * Copyright (c) 2005, 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.osgi.internal.loader.buddy;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import org.eclipse.osgi.framework.internal.core.AbstractBundle;
import org.eclipse.osgi.framework.internal.core.Constants;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.eclipse.osgi.internal.loader.BundleLoaderProxy;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.BundleException;

/**
 *Registered policy is an implementation of a buddy policy. 
 * It is responsible for looking up a class in the bundles (registrant) that declare interest in the bundle that require the buddy loading.
 * Note that the registrants must have a direct dependency on the bundle needing buddy.
 */
public class RegisteredPolicy extends DependentPolicy {

	public RegisteredPolicy(BundleLoader requester) {
		super(requester);

		//Filter the dependents;
		if (allDependents == null)
			return;

		for (Iterator iter = allDependents.iterator(); iter.hasNext();) {
			BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) iter.next());
			if (proxy == null)
				iter.remove();

			try {
				String[] allContributions = ManifestElement.getArrayFromList((String) ((AbstractBundle) proxy.getBundle()).getBundleData().getManifest().get(Constants.REGISTERED_POLICY));
				if (allContributions == null) {
					iter.remove();
					continue;
				}
				boolean contributes = false;
				for (int j = 0; j < allContributions.length && contributes == false; j++) {
					if (allContributions[j].equals(buddyRequester.getBundle().getSymbolicName()))
						contributes = true;
				}
				if (!contributes)
					iter.remove();

			} catch (BundleException e) {
				iter.remove();
			}
		}

		//After the filtering, if nothing is left then null out the variable for optimization
		if (allDependents.size() == 0)
			allDependents = null;
	}

	public Class loadClass(String name) {
		if (allDependents == null)
			return null;

		Class result = null;
		int size = allDependents.size();
		for (int i = 0; i < size && result == null; i++) {
			try {
				BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) allDependents.get(i));
				if (proxy == null)
					continue;
				result = proxy.getBundleLoader().findClass(name);
			} catch (ClassNotFoundException e) {
				//Nothing to do, just keep looking
				continue;
			}
		}
		return result;
	}

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

		URL result = null;
		int size = allDependents.size();
		for (int i = 0; i < size && result == null; i++) {
			BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) allDependents.get(i));
			if (proxy == null)
				continue;
			result = proxy.getBundleLoader().findResource(name);
		}
		return result;
	}

	public Enumeration loadResources(String name) {
		if (allDependents == null)
			return null;

		Enumeration results = null;
		int size = allDependents.size();
		for (int i = 0; i < size; i++) {
			try {
				BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) allDependents.get(i));
				if (proxy == null)
					continue;
				results = BundleLoader.compoundEnumerations(results, proxy.getBundleLoader().findResources(name));
			} catch (IOException e) {
				//Ignore and keep looking
			}
		}
		return results;
	}
}

Back to the top