001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.util.Set;
013: import java.util.StringTokenizer;
014: import java.util.TreeSet;
015:
016: import org.eclipse.core.runtime.Preferences;
017: import org.eclipse.osgi.service.resolver.BundleDescription;
018: import org.eclipse.osgi.service.resolver.BundleSpecification;
019: import org.eclipse.osgi.service.resolver.ExportPackageDescription;
020: import org.eclipse.osgi.service.resolver.HostSpecification;
021: import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
022: import org.eclipse.osgi.service.resolver.State;
023: import org.eclipse.pde.core.plugin.IPluginExtension;
024: import org.eclipse.pde.core.plugin.IPluginModelBase;
025: import org.osgi.framework.Constants;
026:
027: public class DependencyManager {
028:
029: /**
030: * @return a set of plug-in IDs
031: *
032: */
033: public static Set getSelfAndDependencies(IPluginModelBase model) {
034: return getDependencies(new Object[] { model },
035: getImplicitDependencies(), TargetPlatformHelper
036: .getState(), false, true);
037: }
038:
039: /**
040: * @return a set of plug-in IDs
041: *
042: */
043: public static Set getSelfandDependencies(IPluginModelBase[] models) {
044: return getDependencies(models, getImplicitDependencies(),
045: TargetPlatformHelper.getState(), false, true);
046: }
047:
048: /**
049: * @return a set of plug-in IDs
050: *
051: */
052: public static Set getDependencies(Object[] selected,
053: String[] implicit, State state) {
054: return getDependencies(selected, implicit, state, true, true);
055: }
056:
057: /**
058: * @return a set of plug-in IDs
059: *
060: */
061: public static Set getDependencies(Object[] selected,
062: boolean includeOptional) {
063: return getDependencies(selected, getImplicitDependencies(),
064: TargetPlatformHelper.getState(), true, includeOptional);
065: }
066:
067: /**
068: * @return a set of plug-in IDs
069: *
070: */
071: private static Set getDependencies(Object[] selected,
072: String[] implicit, State state, boolean removeSelf,
073: boolean includeOptional) {
074: Set set = new TreeSet();
075: for (int i = 0; i < selected.length; i++) {
076: if (!(selected[i] instanceof IPluginModelBase))
077: continue;
078: IPluginModelBase model = (IPluginModelBase) selected[i];
079: addBundleAndDependencies(model.getBundleDescription(), set,
080: includeOptional);
081: IPluginExtension[] extensions = model.getPluginBase()
082: .getExtensions();
083: for (int j = 0; j < extensions.length; j++) {
084: String point = extensions[j].getPoint();
085: if (point != null) {
086: int dot = point.lastIndexOf('.');
087: if (dot != -1) {
088: String id = point.substring(0, dot);
089: addBundleAndDependencies(state.getBundle(id,
090: null), set, includeOptional);
091: }
092: }
093: }
094: }
095:
096: for (int i = 0; i < implicit.length; i++) {
097: addBundleAndDependencies(
098: state.getBundle(implicit[i], null), set,
099: includeOptional);
100: }
101:
102: if (removeSelf) {
103: for (int i = 0; i < selected.length; i++) {
104: if (!(selected[i] instanceof IPluginModelBase))
105: continue;
106: IPluginModelBase model = (IPluginModelBase) selected[i];
107: set.remove(model.getPluginBase().getId());
108: }
109: }
110: return set;
111: }
112:
113: private static String[] getImplicitDependencies() {
114: Preferences preferences = PDECore.getDefault()
115: .getPluginPreferences();
116: String dependencies = preferences
117: .getString(ICoreConstants.IMPLICIT_DEPENDENCIES);
118: if (dependencies.length() == 0)
119: return new String[0];
120: StringTokenizer tokenizer = new StringTokenizer(dependencies,
121: ","); //$NON-NLS-1$
122: String[] implicitIds = new String[tokenizer.countTokens()];
123: for (int i = 0; i < implicitIds.length; i++)
124: implicitIds[i] = tokenizer.nextToken();
125: return implicitIds;
126: }
127:
128: private static void addBundleAndDependencies(
129: BundleDescription desc, Set set, boolean includeOptional) {
130: if (desc != null && set.add(desc.getSymbolicName())) {
131: BundleSpecification[] required = desc.getRequiredBundles();
132: for (int i = 0; i < required.length; i++) {
133: if (includeOptional || !required[i].isOptional())
134: addBundleAndDependencies(
135: (BundleDescription) required[i]
136: .getSupplier(), set,
137: includeOptional);
138: }
139: ImportPackageSpecification[] importedPkgs = desc
140: .getImportPackages();
141: for (int i = 0; i < importedPkgs.length; i++) {
142: ExportPackageDescription exporter = (ExportPackageDescription) importedPkgs[i]
143: .getSupplier();
144: // Continue if the Imported Package is unresolved of the package is optional and don't want optional packages
145: if (exporter == null
146: || (!includeOptional && Constants.RESOLUTION_OPTIONAL
147: .equals(importedPkgs[i]
148: .getDirective(Constants.RESOLUTION_DIRECTIVE))))
149: continue;
150: addBundleAndDependencies(exporter.getExporter(), set,
151: includeOptional);
152: }
153: BundleDescription[] fragments = desc.getFragments();
154: for (int i = 0; i < fragments.length; i++) {
155: if (!fragments[i].isResolved())
156: continue;
157: String id = fragments[i].getSymbolicName();
158: if (!"org.eclipse.ui.workbench.compatibility".equals(id)) //$NON-NLS-1$
159: addBundleAndDependencies(fragments[i], set,
160: includeOptional);
161: }
162: HostSpecification host = desc.getHost();
163: if (host != null)
164: addBundleAndDependencies((BundleDescription) host
165: .getSupplier(), set, includeOptional);
166: }
167: }
168:
169: }
|