KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > dependencies > DependencyCalculator


1 /*******************************************************************************
2  * Copyright (c) 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.search.dependencies;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.osgi.service.resolver.BaseDescription;
17 import org.eclipse.osgi.service.resolver.BundleDescription;
18 import org.eclipse.osgi.service.resolver.BundleSpecification;
19 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
20 import org.eclipse.osgi.service.resolver.HostSpecification;
21 import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.PluginRegistry;
24 import org.osgi.framework.Constants;
25
26 public class DependencyCalculator {
27     
28     boolean fIncludeOptional;
29     protected HashMap JavaDoc fDependencies;
30     
31     /*
32      * Object[] can be IPluginModelBases, BundleDescriptions, or Strings (id's of bundles)
33      */

34     public DependencyCalculator(boolean includeOptional) {
35         super();
36         fIncludeOptional = includeOptional;
37     }
38     
39     
40     public void findDependencies(Object JavaDoc[] includedBundles) {
41         if (fDependencies == null)
42             fDependencies = new HashMap JavaDoc();
43         for (int i = 0; i < includedBundles.length; i++) {
44             findObjectDependencies(includedBundles[i]);
45         }
46     }
47     
48     public void findDependency(Object JavaDoc bundle) {
49         if (fDependencies == null)
50             fDependencies = new HashMap JavaDoc();
51         findObjectDependencies(bundle);
52     }
53     
54     private void findObjectDependencies(Object JavaDoc obj) {
55         if (obj instanceof IPluginModelBase) {
56             IPluginModelBase base = ((IPluginModelBase)obj);
57             BundleDescription desc = base.getBundleDescription();
58             if (desc != null)
59                 obj = desc;
60         }
61         if (obj instanceof BundleDescription)
62             findDependencies((BundleDescription)obj);
63     }
64     
65     /*
66      * Returns a Set of Bundle Ids
67      */

68     public Set JavaDoc getBundleIDs() {
69         Set JavaDoc temp = fDependencies.keySet();
70         fDependencies = null;
71         return temp;
72     }
73     
74     protected void findDependencies(BundleDescription desc) {
75         if (desc == null)
76             return;
77         String JavaDoc id = desc.getSymbolicName();
78         if (fDependencies.containsKey(id))
79             return;
80         IPluginModelBase model = PluginRegistry.findModel(desc);
81         if (model == null)
82             return;
83         fDependencies.put(id, model);
84
85         addRequiredBundles(desc.getRequiredBundles());
86         addImportedPackages(desc.getImportPackages());
87         
88         HostSpecification host = desc.getHost();
89         if (host != null) {
90             // if current BundleDescription is a fragment, include host bundle
91
BaseDescription bd = host.getSupplier();
92             if (bd != null && bd instanceof BundleDescription)
93                 findDependencies((BundleDescription)bd);
94         } else {
95             // otherwise, include applicable fragments for bundle
96
addFragments(desc);
97         }
98     }
99     
100     protected void addRequiredBundles(BundleSpecification[] requiredBundles) {
101         for (int i = 0; i < requiredBundles.length; i++) {
102             if (requiredBundles[i].isOptional() && !fIncludeOptional)
103                 continue;
104             BaseDescription bd = requiredBundles[i].getSupplier();
105             // only recursively search statisfied require-bundles
106
if (bd != null && bd instanceof BundleDescription)
107                 findDependencies((BundleDescription)bd);
108         }
109     }
110     
111     protected void addImportedPackages(ImportPackageSpecification[] packages) {
112         for (int i = 0; i < packages.length; i++) {
113             if (!fIncludeOptional)
114                 if (Constants.RESOLUTION_OPTIONAL.equals(packages[i].getDirective(Constants.RESOLUTION_DIRECTIVE))) {
115                     continue;
116                 }
117             BaseDescription bd = packages[i].getSupplier();
118             // only recursively search statisfied import-packages
119
if (bd != null && bd instanceof ExportPackageDescription) {
120                 BundleDescription exporter = ((ExportPackageDescription)bd).getExporter();
121                 if (exporter != null)
122                     findDependencies(exporter);
123             }
124         }
125     }
126     
127     protected void addFragments(BundleDescription desc) {
128         BundleDescription[] fragments = desc.getFragments();
129         for (int i = 0; i < fragments.length; i++)
130             if (fragments[i].isResolved() && !fragments[i].getSymbolicName().equals("org.eclipse.ui.workbench.compatibility")) { //$NON-NLS-1$
131
findDependencies(fragments[i]);
132             }
133     }
134     
135     public boolean containsPluginId(String JavaDoc id) {
136         return fDependencies.containsKey(id);
137     }
138
139 }
140
Popular Tags