Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 287f9eb7a7ba6ffc94a6420a28f164408d9da830 (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
/*******************************************************************************
 * Copyright (c) 2004, 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.loader;

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

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

	public FilteredSourcePackage(String name, BundleLoaderProxy supplier, String includes, String excludes, String[] friends) {
		super(name, supplier);
		if (includes != null)
			this.includes = ManifestElement.getArrayFromList(includes);
		if (excludes != null)
			this.excludes = ManifestElement.getArrayFromList(excludes);
		this.friends = friends;
	}

	public boolean isFriend(String symbolicName) {
		if (friends == null)
			return true;
		for (int i = 0; i < friends.length; i++)
			if (friends[i].equals(symbolicName))
				return true;
		return false;
	}

	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