1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.Properties ; 21 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.pde.core.plugin.TargetPlatform; 26 import org.eclipse.update.configurator.ConfiguratorUtils; 27 import org.eclipse.update.configurator.IPlatformConfiguration; 28 29 public class PluginPathFinder { 30 31 private static final String URL_PROPERTY = "org.eclipse.update.resolution_url"; private static final String EMPTY_STRING = ""; 34 41 private static String getSitePath(String platformHome, File linkFile, boolean features) { 42 String prefix = new Path(platformHome).removeLastSegments(1).toString(); 43 Properties properties = new Properties (); 44 try { 45 FileInputStream fis = new FileInputStream (linkFile); 46 properties.load(fis); 47 fis.close(); 48 String path = properties.getProperty("path"); if (path != null) { 50 if (!new Path(path).isAbsolute()) 51 path = prefix + IPath.SEPARATOR + path; 52 path += IPath.SEPARATOR + "eclipse" + IPath.SEPARATOR; if (features) 54 path += "features"; else 56 path += "plugins"; if (new File (path).exists()) { 58 return path; 59 } 60 } 61 } catch (IOException e) { 62 } 63 return null; 64 } 65 66 72 private static File [] getSites(String platformHome, boolean features) { 73 HashSet sites = new HashSet (); 74 File file = new File (platformHome, features ? "features" : "plugins"); if (!features && !file.exists()) 76 file = new File (platformHome); 77 if (file.exists()) 78 sites.add(file); 79 80 File [] linkFiles = new File (platformHome + IPath.SEPARATOR + "links").listFiles(); if (linkFiles != null) { 82 for (int i = 0; i < linkFiles.length; i++) { 83 String path = getSitePath(platformHome, linkFiles[i], features); 84 if (path != null) { 85 sites.add(new File (path)); 86 } 87 } 88 } 89 return (File [])sites.toArray(new File [sites.size()]); 90 } 91 92 public static URL [] getPluginPaths(String platformHome) { 93 if (new Path(platformHome).equals(new Path(TargetPlatform.getDefaultLocation())) && !isDevLaunchMode()) 94 return ConfiguratorUtils.getCurrentPlatformConfiguration().getPluginPath(); 95 96 File file = new File (platformHome, "configuration/org.eclipse.update/platform.xml"); if (file.exists()) { 98 try { 99 String value = new Path(platformHome).toFile().toURL().toExternalForm(); 100 System.setProperty(URL_PROPERTY, value); 101 try { 102 IPlatformConfiguration config = ConfiguratorUtils.getPlatformConfiguration(file.toURL()); 103 return getConfiguredSitesPaths(platformHome, config, false); 104 } finally { 105 System.setProperty(URL_PROPERTY, EMPTY_STRING); 106 } 107 } catch (MalformedURLException e) { 108 } catch (IOException e) { 109 } 110 } 111 return scanLocations(getSites(platformHome, false)); 112 } 113 114 public static URL [] getFeaturePaths(String platformHome) { 115 File file = new File (platformHome, "configuration/org.eclipse.update/platform.xml"); if (file.exists()) { 117 try { 118 String value = new Path(platformHome).toFile().toURL().toExternalForm(); 119 System.setProperty(URL_PROPERTY, value); 120 try { 121 IPlatformConfiguration config = ConfiguratorUtils.getPlatformConfiguration(file.toURL()); 122 return getConfiguredSitesPaths(platformHome, config, true); 123 } finally { 124 System.setProperty(URL_PROPERTY, EMPTY_STRING); 125 } 126 } catch (MalformedURLException e) { 127 } catch (IOException e) { 128 } 129 } 130 return scanLocations(getSites(platformHome, true)); 131 } 132 133 private static URL [] getConfiguredSitesPaths(String platformHome, IPlatformConfiguration configuration, boolean features) { 134 URL [] installPlugins = scanLocations(new File [] { new File ( 135 platformHome, features ? "features" : "plugins") }); URL [] extensionPlugins = getExtensionPluginURLs(configuration, features); 137 138 URL [] all = new URL [installPlugins.length + extensionPlugins.length]; 139 System.arraycopy(installPlugins, 0, all, 0, installPlugins.length); 140 System.arraycopy(extensionPlugins, 0, all, installPlugins.length, extensionPlugins.length); 141 return all; 142 } 143 144 150 private static URL [] getExtensionPluginURLs(IPlatformConfiguration config, boolean features) { 151 ArrayList extensionPlugins = new ArrayList (); 152 IPlatformConfiguration.ISiteEntry[] sites = config.getConfiguredSites(); 153 for (int i = 0; i < sites.length; i++) { 154 URL url = sites[i].getURL(); 155 if ("file".equalsIgnoreCase(url.getProtocol())) { String [] entries; 157 if(features) 158 entries = sites[i].getFeatures(); 159 else 160 entries = sites[i].getPlugins(); 161 for (int j = 0; j < entries.length; j++) { 162 try { 163 extensionPlugins.add(new File (url.getFile(), entries[j]).toURL()); 164 } catch (MalformedURLException e) { 165 } 166 } 167 } 168 } 169 return (URL []) extensionPlugins.toArray(new URL [extensionPlugins.size()]); 170 } 171 172 177 public static URL [] scanLocations(File [] sites) { 178 HashSet result = new HashSet (); 179 for (int i = 0; i < sites.length; i++){ 180 if (!sites[i].exists()) 181 continue; 182 File [] children = sites[i].listFiles(); 183 if (children != null) { 184 for (int j = 0; j < children.length; j++) { 185 try { 186 result.add(children[j].toURL()); 187 } catch (MalformedURLException e) { 188 } 189 } 190 } 191 } 192 return (URL []) result.toArray(new URL [result.size()]); 193 } 194 195 public static boolean isDevLaunchMode() { 196 if (Boolean.getBoolean("eclipse.pde.launch")) return true; 198 String [] args = Platform.getApplicationArgs(); 199 for (int i = 0; i < args.length; i++) { 200 if (args[i].equals("-pdelaunch")) return true; 202 } 203 return false; 204 } 205 206 207 } 208 | Popular Tags |