KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.*;
14 import java.net.*;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.service.resolver.*;
18 import org.eclipse.pde.core.plugin.*;
19 import org.eclipse.pde.internal.core.plugin.*;
20
21 /**
22  *
23  */

24 public class TargetPlatformRegistryLoader {
25     
26     private static final String JavaDoc KEY_SCANNING_PROBLEMS =
27         "ExternalModelManager.scanningProblems"; //$NON-NLS-1$
28
private static String JavaDoc getMode(URL[] urls) {
29         String JavaDoc targetMode = "2.1"; //$NON-NLS-1$
30
for (int i = 0; i < urls.length; i++) {
31             if (urls[i].getFile().indexOf("org.eclipse.osgi") != -1) //$NON-NLS-1$
32
targetMode = null;
33         }
34         return targetMode;
35     }
36
37     public static void load(URL[] urls, PDEState state, IProgressMonitor monitor) {
38         String JavaDoc targetMode = getMode(urls);
39         state.setTargetMode(targetMode);
40         monitor.beginTask("", urls.length); //$NON-NLS-1$
41
for (int i = 0; i < urls.length; i++) {
42             state.addBundle(new File(urls[i].getFile()));
43             monitor.worked(1);
44         }
45     }
46     
47     public static IPluginModelBase[] loadModels(URL[] urls, boolean resolve, PDEState state, IProgressMonitor monitor) {
48         if (monitor == null)
49             monitor = new NullProgressMonitor();
50         
51         monitor.beginTask(PDECore.getResourceString("TargetPlatformRegistryLoader.parsing"), 10); //$NON-NLS-1$
52
load(urls, state, new SubProgressMonitor(monitor, 8));
53         
54         state.resolveState();
55         monitor.worked(1);
56         
57         if (resolve) {
58             logResolutionErrors(state.getState());
59         }
60         
61         BundleDescription[] bundleDescriptions = resolve ? state.getState().getResolvedBundles() : state.getState().getBundles();
62         IPluginModelBase[] models = new IPluginModelBase[bundleDescriptions.length];
63         for (int i = 0; i < bundleDescriptions.length; i++) {
64             monitor.subTask(bundleDescriptions[i].getSymbolicName());
65             models[i] = createModelFromDescription(bundleDescriptions[i], state);
66         }
67         monitor.done();
68         return models;
69     }
70     
71     private static void logResolutionErrors(State state) {
72         MultiStatus errors =
73             new MultiStatus(
74                 PDECore.getPluginId(),
75                 1,
76                 PDECore.getResourceString(KEY_SCANNING_PROBLEMS),
77                 null);
78         
79         StateHelper helper = acquireStateHelper();
80         BundleDescription[] all = state.getBundles();
81         for (int i = 0; i < all.length; i++) {
82             if (!all[i].isResolved()) {
83                 VersionConstraint[] unsatisfiedConstraints = helper.getUnsatisfiedConstraints(all[i]);
84                 if (unsatisfiedConstraints.length == 0) {
85                     BundleDescription activeBundle = findActiveBundle(state, all[i].getSymbolicName());
86                     if (activeBundle == null) {
87                         String JavaDoc message = PDECore.getFormattedMessage("ECLIPSE_IGNORE", all[i].getLocation()); //$NON-NLS-1$
88
errors.add(new Status(IStatus.ERROR, all[i].getSymbolicName(), IStatus.WARNING, message, null));
89                     } else {
90                         String JavaDoc message = PDECore.getFormattedMessage("ECLIPSE_OTHER_VERSION", new String JavaDoc[] {all[i].getLocation(), activeBundle.getLocation()}); //$NON-NLS-1$
91
errors.add(new Status(IStatus.INFO, all[i].getSymbolicName(), IStatus.INFO, message, null));
92                     }
93                 } else {
94                     for (int j = 0; j < unsatisfiedConstraints.length; j++) {
95                         String JavaDoc message = getResolutionFailureMessage(unsatisfiedConstraints[j]);
96                         errors.add(new Status(IStatus.WARNING, all[i].getSymbolicName(), IStatus.WARNING, message, null));
97                     }
98                 }
99             }
100         }
101         if (errors.getChildren().length > 0)
102             PDECore.log(errors);
103         
104     }
105     
106     private static BundleDescription findActiveBundle(State state, String JavaDoc symbolicName) {
107         BundleDescription[] bundles = state.getBundles(symbolicName);
108         for (int i = 0; i < bundles.length; i++) {
109             if (bundles[i].isResolved())
110                 return bundles[i];
111         }
112         return null;
113     }
114     
115     public static String JavaDoc getResolutionFailureMessage(VersionConstraint unsatisfied) {
116         if (unsatisfied.isResolved())
117             throw new IllegalArgumentException JavaDoc();
118         if (unsatisfied instanceof PackageSpecification)
119             return PDECore.getFormattedMessage("ECLIPSE_MISSING_IMPORTED_PACKAGE", toString(unsatisfied)); //$NON-NLS-1$
120
if (unsatisfied instanceof BundleSpecification) {
121             if (((BundleSpecification) unsatisfied).isOptional())
122                 return PDECore.getFormattedMessage("ECLIPSE_MISSING_OPTIONAL_REQUIRED_BUNDLE", toString(unsatisfied)); //$NON-NLS-1$
123
return PDECore.getFormattedMessage("ECLIPSE_MISSING_REQUIRED_BUNDLE", toString(unsatisfied)); //$NON-NLS-1$
124
}
125         return PDECore.getFormattedMessage("ECLIPSE_MISSING_HOST", toString(unsatisfied)); //$NON-NLS-1$
126
}
127     
128     private static String JavaDoc toString(VersionConstraint constraint) {
129         VersionRange versionRange = constraint.getVersionRange();
130         if (versionRange == null || versionRange.getMinimum() != null)
131             return constraint.getName();
132         return constraint.getName() + '_' + versionRange;
133     }
134
135     
136     public static IPluginModelBase[] loadModels(URL[] urls, boolean resolve, IProgressMonitor monitor) {
137         PDEState state = new PDEState();
138         return loadModels(urls, resolve, state, monitor);
139     }
140     
141     /**
142      * @param description
143      * @return
144      */

145     private static IPluginModelBase createModelFromDescription(BundleDescription description, PDEState state) {
146         ExternalPluginModelBase model = null;
147         if (description.getHost() == null)
148             model = new ExternalPluginModel();
149         else
150             model = new ExternalFragmentModel();
151         model.load(description, state);
152         return model;
153     }
154     
155     private static StateHelper acquireStateHelper(){
156         return PDECore.getDefault().acquirePlatform().getStateHelper();
157     }
158
159
160 }
161
Popular Tags