KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > views > dependencies > CalleesContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.ui.views.dependencies;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.osgi.service.resolver.BaseDescription;
16 import org.eclipse.osgi.service.resolver.BundleDescription;
17 import org.eclipse.osgi.service.resolver.BundleSpecification;
18 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
19 import org.eclipse.osgi.service.resolver.HostSpecification;
20 import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
21 import org.eclipse.pde.core.plugin.IPluginModelBase;
22 import org.osgi.framework.Constants;
23
24 public class CalleesContentProvider extends DependenciesViewPageContentProvider {
25     private BundleDescription fFragmentDescription;
26     
27     public CalleesContentProvider(DependenciesView view) {
28         super(view);
29     }
30     
31     protected Object JavaDoc[] findCallees(IPluginModelBase model) {
32         BundleDescription desc = model.getBundleDescription();
33         if (desc == null)
34             return new Object JavaDoc[0];
35         fFragmentDescription = null;
36         HostSpecification spec = desc.getHost();
37         if (spec != null) {
38             fFragmentDescription = desc;
39             Object JavaDoc[] fragmentDependencies = getDependencies(desc);
40             BaseDescription host = spec.getSupplier();
41             if (host instanceof BundleDescription) {
42                 BundleDescription hostDesc = (BundleDescription)host;
43                 // check to see if the host is already included as a dependency. If so, we don't need to include the host manually.
44
for (int i = 0; i < fragmentDependencies.length; i++) {
45                     BundleDescription dependency = null;
46                     if (fragmentDependencies[i] instanceof BundleSpecification)
47                         dependency = ((BundleSpecification)fragmentDependencies[i]).getBundle();
48                     else if (fragmentDependencies[i] instanceof ImportPackageSpecification) {
49                         ExportPackageDescription epd = (ExportPackageDescription)((ImportPackageSpecification)fragmentDependencies[i]).getSupplier();
50                         if (epd != null)
51                             dependency = epd.getSupplier();
52                     }
53                     if (dependency != null && dependency.equals(hostDesc))
54                         return fragmentDependencies;
55                 }
56                 
57                 // host not included as dependency, include it manually.
58
Object JavaDoc[] result = new Object JavaDoc[fragmentDependencies.length + 1];
59                 result[0] = hostDesc;
60                 System.arraycopy(fragmentDependencies, 0, result, 1, fragmentDependencies.length);
61                 return result;
62             }
63             return fragmentDependencies;
64         }
65         return getDependencies(desc);
66     }
67     
68     protected Object JavaDoc[] findCallees(BundleDescription desc) {
69         if (desc == null)
70             return new Object JavaDoc[0];
71         return getDependencies(desc);
72     }
73     
74     private Object JavaDoc[] getDependencies(BundleDescription desc) {
75         // use map to store dependencies so if Import-Package is supplied by same BundleDescription as supplier of Require-Bundle, it only shows up once
76
// Also, have to use BundleSpecficiation instead of BundleDescroption to show re-exported icon on re-exported Required-Bundles
77
// Have to use ImportPackageSpecification to determine if an import is optional and should be filtered.
78
HashMap JavaDoc dependencies = new HashMap JavaDoc();
79         BundleSpecification[] requiredBundles = desc.getRequiredBundles();
80         for (int i = 0; i < requiredBundles.length; i++) {
81             BaseDescription bd = requiredBundles[i].getSupplier();
82             if (bd != null)
83                 dependencies.put(bd, requiredBundles[i]);
84             else
85                 dependencies.put(requiredBundles[i], requiredBundles[i]);
86         }
87         ImportPackageSpecification[] importedPkgs = desc.getImportPackages();
88         for (int i = 0; i < importedPkgs.length; i++) {
89             BaseDescription bd = importedPkgs[i].getSupplier();
90             if (bd != null && bd instanceof ExportPackageDescription) {
91                 BundleDescription exporter = ((ExportPackageDescription)bd).getExporter();
92                 if (exporter != null) {
93                     Object JavaDoc obj = dependencies.get(exporter);
94                     if (obj == null) {
95                         dependencies.put(exporter, importedPkgs[i]);
96                     } else if (!Constants.RESOLUTION_OPTIONAL.equals(importedPkgs[i].getDirective(Constants.RESOLUTION_DIRECTIVE))
97                             && obj instanceof ImportPackageSpecification &&
98                             Constants.RESOLUTION_OPTIONAL.equals(((ImportPackageSpecification)obj).getDirective(Constants.RESOLUTION_DIRECTIVE))) {
99                     // if we have a non-optional Import-Package dependency on a bundle which we already depend on, check to make sure our
100
// current dependency is not optional. If it is, replace the optional dependency with the non-optional one
101
dependencies.put(exporter, importedPkgs[i]);
102                     }
103                 }
104             }
105             // ignore unresolved packages
106
}
107         // include fragments which are "linked" to this bundle
108
BundleDescription frags[] = desc.getFragments();
109         for (int i = 0; i < frags.length; i++) {
110             if (!frags[i].equals(fFragmentDescription))
111                 dependencies.put(frags[i], frags[i]);
112         }
113         return dependencies.values().toArray();
114     }
115
116 }
117
Popular Tags