KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > FilteredSourcePackage


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.internal.core;
12
13 import java.net.URL JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import org.eclipse.osgi.util.ManifestElement;
16
17 public class FilteredSourcePackage extends SingleSourcePackage {
18     private static final char ALL = '*';
19     String JavaDoc[] includes;
20     String JavaDoc[] excludes;
21     String JavaDoc[] friends;
22
23     public FilteredSourcePackage(String JavaDoc name, int expid, BundleLoaderProxy supplier, String JavaDoc includes, String JavaDoc excludes, String JavaDoc[] friends) {
24         super(name, expid, supplier);
25         if (includes != null)
26             this.includes = ManifestElement.getArrayFromList(includes);
27         if (excludes != null)
28             this.excludes = ManifestElement.getArrayFromList(excludes);
29         this.friends = friends;
30     }
31
32     public boolean isFriend(String JavaDoc symbolicName) {
33         if (friends == null)
34             return true;
35         for (int i = 0; i < friends.length; i++)
36             if (friends[i].equals(symbolicName))
37                 return true;
38         return false;
39     }
40
41     public URL JavaDoc getResource(String JavaDoc name) {
42         if (isFiltered(name, getId()))
43             return null;
44         return super.getResource(name);
45     }
46     public Enumeration JavaDoc getResources(String JavaDoc name) {
47         if (isFiltered(name, getId()))
48             return null;
49         return super.getResources(name);
50     }
51     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
52         if (isFiltered(name, getId()))
53             return null;
54         return super.loadClass(name);
55     }
56
57     private boolean isFiltered(String JavaDoc name, String JavaDoc pkgName) {
58         String JavaDoc lastName = getName(name, pkgName);
59         return !isIncluded(lastName) || isExcluded(lastName);
60     }
61
62     private String JavaDoc getName(String JavaDoc name, String JavaDoc pkgName) {
63         if (!BundleLoader.DEFAULT_PACKAGE.equals(pkgName) && pkgName.length() + 1 <= name.length())
64             return name.substring(pkgName.length() + 1);
65         return name;
66     }
67
68     private boolean isIncluded(String JavaDoc name) {
69         if (includes == null)
70             return true;
71         return isInList(name, includes);
72     }
73
74     private boolean isExcluded(String JavaDoc name) {
75         if (excludes == null)
76             return false;
77         return isInList(name, excludes);
78     }
79
80     private boolean isInList(String JavaDoc name, String JavaDoc[] list) {
81         for (int i = 0; i < list.length; i++) {
82             int len = list[i].length();
83             if (len == 0)
84                 continue;
85             if (list[i].charAt(0) == ALL && len == 1)
86                 return true; // handles "*" wild card
87
if (list[i].charAt(len-1) == ALL)
88                 if (name.startsWith(list[i].substring(0, len-1)))
89                     return true;
90             if (name.equals(list[i]))
91                 return true;
92             
93         }
94         return false;
95     }
96 }
97
Popular Tags