KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.osgi.framework.internal.core;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17 import org.eclipse.osgi.service.resolver.BundleDescription;
18
19 /**
20  * DependentPolicy is an implementation of a buddy policy.
21  * It is responsible for looking up a class in the dependents of the bundle
22  * to which this policy is attached to.
23  */

24 public class DependentPolicy implements IBuddyPolicy {
25     BundleLoader buddyRequester;
26     int lastDependentOfAdded = -1; //remember the index of the bundle for which we last added the dependent
27
List allDependents = null; //the list of all dependents known so far
28

29     public DependentPolicy(BundleLoader requester) {
30         buddyRequester = requester;
31
32         //Initialize with the first level of dependent the list
33
allDependents = new ArrayList();
34         basicAddImmediateDependents(buddyRequester.getBundle().getBundleDescription());
35         //If there is no dependent, reset to null
36
if (allDependents.size() == 0)
37             allDependents = null;
38     }
39
40     public Class JavaDoc loadClass(String JavaDoc name) {
41         if (allDependents == null)
42             return null;
43
44         Class JavaDoc result = null;
45         //size may change, so we must check it every time
46
for (int i = 0; i < allDependents.size() && result == null; i++) {
47             BundleDescription searchedBundle = (BundleDescription) allDependents.get(i);
48             try {
49                 BundleLoaderProxy proxy = buddyRequester.getLoaderProxy(searchedBundle);
50                 if (proxy == null)
51                     continue;
52                 result = proxy.getBundleLoader().findClass(name, true);
53             } catch (ClassNotFoundException JavaDoc e) {
54                 if (result == null)
55                     addDependent(i, searchedBundle);
56             }
57         }
58         return result;
59     }
60
61     private synchronized void addDependent(int i, BundleDescription searchedBundle) {
62         if (i > lastDependentOfAdded) {
63             lastDependentOfAdded = i;
64             basicAddImmediateDependents(searchedBundle);
65         }
66     }
67
68     public URL JavaDoc loadResource(String JavaDoc name) {
69         if (allDependents == null)
70             return null;
71
72         URL JavaDoc result = null;
73         //size may change, so we must check it every time
74
for (int i = 0; i < allDependents.size() && result == null; i++) {
75             BundleDescription searchedBundle = (BundleDescription) allDependents.get(i);
76             BundleLoaderProxy proxy = buddyRequester.getLoaderProxy(searchedBundle);
77             if (proxy == null)
78                 continue;
79             result = proxy.getBundleLoader().findResource(name, true);
80             if (result == null) {
81                 addDependent(i, searchedBundle);
82             }
83         }
84         return result;
85     }
86
87     public Enumeration loadResources(String JavaDoc name) {
88         if (allDependents == null)
89             return null;
90
91         Enumeration results = null;
92         //size may change, so we must check it every time
93
for (int i = 0; i < allDependents.size(); i++) {
94             BundleDescription searchedBundle = (BundleDescription) allDependents.get(i);
95             try {
96                 BundleLoaderProxy proxy = buddyRequester.getLoaderProxy(searchedBundle);
97                 if (proxy == null)
98                     continue;
99                 results = BundleLoader.compoundEnumerations(results, proxy.getBundleLoader().findResources(name));
100                 addDependent(i, searchedBundle);
101             } catch (IOException JavaDoc e) {
102                 //Ignore and keep looking
103
}
104         }
105         return results;
106     }
107
108     private void basicAddImmediateDependents(BundleDescription root) {
109         BundleDescription[] dependents = root.getDependents();
110         for (int i = 0; i < dependents.length; i++) {
111             BundleDescription toAdd = dependents[i];
112             if (toAdd.getHost() == null && !allDependents.contains(toAdd)) {
113                 allDependents.add(toAdd);
114             }
115         }
116     }
117 }
118
Popular Tags