Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0706ff41e46535e94e10ba51a3c93a3a6e9ffb9 (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
/*******************************************************************************
 * Copyright (c) 2008, 2010 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.composite;

import java.io.IOException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.*;
import org.eclipse.osgi.baseadaptor.BaseData;
import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile;
import org.eclipse.osgi.baseadaptor.loader.*;
import org.eclipse.osgi.framework.adaptor.BundleData;
import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
import org.osgi.framework.Bundle;

public class CompositeClassLoader extends ClassLoader implements BaseClassLoader {

	private final ClassLoaderDelegate delegate;
	private final ClasspathManager manager;
	private final ClassLoaderDelegate companionDelegate;
	//Support to cut class / resource loading cycles in the context of one thread. The contained object is a set of classname
	private final ThreadLocal beingLoaded = new ThreadLocal();

	public CompositeClassLoader(ClassLoader parent, ClassLoaderDelegate delegate, ClassLoaderDelegate companionDelegate, BaseData data) {
		super(parent);
		this.delegate = delegate;
		this.manager = new ClasspathManager(data, new String[0], this);
		this.companionDelegate = companionDelegate;
	}

	public ClasspathEntry createClassPathEntry(BundleFile bundlefile, ProtectionDomain cpDomain) {
		// nothing
		return null;
	}

	public Class defineClass(String name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry) {
		// nothing
		return null;
	}

	public ClasspathManager getClasspathManager() {
		return manager;
	}

	public ProtectionDomain getDomain() {
		// no domain
		return null;
	}

	public Object publicDefinePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) {
		return definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase);
	}

	public Class publicFindLoaded(String classname) {
		return findLoadedClass(classname);
	}

	public Object publicGetPackage(String pkgname) {
		return getPackage(pkgname);
	}

	public void attachFragment(BundleData bundledata, ProtectionDomain domain, String[] classpath) {
		// nothing
	}

	public void close() {
		// nothing
	}

	public Class findLocalClass(String classname) throws ClassNotFoundException {
		if (!startLoading(classname))
			throw new ClassNotFoundException(classname);
		try {
			return companionDelegate.findClass(classname);
		} finally {
			stopLoading(classname);
		}
	}

	public URL findLocalResource(String resource) {
		if (!startLoading(resource))
			return null;
		try {
			return companionDelegate.findResource(resource);
		} finally {
			stopLoading(resource);
		}
	}

	public Enumeration findLocalResources(String resource) {
		if (!startLoading(resource))
			return null;
		try {
			return companionDelegate.findResources(resource);
		} catch (IOException e) {
			return null;
		} finally {
			stopLoading(resource);
		}
	}

	public ClassLoaderDelegate getDelegate() {
		return delegate;
	}

	public URL getResource(String name) {
		return delegate.findResource(name);
	}

	public void initialize() {
		manager.initialize();
	}

	public Class loadClass(String name) throws ClassNotFoundException {
		return delegate.findClass(name);
	}

	private boolean startLoading(String name) {
		Set classesAndResources = (Set) beingLoaded.get();
		if (classesAndResources != null && classesAndResources.contains(name))
			return false;

		if (classesAndResources == null) {
			classesAndResources = new HashSet(3);
			beingLoaded.set(classesAndResources);
		}
		classesAndResources.add(name);
		return true;
	}

	private void stopLoading(String name) {
		((Set) beingLoaded.get()).remove(name);
	}

	public Bundle getBundle() {
		return manager.getBaseData().getBundle();
	}

	@SuppressWarnings("unchecked")
	public List<URL> findEntries(String path, String filePattern, int options) {
		return Collections.EMPTY_LIST;
	}

	@SuppressWarnings("unchecked")
	public Collection<String> listResources(String path, String filePattern, int options) {
		return Collections.EMPTY_LIST;
	}

	@SuppressWarnings("unchecked")
	public Collection<String> listLocalResources(String path, String filePattern, int options) {
		return Collections.EMPTY_LIST;
	}
}

Back to the top