KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > PluginPathFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Properties JavaDoc;
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 JavaDoc URL_PROPERTY = "org.eclipse.update.resolution_url"; //$NON-NLS-1$
32
private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
33

34     /**
35      *
36      * @param platformHome
37      * @param linkFile
38      * @param features false for plugins, true for features
39      * @return path of plugins or features directory of an extension site
40      */

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

72     private static File JavaDoc[] getSites(String JavaDoc platformHome, boolean features) {
73         HashSet JavaDoc sites = new HashSet JavaDoc();
74         File JavaDoc file = new File JavaDoc(platformHome, features ? "features" : "plugins"); //$NON-NLS-1$ //$NON-NLS-2$
75
if (!features && !file.exists())
76             file = new File JavaDoc(platformHome);
77         if (file.exists())
78             sites.add(file);
79         
80         File JavaDoc[] linkFiles = new File JavaDoc(platformHome + IPath.SEPARATOR + "links").listFiles(); //$NON-NLS-1$
81
if (linkFiles != null) {
82             for (int i = 0; i < linkFiles.length; i++) {
83                 String JavaDoc path = getSitePath(platformHome, linkFiles[i], features);
84                 if (path != null) {
85                     sites.add(new File JavaDoc(path));
86                 }
87             }
88         }
89         return (File JavaDoc[])sites.toArray(new File JavaDoc[sites.size()]);
90     }
91     
92     public static URL JavaDoc[] getPluginPaths(String JavaDoc platformHome) {
93         if (new Path(platformHome).equals(new Path(TargetPlatform.getDefaultLocation())) && !isDevLaunchMode())
94             return ConfiguratorUtils.getCurrentPlatformConfiguration().getPluginPath();
95         
96         File JavaDoc file = new File JavaDoc(platformHome, "configuration/org.eclipse.update/platform.xml"); //$NON-NLS-1$
97
if (file.exists()) {
98             try {
99                 String JavaDoc 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 JavaDoc e) {
108             } catch (IOException JavaDoc e) {
109             }
110         }
111         return scanLocations(getSites(platformHome, false));
112     }
113     
114     public static URL JavaDoc[] getFeaturePaths(String JavaDoc platformHome) {
115         File JavaDoc file = new File JavaDoc(platformHome, "configuration/org.eclipse.update/platform.xml"); //$NON-NLS-1$
116
if (file.exists()) {
117             try {
118                 String JavaDoc 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 JavaDoc e) {
127             } catch (IOException JavaDoc e) {
128             }
129         }
130         return scanLocations(getSites(platformHome, true));
131     }
132     
133     private static URL JavaDoc[] getConfiguredSitesPaths(String JavaDoc platformHome, IPlatformConfiguration configuration, boolean features) {
134         URL JavaDoc[] installPlugins = scanLocations(new File JavaDoc[] { new File JavaDoc(
135                 platformHome, features ? "features" : "plugins") }); //$NON-NLS-1$ //$NON-NLS-2$
136
URL JavaDoc[] extensionPlugins = getExtensionPluginURLs(configuration, features);
137         
138         URL JavaDoc[] all = new URL JavaDoc[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     /**
145      *
146      * @param config
147      * @param features true for features false for plugins
148      * @return URLs for features or plugins on the site
149      */

150     private static URL JavaDoc[] getExtensionPluginURLs(IPlatformConfiguration config, boolean features) {
151         ArrayList JavaDoc extensionPlugins = new ArrayList JavaDoc();
152         IPlatformConfiguration.ISiteEntry[] sites = config.getConfiguredSites();
153         for (int i = 0; i < sites.length; i++) {
154             URL JavaDoc url = sites[i].getURL();
155             if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
156
String JavaDoc[] 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 JavaDoc(url.getFile(), entries[j]).toURL());
164                     } catch (MalformedURLException JavaDoc e) {
165                     }
166                 }
167             }
168         }
169         return (URL JavaDoc[]) extensionPlugins.toArray(new URL JavaDoc[extensionPlugins.size()]);
170     }
171     
172     /**
173      * Scan given plugin/feature directores or jars for existance
174      * @param sites
175      * @return URLs to plugins/features
176      */

177     public static URL JavaDoc[] scanLocations(File JavaDoc[] sites) {
178         HashSet JavaDoc result = new HashSet JavaDoc();
179         for (int i = 0; i < sites.length; i++){
180             if (!sites[i].exists())
181                 continue;
182             File JavaDoc[] 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 JavaDoc e) {
188                     }
189                 }
190             }
191         }
192         return (URL JavaDoc[]) result.toArray(new URL JavaDoc[result.size()]);
193     }
194     
195     public static boolean isDevLaunchMode() {
196         if (Boolean.getBoolean("eclipse.pde.launch")) //$NON-NLS-1$
197
return true;
198         String JavaDoc[] args = Platform.getApplicationArgs();
199         for (int i = 0; i < args.length; i++) {
200             if (args[i].equals("-pdelaunch")) //$NON-NLS-1$
201
return true;
202         }
203         return false;
204     }
205
206     
207 }
208
Popular Tags