001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others. All rights reserved.
003: * This program and the accompanying materials are made available under the
004: * terms of the Eclipse Public License v1.0 which accompanies this distribution,
005: * and is available at http://www.eclipse.org/legal/epl-v10.html
006: *
007: * Contributors: IBM - Initial API and implementation
008: ******************************************************************************/package org.eclipse.pde.internal.build.site;
009:
010: import java.io.*;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013: import java.util.ArrayList;
014: import java.util.Properties;
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.Path;
017: import org.eclipse.pde.internal.build.Utils;
018: import org.eclipse.update.configurator.ConfiguratorUtils;
019: import org.eclipse.update.configurator.IPlatformConfiguration;
020:
021: public class PluginPathFinder {
022: private static final String URL_PROPERTY = "org.eclipse.update.resolution_url"; //$NON-NLS-1$
023: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
024:
025: /**
026: *
027: * @param platformHome
028: * @param linkFile
029: * @param features false for plugins, true for features
030: * @return path of plugins or features directory of an extension site
031: */
032: private static String getSitePath(String platformHome,
033: File linkFile, boolean features) {
034: String prefix = new Path(platformHome).removeLastSegments(1)
035: .toString();
036: Properties properties = new Properties();
037: try {
038: FileInputStream fis = new FileInputStream(linkFile);
039: properties.load(fis);
040: fis.close();
041: String path = properties.getProperty("path"); //$NON-NLS-1$
042: if (path != null) {
043: if (!new Path(path).isAbsolute())
044: path = prefix + IPath.SEPARATOR + path;
045: path += IPath.SEPARATOR + "eclipse" + IPath.SEPARATOR; //$NON-NLS-1$
046: if (features)
047: path += "features"; //$NON-NLS-1$
048: else
049: path += "plugins"; //$NON-NLS-1$
050: if (new File(path).exists()) {
051: return path;
052: }
053: }
054: } catch (IOException e) {
055: }
056: return null;
057: }
058:
059: /**
060: *
061: * @param platformHome
062: * @param features false for plugin sites, true for feature sites
063: * @return array of ".../plugins" or ".../features" Files
064: */
065: private static File[] getSites(String platformHome, boolean features) {
066: ArrayList sites = new ArrayList();
067:
068: File file = new File(platformHome,
069: features ? "features" : "plugins"); //$NON-NLS-1$ //$NON-NLS-2$
070: if (!features && !file.exists())
071: file = new File(platformHome);
072: if (file.exists())
073: sites.add(file);
074:
075: File[] linkFiles = new File(platformHome + IPath.SEPARATOR
076: + "links").listFiles(); //$NON-NLS-1$
077: if (linkFiles != null) {
078: for (int i = 0; i < linkFiles.length; i++) {
079: String path = getSitePath(platformHome, linkFiles[i],
080: features);
081: if (path != null) {
082: sites.add(new File(path));
083: }
084: }
085: }
086: return (File[]) sites.toArray(new File[sites.size()]);
087: }
088:
089: public static File[] getFeaturePaths(String platformHome) {
090: return getPaths(platformHome, true);
091: }
092:
093: public static File[] getPluginPaths(String platformHome) {
094: return getPaths(platformHome, false);
095: }
096:
097: public static File[] getPaths(String platformHome, boolean features) {
098: File file = new File(platformHome,
099: "configuration/org.eclipse.update/platform.xml"); //$NON-NLS-1$
100: if (file.exists()) {
101: try {
102: String value = new Path(platformHome).toFile().toURL()
103: .toExternalForm();
104: System.setProperty(URL_PROPERTY, value);
105: try {
106: IPlatformConfiguration config = ConfiguratorUtils
107: .getPlatformConfiguration(file.toURL());
108: return Utils.asFile(getConfiguredSitesPaths(
109: platformHome, config, features));
110: } finally {
111: System.setProperty(URL_PROPERTY, EMPTY_STRING);
112: }
113: } catch (MalformedURLException e) {
114: } catch (IOException e) {
115: }
116: }
117:
118: return Utils.asFile(scanLocations(getSites(platformHome,
119: features)));
120: }
121:
122: private static URL[] getConfiguredSitesPaths(String platformHome,
123: IPlatformConfiguration configuration, boolean features) {
124: URL[] installPlugins = scanLocations(new File[] { new File(
125: platformHome, features ? "features" : "plugins") }); //$NON-NLS-1$ //$NON-NLS-2$
126: URL[] extensionPlugins = getExtensionPluginURLs(configuration,
127: features);
128:
129: URL[] all = new URL[installPlugins.length
130: + extensionPlugins.length];
131: System.arraycopy(installPlugins, 0, all, 0,
132: installPlugins.length);
133: System.arraycopy(extensionPlugins, 0, all,
134: installPlugins.length, extensionPlugins.length);
135: return all;
136: }
137:
138: /**
139: *
140: * @param config
141: * @param features true for features false for plugins
142: * @return URLs for features or plugins on the site
143: */
144: private static URL[] getExtensionPluginURLs(
145: IPlatformConfiguration config, boolean features) {
146: ArrayList extensionPlugins = new ArrayList();
147: IPlatformConfiguration.ISiteEntry[] sites = config
148: .getConfiguredSites();
149: for (int i = 0; i < sites.length; i++) {
150: URL url = sites[i].getURL();
151: if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
152: String[] entries;
153: if (features)
154: entries = sites[i].getFeatures();
155: else
156: entries = sites[i].getPlugins();
157: for (int j = 0; j < entries.length; j++) {
158: try {
159: extensionPlugins.add(new File(url.getFile(),
160: entries[j]).toURL());
161: } catch (MalformedURLException e) {
162: }
163: }
164: }
165: }
166: return (URL[]) extensionPlugins
167: .toArray(new URL[extensionPlugins.size()]);
168: }
169:
170: /**
171: * Scan given plugin/feature directories or jars for existence
172: * @param sites
173: * @return URLs to plugins/features
174: */
175: private static URL[] scanLocations(File[] sites) {
176: ArrayList result = new ArrayList();
177: for (int i = 0; i < sites.length; i++) {
178: if (!sites[i].exists())
179: continue;
180: File[] children = sites[i].listFiles();
181: if (children != null) {
182: for (int j = 0; j < children.length; j++) {
183: try {
184: result.add(children[j].toURL());
185: } catch (MalformedURLException e) {
186: }
187: }
188: }
189: }
190: return (URL[]) result.toArray(new URL[result.size()]);
191: }
192: }
|