001: /*******************************************************************************
002: * Copyright (c) 2000, 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.io.File;
013: import java.io.FileInputStream;
014: import java.io.IOException;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017: import java.util.ArrayList;
018: import java.util.HashSet;
019: import java.util.Properties;
020:
021: import org.eclipse.core.runtime.IPath;
022: import org.eclipse.core.runtime.Path;
023: import org.eclipse.core.runtime.Platform;
024: import org.eclipse.pde.core.plugin.TargetPlatform;
025: import org.eclipse.update.configurator.ConfiguratorUtils;
026: import org.eclipse.update.configurator.IPlatformConfiguration;
027:
028: public class PluginPathFinder {
029:
030: private static final String URL_PROPERTY = "org.eclipse.update.resolution_url"; //$NON-NLS-1$
031: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
032:
033: /**
034: *
035: * @param platformHome
036: * @param linkFile
037: * @param features false for plugins, true for features
038: * @return path of plugins or features directory of an extension site
039: */
040: private static String getSitePath(String platformHome,
041: File linkFile, boolean features) {
042: String prefix = new Path(platformHome).removeLastSegments(1)
043: .toString();
044: Properties properties = new Properties();
045: try {
046: FileInputStream fis = new FileInputStream(linkFile);
047: properties.load(fis);
048: fis.close();
049: String path = properties.getProperty("path"); //$NON-NLS-1$
050: if (path != null) {
051: if (!new Path(path).isAbsolute())
052: path = prefix + IPath.SEPARATOR + path;
053: path += IPath.SEPARATOR + "eclipse" + IPath.SEPARATOR; //$NON-NLS-1$
054: if (features)
055: path += "features"; //$NON-NLS-1$
056: else
057: path += "plugins"; //$NON-NLS-1$
058: if (new File(path).exists()) {
059: return path;
060: }
061: }
062: } catch (IOException e) {
063: }
064: return null;
065: }
066:
067: /**
068: *
069: * @param platformHome
070: * @param features false for plugin sites, true for feature sites
071: * @return array of ".../plugins" or ".../features" Files
072: */
073: private static File[] getSites(String platformHome, boolean features) {
074: HashSet sites = new HashSet();
075: File file = new File(platformHome,
076: features ? "features" : "plugins"); //$NON-NLS-1$ //$NON-NLS-2$
077: if (!features && !file.exists())
078: file = new File(platformHome);
079: if (file.exists())
080: sites.add(file);
081:
082: File[] linkFiles = new File(platformHome + IPath.SEPARATOR
083: + "links").listFiles(); //$NON-NLS-1$
084: if (linkFiles != null) {
085: for (int i = 0; i < linkFiles.length; i++) {
086: String path = getSitePath(platformHome, linkFiles[i],
087: features);
088: if (path != null) {
089: sites.add(new File(path));
090: }
091: }
092: }
093: return (File[]) sites.toArray(new File[sites.size()]);
094: }
095:
096: public static URL[] getPluginPaths(String platformHome) {
097: if (new Path(platformHome).equals(new Path(TargetPlatform
098: .getDefaultLocation()))
099: && !isDevLaunchMode())
100: return ConfiguratorUtils.getCurrentPlatformConfiguration()
101: .getPluginPath();
102:
103: File file = new File(platformHome,
104: "configuration/org.eclipse.update/platform.xml"); //$NON-NLS-1$
105: if (file.exists()) {
106: try {
107: String value = new Path(platformHome).toFile().toURL()
108: .toExternalForm();
109: System.setProperty(URL_PROPERTY, value);
110: try {
111: IPlatformConfiguration config = ConfiguratorUtils
112: .getPlatformConfiguration(file.toURL());
113: return getConfiguredSitesPaths(platformHome,
114: config, false);
115: } finally {
116: System.setProperty(URL_PROPERTY, EMPTY_STRING);
117: }
118: } catch (MalformedURLException e) {
119: } catch (IOException e) {
120: }
121: }
122: return scanLocations(getSites(platformHome, false));
123: }
124:
125: public static URL[] getFeaturePaths(String platformHome) {
126: File file = new File(platformHome,
127: "configuration/org.eclipse.update/platform.xml"); //$NON-NLS-1$
128: if (file.exists()) {
129: try {
130: String value = new Path(platformHome).toFile().toURL()
131: .toExternalForm();
132: System.setProperty(URL_PROPERTY, value);
133: try {
134: IPlatformConfiguration config = ConfiguratorUtils
135: .getPlatformConfiguration(file.toURL());
136: return getConfiguredSitesPaths(platformHome,
137: config, true);
138: } finally {
139: System.setProperty(URL_PROPERTY, EMPTY_STRING);
140: }
141: } catch (MalformedURLException e) {
142: } catch (IOException e) {
143: }
144: }
145: return scanLocations(getSites(platformHome, true));
146: }
147:
148: private static URL[] getConfiguredSitesPaths(String platformHome,
149: IPlatformConfiguration configuration, boolean features) {
150: URL[] installPlugins = scanLocations(new File[] { new File(
151: platformHome, features ? "features" : "plugins") }); //$NON-NLS-1$ //$NON-NLS-2$
152: URL[] extensionPlugins = getExtensionPluginURLs(configuration,
153: features);
154:
155: URL[] all = new URL[installPlugins.length
156: + extensionPlugins.length];
157: System.arraycopy(installPlugins, 0, all, 0,
158: installPlugins.length);
159: System.arraycopy(extensionPlugins, 0, all,
160: installPlugins.length, extensionPlugins.length);
161: return all;
162: }
163:
164: /**
165: *
166: * @param config
167: * @param features true for features false for plugins
168: * @return URLs for features or plugins on the site
169: */
170: private static URL[] getExtensionPluginURLs(
171: IPlatformConfiguration config, boolean features) {
172: ArrayList extensionPlugins = new ArrayList();
173: IPlatformConfiguration.ISiteEntry[] sites = config
174: .getConfiguredSites();
175: for (int i = 0; i < sites.length; i++) {
176: URL url = sites[i].getURL();
177: if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
178: String[] entries;
179: if (features)
180: entries = sites[i].getFeatures();
181: else
182: entries = sites[i].getPlugins();
183: for (int j = 0; j < entries.length; j++) {
184: try {
185: extensionPlugins.add(new File(url.getFile(),
186: entries[j]).toURL());
187: } catch (MalformedURLException e) {
188: }
189: }
190: }
191: }
192: return (URL[]) extensionPlugins
193: .toArray(new URL[extensionPlugins.size()]);
194: }
195:
196: /**
197: * Scan given plugin/feature directores or jars for existance
198: * @param sites
199: * @return URLs to plugins/features
200: */
201: public static URL[] scanLocations(File[] sites) {
202: HashSet result = new HashSet();
203: for (int i = 0; i < sites.length; i++) {
204: if (!sites[i].exists())
205: continue;
206: File[] children = sites[i].listFiles();
207: if (children != null) {
208: for (int j = 0; j < children.length; j++) {
209: try {
210: result.add(children[j].toURL());
211: } catch (MalformedURLException e) {
212: }
213: }
214: }
215: }
216: return (URL[]) result.toArray(new URL[result.size()]);
217: }
218:
219: public static boolean isDevLaunchMode() {
220: if (Boolean.getBoolean("eclipse.pde.launch")) //$NON-NLS-1$
221: return true;
222: String[] args = Platform.getApplicationArgs();
223: for (int i = 0; i < args.length; i++) {
224: if (args[i].equals("-pdelaunch")) //$NON-NLS-1$
225: return true;
226: }
227: return false;
228: }
229:
230: }
|