KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > site > PluginRegistryConverter


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.site;
12
13 import java.io.File JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.model.*;
18 import org.eclipse.osgi.service.resolver.*;
19 import org.eclipse.pde.internal.build.*;
20 import org.osgi.framework.Constants;
21 import org.osgi.framework.Version;
22
23 /**
24  * @deprecated
25  */

26 public class PluginRegistryConverter extends PDEState {
27     private PluginRegistryModel registry;
28
29     private URL JavaDoc[] removeInvalidURLs(URL JavaDoc[] files) {
30         URL JavaDoc[] validURLs = new URL JavaDoc[files.length];
31         int validURL = 0;
32         for (int i = 0; i < files.length; i++) {
33             if (!files[i].toExternalForm().endsWith("feature.xml") && !files[i].toExternalForm().endsWith("MANIFEST.MF")) //$NON-NLS-1$//$NON-NLS-2$
34
validURLs[validURL++] = files[i];
35         }
36         if (files.length == validURL)
37             return validURLs;
38         URL JavaDoc[] result = new URL JavaDoc[validURL];
39         System.arraycopy(validURLs, 0, result, 0, validURL);
40         return result;
41     }
42
43     private PluginRegistryModel getPluginRegistry(URL JavaDoc[] files) throws CoreException {
44         if (registry == null) {
45             files = removeInvalidURLs(files);
46             // create the registry according to the site where the code to compile is, and a existing installation of eclipse
47
MultiStatus problems = new MultiStatus(IPDEBuildConstants.PI_PDEBUILD, EXCEPTION_MODEL_PARSE, Messages.exception_pluginParse, null);
48             Factory factory = new Factory(problems);
49             registry = PluginRegistryModel.parsePlugins(files, factory);
50             registry.resolve(true, false);
51             IStatus status = factory.getStatus();
52             if (!status.isOK())
53                 throw new CoreException(status);
54         }
55         return registry;
56     }
57
58     public void addRegistryToState() {
59         PluginModel[] plugins = registry.getPlugins();
60         PluginFragmentModel[] fragments = registry.getFragments();
61
62         for (int i = 0; i < plugins.length; i++) {
63             BundleDescription bd = state.getFactory().createBundleDescription(getNextId(), plugins[i].getPluginId(), Version.parseVersion(plugins[i].getVersion()), plugins[i].getLocation(), createBundleSpecification(plugins[i].getRequires()), (HostSpecification) null, null, null, null, true);
64             String JavaDoc libs = createClasspath(plugins[i].getRuntime());
65             Properties manifest = new Properties();
66             if (libs != null)
67                 manifest.put(Constants.BUNDLE_CLASSPATH, libs);
68             loadPropertyFileIn(manifest, new File JavaDoc(fragments[i].getLocation()));
69             bd.setUserObject(manifest);
70             addBundleDescription(bd);
71         }
72
73         for (int i = 0; i < fragments.length; i++) {
74             HostSpecification host = state.getFactory().createHostSpecification(fragments[i].getPluginId(), new VersionRange(fragments[i].getPluginVersion()));
75             BundleDescription bd = state.getFactory().createBundleDescription(getNextId(), fragments[i].getId(), Version.parseVersion(fragments[i].getVersion()), fragments[i].getLocation(), createBundleSpecification(fragments[i].getRequires()), host, null, null, null, true);
76             String JavaDoc libs = createClasspath(fragments[i].getRuntime());
77             Properties manifest = new Properties();
78             if (libs != null)
79                 manifest.put(Constants.BUNDLE_CLASSPATH, libs);
80             loadPropertyFileIn(manifest, new File JavaDoc(fragments[i].getLocation()));
81             bd.setUserObject(manifest);
82             addBundleDescription(bd);
83         }
84     }
85
86     protected BundleSpecification[] createBundleSpecification(PluginPrerequisiteModel[] prereqs) {
87         if (prereqs == null)
88             return new BundleSpecification[0];
89         BundleSpecification[] specs = new BundleSpecification[prereqs.length];
90         for (int i = 0; i < prereqs.length; i++) {
91             specs[i] = state.getFactory().createBundleSpecification(prereqs[i].getPlugin(), new VersionRange(prereqs[i].getVersion()), prereqs[i].getExport(), prereqs[i].getOptional());
92         }
93         return specs;
94     }
95
96     private String JavaDoc createClasspath(LibraryModel[] libs) {
97         if (libs == null || libs.length == 0)
98             return null;
99
100         String JavaDoc result = ""; //$NON-NLS-1$
101
for (int i = 0; i < libs.length; i++) {
102             result += libs[i].getName() + (i == libs.length - 1 ? "" : ","); //$NON-NLS-1$//$NON-NLS-2$
103
}
104         return result;
105     }
106
107     public void addBundles(Collection bundles) {
108         try {
109             getPluginRegistry(Utils.asURL(bundles));
110         } catch (CoreException e) {
111             IStatus status = new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, EXCEPTION_STATE_PROBLEM, Messages.exception_registryResolution, e);
112             BundleHelper.getDefault().getLog().log(status);
113         }
114         for (Iterator iter = bundles.iterator(); iter.hasNext();) {
115             File JavaDoc bundle = (File JavaDoc) iter.next();
116             addBundle(bundle);
117         }
118     }
119 }
120
Popular Tags