Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 815678fc489dc080bf132dc343872ba320193a7b (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
/*******************************************************************************
 * Copyright (c) 2004, 2014 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.sources;

import java.net.URL;
import java.util.*;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.eclipse.osgi.util.ManifestElement;

public class FilteredSourcePackage extends SingleSourcePackage {
	private static final char ALL = '*';
	private final String[] includes;
	private final String[] excludes;

	public FilteredSourcePackage(String name, BundleLoader supplier, String includes, String excludes) {
		super(name, supplier);
		this.includes = includes != null ? ManifestElement.getArrayFromList(includes) : null;
		this.excludes = excludes != null ? ManifestElement.getArrayFromList(excludes) : null;
	}

	public URL getResource(String name) {
		if (isFiltered(name, getId()))
			return null;
		return super.getResource(name);
	}

	public Enumeration<URL> getResources(String name) {
		if (isFiltered(name, getId()))
			return null;
		return super.getResources(name);
	}

	public Class<?> loadClass(String name) throws ClassNotFoundException {
		if (isFiltered(name, getId()))
			return null;
		return super.loadClass(name);
	}

	private boolean isFiltered(String name, String pkgName) {
		String lastName = getName(name, pkgName);
		return !isIncluded(lastName) || isExcluded(lastName);
	}

	private String getName(String name, String pkgName) {
		if (!BundleLoader.DEFAULT_PACKAGE.equals(pkgName) && pkgName.length() + 1 <= name.length())
			return name.substring(pkgName.length() + 1);
		return name;
	}

	private boolean isIncluded(String name) {
		if (includes == null)
			return true;
		return isInList(name, includes);
	}

	private boolean isExcluded(String name) {
		if (excludes == null)
			return false;
		return isInList(name, excludes);
	}

	private boolean isInList(String name, String[] list) {
		for (int i = 0; i < list.length; i++) {
			int len = list[i].length();
			if (len == 0)
				continue;
			if (list[i].charAt(0) == ALL && len == 1)
				return true; // handles "*" wild card
			if (list[i].charAt(len - 1) == ALL)
				if (name.startsWith(list[i].substring(0, len - 1)))
					return true;
			if (name.equals(list[i]))
				return true;

		}
		return false;
	}

	@Override
	public Collection<String> listResources(String path, String filePattern) {
		Collection<String> result = super.listResources(path, filePattern);
		for (Iterator<String> resources = result.iterator(); resources.hasNext();) {
			String resource = resources.next();
			int lastSlash = resource.lastIndexOf('/');
			String fileName = lastSlash >= 0 ? resource.substring(lastSlash + 1) : resource;
			if (!isIncluded(fileName) || isExcluded(fileName))
				resources.remove();
		}
		return result;
	}

}

Back to the top