KickJava   Java API By Example, From Geeks To Geeks.

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


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

9 package org.eclipse.pde.internal.build.site;
10
11 import java.io.*;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Properties JavaDoc;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.pde.internal.build.Utils;
19 import org.eclipse.update.configurator.ConfiguratorUtils;
20 import org.eclipse.update.configurator.IPlatformConfiguration;
21
22 public class PluginPathFinder {
23     private static final String JavaDoc URL_PROPERTY = "org.eclipse.update.resolution_url"; //$NON-NLS-1$
24
private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
25

26     /**
27      *
28      * @param platformHome
29      * @param linkFile
30      * @param features false for plugins, true for features
31      * @return path of plugins or features directory of an extension site
32      */

33     private static String JavaDoc getSitePath(String JavaDoc platformHome, File linkFile, boolean features) {
34         String JavaDoc prefix = new Path(platformHome).removeLastSegments(1).toString();
35         Properties JavaDoc properties = new Properties JavaDoc();
36         try {
37             FileInputStream fis = new FileInputStream(linkFile);
38             properties.load(fis);
39             fis.close();
40             String JavaDoc path = properties.getProperty("path"); //$NON-NLS-1$
41
if (path != null) {
42                 if (!new Path(path).isAbsolute())
43                     path = prefix + IPath.SEPARATOR + path;
44                 path += IPath.SEPARATOR + "eclipse" + IPath.SEPARATOR; //$NON-NLS-1$
45
if (features)
46                     path += "features"; //$NON-NLS-1$
47
else
48                     path += "plugins"; //$NON-NLS-1$
49
if (new File(path).exists()) {
50                     return path;
51                 }
52             }
53         } catch (IOException e) {
54         }
55         return null;
56     }
57
58     /**
59      *
60      * @param platformHome
61      * @param features false for plugin sites, true for feature sites
62      * @return array of ".../plugins" or ".../features" Files
63      */

64     private static File[] getSites(String JavaDoc platformHome, boolean features) {
65         ArrayList JavaDoc sites = new ArrayList JavaDoc();
66
67         File file = new File(platformHome, features ? "features" : "plugins"); //$NON-NLS-1$ //$NON-NLS-2$
68
if (!features && !file.exists())
69             file = new File(platformHome);
70         if (file.exists())
71             sites.add(file);
72
73         File[] linkFiles = new File(platformHome + IPath.SEPARATOR + "links").listFiles(); //$NON-NLS-1$
74
if (linkFiles != null) {
75             for (int i = 0; i < linkFiles.length; i++) {
76                 String JavaDoc path = getSitePath(platformHome, linkFiles[i], features);
77                 if (path != null) {
78                     sites.add(new File(path));
79                 }
80             }
81         }
82         return (File[]) sites.toArray(new File[sites.size()]);
83     }
84
85     public static File[] getFeaturePaths(String JavaDoc platformHome) {
86         return getPaths(platformHome, true);
87     }
88     
89     public static File[] getPluginPaths(String JavaDoc platformHome) {
90         return getPaths(platformHome, false);
91     }
92     
93     public static File[] getPaths(String JavaDoc platformHome, boolean features) {
94         File file = new File(platformHome, "configuration/org.eclipse.update/platform.xml"); //$NON-NLS-1$
95
if (file.exists()) {
96             try {
97                 String JavaDoc value = new Path(platformHome).toFile().toURL().toExternalForm();
98                 System.setProperty(URL_PROPERTY, value);
99                 try {
100                     IPlatformConfiguration config = ConfiguratorUtils.getPlatformConfiguration(file.toURL());
101                     return Utils.asFile(getConfiguredSitesPaths(platformHome, config, features));
102                 } finally {
103                     System.setProperty(URL_PROPERTY, EMPTY_STRING);
104                 }
105             } catch (MalformedURLException JavaDoc e) {
106             } catch (IOException e) {
107             }
108         }
109
110         return Utils.asFile(scanLocations(getSites(platformHome, features)));
111     }
112
113     private static URL JavaDoc[] getConfiguredSitesPaths(String JavaDoc platformHome, IPlatformConfiguration configuration, boolean features) {
114         URL JavaDoc[] installPlugins = scanLocations(new File[] {new File(platformHome, features ? "features" : "plugins")}); //$NON-NLS-1$ //$NON-NLS-2$
115
URL JavaDoc[] extensionPlugins = getExtensionPluginURLs(configuration, features);
116
117         URL JavaDoc[] all = new URL JavaDoc[installPlugins.length + extensionPlugins.length];
118         System.arraycopy(installPlugins, 0, all, 0, installPlugins.length);
119         System.arraycopy(extensionPlugins, 0, all, installPlugins.length, extensionPlugins.length);
120         return all;
121     }
122
123     /**
124      *
125      * @param config
126      * @param features true for features false for plugins
127      * @return URLs for features or plugins on the site
128      */

129     private static URL JavaDoc[] getExtensionPluginURLs(IPlatformConfiguration config, boolean features) {
130         ArrayList JavaDoc extensionPlugins = new ArrayList JavaDoc();
131         IPlatformConfiguration.ISiteEntry[] sites = config.getConfiguredSites();
132         for (int i = 0; i < sites.length; i++) {
133             URL JavaDoc url = sites[i].getURL();
134             if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
135
String JavaDoc[] entries;
136                 if (features)
137                     entries = sites[i].getFeatures();
138                 else
139                     entries = sites[i].getPlugins();
140                 for (int j = 0; j < entries.length; j++) {
141                     try {
142                         extensionPlugins.add(new File(url.getFile(), entries[j]).toURL());
143                     } catch (MalformedURLException JavaDoc e) {
144                     }
145                 }
146             }
147         }
148         return (URL JavaDoc[]) extensionPlugins.toArray(new URL JavaDoc[extensionPlugins.size()]);
149     }
150
151     /**
152      * Scan given plugin/feature directories or jars for existence
153      * @param sites
154      * @return URLs to plugins/features
155      */

156     private static URL JavaDoc[] scanLocations(File[] sites) {
157         ArrayList JavaDoc result = new ArrayList JavaDoc();
158         for (int i = 0; i < sites.length; i++) {
159             if (!sites[i].exists())
160                 continue;
161             File[] children = sites[i].listFiles();
162             if (children != null) {
163                 for (int j = 0; j < children.length; j++) {
164                     try {
165                         result.add(children[j].toURL());
166                     } catch (MalformedURLException JavaDoc e) {
167                     }
168                 }
169             }
170         }
171         return (URL JavaDoc[]) result.toArray(new URL JavaDoc[result.size()]);
172     }
173 }
174
Popular Tags