KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > DependencyManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core;
12
13 import java.util.Set JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import java.util.TreeSet JavaDoc;
16
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.osgi.service.resolver.BundleDescription;
19 import org.eclipse.osgi.service.resolver.BundleSpecification;
20 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
21 import org.eclipse.osgi.service.resolver.HostSpecification;
22 import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
23 import org.eclipse.osgi.service.resolver.State;
24 import org.eclipse.pde.core.plugin.IPluginExtension;
25 import org.eclipse.pde.core.plugin.IPluginModelBase;
26 import org.osgi.framework.Constants;
27
28 public class DependencyManager {
29     
30     /**
31      * @return a set of plug-in IDs
32      *
33      */

34     public static Set JavaDoc getSelfAndDependencies(IPluginModelBase model) {
35         return getDependencies(new Object JavaDoc[] {model}, getImplicitDependencies(), TargetPlatformHelper.getState(), false, true);
36     }
37     
38     /**
39      * @return a set of plug-in IDs
40      *
41      */

42     public static Set JavaDoc getSelfandDependencies(IPluginModelBase[] models) {
43         return getDependencies(models, getImplicitDependencies(), TargetPlatformHelper.getState(), false, true);
44     }
45     
46     /**
47      * @return a set of plug-in IDs
48      *
49      */

50     public static Set JavaDoc getDependencies(Object JavaDoc[] selected, String JavaDoc[] implicit, State state) {
51         return getDependencies(selected, implicit, state, true, true);
52     }
53     
54     /**
55      * @return a set of plug-in IDs
56      *
57      */

58     public static Set JavaDoc getDependencies(Object JavaDoc[] selected, boolean includeOptional) {
59         return getDependencies(selected, getImplicitDependencies(), TargetPlatformHelper.getState(), true, includeOptional);
60     }
61     
62     /**
63      * @return a set of plug-in IDs
64      *
65      */

66     private static Set JavaDoc getDependencies(Object JavaDoc[] selected, String JavaDoc[] implicit, State state, boolean removeSelf, boolean includeOptional) {
67         Set JavaDoc set = new TreeSet JavaDoc();
68         for (int i = 0; i < selected.length; i++) {
69             if (!(selected[i] instanceof IPluginModelBase))
70                 continue;
71             IPluginModelBase model = (IPluginModelBase)selected[i];
72             addBundleAndDependencies(model.getBundleDescription(), set, includeOptional);
73             IPluginExtension[] extensions = model.getPluginBase().getExtensions();
74             for (int j = 0; j < extensions.length; j++) {
75                 String JavaDoc point = extensions[j].getPoint();
76                 if (point != null) {
77                     int dot = point.lastIndexOf('.');
78                     if (dot != -1) {
79                         String JavaDoc id = point.substring(0, dot);
80                         addBundleAndDependencies(state.getBundle(id, null), set, includeOptional);
81                     }
82                 }
83             }
84         }
85         
86         for (int i = 0; i < implicit.length; i++) {
87             addBundleAndDependencies(state.getBundle(implicit[i], null), set, includeOptional);
88         }
89         
90         if (removeSelf) {
91             for (int i = 0; i < selected.length; i++) {
92                 if (!(selected[i] instanceof IPluginModelBase))
93                     continue;
94                 IPluginModelBase model = (IPluginModelBase)selected[i];
95                 set.remove(model.getPluginBase().getId());
96             }
97         }
98         return set;
99     }
100     
101     private static String JavaDoc[] getImplicitDependencies() {
102         Preferences preferences = PDECore.getDefault().getPluginPreferences();
103         String JavaDoc dependencies = preferences.getString(ICoreConstants.IMPLICIT_DEPENDENCIES);
104         if (dependencies.length() == 0)
105             return new String JavaDoc[0];
106         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(dependencies, ","); //$NON-NLS-1$
107
String JavaDoc[] implicitIds = new String JavaDoc[tokenizer.countTokens()];
108         for (int i = 0; i < implicitIds.length; i++)
109             implicitIds[i] = tokenizer.nextToken();
110         return implicitIds;
111     }
112     
113     private static void addBundleAndDependencies(BundleDescription desc, Set JavaDoc set, boolean includeOptional) {
114         if (desc != null && set.add(desc.getSymbolicName())) {
115             BundleSpecification[] required = desc.getRequiredBundles();
116             for (int i = 0; i < required.length; i++) {
117                 if (includeOptional || !required[i].isOptional())
118                     addBundleAndDependencies((BundleDescription)required[i].getSupplier(), set, includeOptional);
119             }
120             ImportPackageSpecification[] importedPkgs = desc.getImportPackages();
121             for (int i = 0; i < importedPkgs.length; i++) {
122                 ExportPackageDescription exporter = (ExportPackageDescription)importedPkgs[i].getSupplier();
123                 // Continue if the Imported Package is unresolved of the package is optional and don't want optional packages
124
if (exporter == null || (!includeOptional &&
125                         Constants.RESOLUTION_OPTIONAL.equals(importedPkgs[i].getDirective(Constants.RESOLUTION_DIRECTIVE))))
126                     continue;
127                 addBundleAndDependencies(exporter.getExporter(), set, includeOptional);
128             }
129             BundleDescription[] fragments = desc.getFragments();
130             for (int i = 0; i < fragments.length; i++) {
131                 if (!fragments[i].isResolved())
132                     continue;
133                 String JavaDoc id = fragments[i].getSymbolicName();
134                 if (!"org.eclipse.ui.workbench.compatibility".equals(id)) //$NON-NLS-1$
135
addBundleAndDependencies(fragments[i], set, includeOptional);
136             }
137             HostSpecification host = desc.getHost();
138             if (host != null)
139                 addBundleAndDependencies((BundleDescription)host.getSupplier(), set, includeOptional);
140         }
141     }
142     
143     
144 }
145
Popular Tags