KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.*;
16
17 import org.eclipse.osgi.service.resolver.BundleDescription;
18 import org.eclipse.osgi.util.ManifestElement;
19 import org.osgi.framework.BundleException;
20
21 /**
22  *Registered policy is an implementation of a buddy policy.
23  * It is responsible for looking up a class in the bundles (registrant) that declare interest in the bundle that require the buddy loading.
24  * Note that the registrants must have a direct dependency on the bundle needing buddy.
25  */

26 public class RegisteredPolicy extends DependentPolicy {
27
28     public RegisteredPolicy(BundleLoader requester) {
29         super(requester);
30
31         //Filter the dependents;
32
if (allDependents == null)
33             return;
34
35         for (Iterator iter = allDependents.iterator(); iter.hasNext();) {
36             BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) iter.next());
37             if (proxy == null)
38                 iter.remove();
39
40             try {
41                 String JavaDoc[] allContributions = ManifestElement.getArrayFromList((String JavaDoc) ((AbstractBundle) proxy.getBundle()).getBundleData().getManifest().get(Constants.REGISTERED_POLICY));
42                 if (allContributions == null) {
43                     iter.remove();
44                     continue;
45                 }
46                 boolean contributes = false;
47                 for (int j = 0; j < allContributions.length && contributes == false; j++) {
48                     if (allContributions[j].equals(buddyRequester.bundle.getSymbolicName()))
49                         contributes = true;
50                 }
51                 if (!contributes)
52                     iter.remove();
53
54             } catch (BundleException e) {
55                 iter.remove();
56             }
57         }
58
59         //After the filtering, if nothing is left then null out the variable for optimization
60
if (allDependents.size() == 0)
61             allDependents = null;
62     }
63
64     public Class JavaDoc loadClass(String JavaDoc name) {
65         if (allDependents == null)
66             return null;
67
68         Class JavaDoc result = null;
69         int size = allDependents.size();
70         for (int i = 0; i < size && result == null; i++) {
71             try {
72                 BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) allDependents.get(i));
73                 if (proxy == null)
74                     continue;
75                 result = proxy.getBundleLoader().findClass(name, true);
76             } catch (ClassNotFoundException JavaDoc e) {
77                 //Nothing to do, just keep looking
78
continue;
79             }
80         }
81         return result;
82     }
83
84     public URL JavaDoc loadResource(String JavaDoc name) {
85         if (allDependents == null)
86             return null;
87
88         URL JavaDoc result = null;
89         int size = allDependents.size();
90         for (int i = 0; i < size && result == null; i++) {
91             BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) allDependents.get(i));
92             if (proxy == null)
93                 continue;
94             result = proxy.getBundleLoader().findResource(name, true);
95         }
96         return result;
97     }
98
99     public Enumeration loadResources(String JavaDoc name) {
100         if (allDependents == null)
101             return null;
102
103         Enumeration results = null;
104         int size = allDependents.size();
105         for (int i = 0; i < size; i++) {
106             try {
107                 BundleLoaderProxy proxy = buddyRequester.getLoaderProxy((BundleDescription) allDependents.get(i));
108                 if (proxy == null)
109                     continue;
110                 results = BundleLoader.compoundEnumerations(results, proxy.getBundleLoader().findResources(name));
111             } catch (IOException JavaDoc e) {
112                 //Ignore and keep looking
113
}
114         }
115         return results;
116     }
117 }
118
Popular Tags