001: /*******************************************************************************
002: * Copyright (c) 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.core.plugin;
011:
012: import java.io.File;
013: import java.net.URL;
014: import java.util.Properties;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.core.runtime.Preferences;
022: import org.eclipse.pde.internal.core.ICoreConstants;
023: import org.eclipse.pde.internal.core.PDECore;
024: import org.eclipse.pde.internal.core.TargetPlatformHelper;
025: import org.eclipse.pde.internal.core.UpdateManagerHelper;
026:
027: /**
028: * The central class for the plug-in development target platform. This class cannot
029: * be instantiated or subclassed by clients; all functionality is provided
030: * by static methods. Features include:
031: * <ul>
032: * <li>the target platform's OS/WS/ARCH</li>
033: * <li>the default application and product</li>
034: * <li>the available applications and products</li>
035: * </ul>
036: * <p>
037: * @since 3.3
038: * </p>
039: */
040: public class TargetPlatform {
041:
042: private static String PRODUCT_PROPERTY = "eclipse.product"; //$NON-NLS-1$
043: private static String APPLICATION_PROPERTY = "eclipse.application"; //$NON-NLS-1$
044:
045: private static String SDK_PRODUCT = "org.eclipse.sdk.ide"; //$NON-NLS-1$
046: private static String PLATFORM_PRODUCT = "org.eclipse.platform.ide"; //$NON-NLS-1$
047:
048: private static String IDE_APPLICATION = "org.eclipse.ui.ide.workbench"; //$NON-NLS-1$
049:
050: /**
051: * Returns the target platform's main location as specified on the <b>Environment</b>
052: * tab of the <b>Plug-in Development > Target Platform</b> preference page.
053: *
054: * @return the target platform's main location
055: */
056: public static String getLocation() {
057: Preferences preferences = PDECore.getDefault()
058: .getPluginPreferences();
059: return preferences.getString(ICoreConstants.PLATFORM_PATH);
060: }
061:
062: /**
063: * Returns the location of the default target platform, namely the location
064: * of the host (running) instance of Eclipse.
065: *
066: * @return the location of the default target platform
067: */
068: public static String getDefaultLocation() {
069: URL installURL = Platform.getInstallLocation().getURL();
070: IPath path = new Path(installURL.getFile())
071: .removeTrailingSeparator();
072: return path.toOSString();
073:
074: }
075:
076: /**
077: * Returns the target operating system as specified on the <b>Environment</b>
078: * tab of the <b>Plug-in Development > Target Platform</b> preference page.
079: *
080: * @return the target operating system
081: */
082: public static String getOS() {
083: return getProperty(ICoreConstants.OS, Platform.getOS());
084: }
085:
086: /**
087: * Returns the target windowing system as specified on the <b>Environment</b>
088: * tab of the <b>Plug-in Development > Target Platform</b> preference page.
089: *
090: * @return the target windowing system
091: */
092: public static String getWS() {
093: return getProperty(ICoreConstants.WS, Platform.getWS());
094: }
095:
096: /**
097: * Returns the target locale as specified on the <b>Environment</b>
098: * tab of the <b>Plug-in Development > Target Platform</b> preference page.
099: *
100: * @return the target locale
101: */
102: public static String getNL() {
103: return getProperty(ICoreConstants.NL, Platform.getNL());
104: }
105:
106: /**
107: * Returns the target system architecture as specified on the <b>Environment</b>
108: * tab of the <b>Plug-in Development > Target Platform</b> preference page.
109: *
110: * @return the target system architecture
111: */
112: public static String getOSArch() {
113: return getProperty(ICoreConstants.ARCH, Platform.getOSArch());
114: }
115:
116: private static String getProperty(String key, String defaultValue) {
117: Preferences preferences = PDECore.getDefault()
118: .getPluginPreferences();
119: String value = preferences.getString(key);
120: return value.equals("") ? defaultValue : value; //$NON-NLS-1$
121: }
122:
123: /**
124: * Returns a list of identifiers for all available applications
125: * (i.e. <code>org.eclipse.core.runtime.applications</code> extensions) declared in the workspace
126: * and target platform plug-ins.
127: * <p>
128: * If a workspace plug-in has the same ID as a plug-in in the target platform, the extensions
129: * declared in the target counterpart are ignored.
130: * </p>
131: *
132: * @return a list of identifiers for all available applications
133: */
134: public static String[] getApplications() {
135: return TargetPlatformHelper.getApplicationNames();
136: }
137:
138: /**
139: * Returns a list of identifiers for all available products
140: * (i.e. <code>org.eclipse.core.runtime.products</code> extensions) declared in the workspace
141: * and target platform plug-ins.
142: * <p>
143: * If a workspace plug-in has the same ID as a plug-in in the target platform, the extensions
144: * declared in the target counterpart are ignored.
145: * </p>
146: *
147: * @return a list of identifiers for all available products
148: */
149: public static String[] getProducts() {
150: return TargetPlatformHelper.getProductNames();
151: }
152:
153: /**
154: * Returns the ID for the default product
155: * (<code>org.eclipse.core.runtime.products</code> extension) for the current target platform,
156: * or <code>null</code> if none can be determined.
157: *
158: * If any of the
159: *
160: * @return the ID for the default product or <code>null</code> if none could be determined
161: */
162: public static String getDefaultProduct() {
163: Properties config = TargetPlatformHelper
164: .getConfigIniProperties();
165: Set set = TargetPlatformHelper.getProductNameSet();
166: if (config != null) {
167: String product = (String) config.get(PRODUCT_PROPERTY);
168: if (product != null && set.contains(product))
169: return product;
170: }
171:
172: if (set.contains(SDK_PRODUCT))
173: return SDK_PRODUCT;
174:
175: return set.contains(PLATFORM_PRODUCT) ? PLATFORM_PRODUCT : null;
176: }
177:
178: /**
179: * Returns the ID for the default application
180: * (<code>org.eclipse.core.runtime.applications</code> extension) for the current target
181: * platform.
182: * <p>
183: * If none could be determined, then <code>org.eclipse.ui.ide.workbench</code>
184: * application is returned.
185: * </p>
186: * @return the default application to run when launching an Eclipse application
187: */
188: public static String getDefaultApplication() {
189: Properties config = TargetPlatformHelper
190: .getConfigIniProperties();
191: Set set = TargetPlatformHelper.getApplicationNameSet();
192: if (config != null) {
193: String application = (String) config
194: .get(APPLICATION_PROPERTY);
195: if (application != null && set.contains(application))
196: return application;
197: }
198: return IDE_APPLICATION;
199: }
200:
201: /**
202: * Creates a platform configuration to be used when launching an Eclipse
203: * application that uses Update Manager as a configurator
204: *
205: * @param location the location where the configuration should be persisted
206: * @param plugins the list of plug-ins that make up the configuration
207: * @param brandingPlugin if specified, a entry for the feature containing the branding plug-in will
208: * be created in the platform configuration
209: *
210: * @throws CoreException an exception is thrown if there was a problem writing the platform
211: * configuration file
212: */
213: public static void createPlatformConfiguration(File location,
214: IPluginModelBase[] plugins, IPluginModelBase brandingPlugin)
215: throws CoreException {
216: UpdateManagerHelper.createPlatformConfiguration(location,
217: plugins, brandingPlugin);
218: }
219:
220: /**
221: * The comma-separated list of bundles which are automatically installed
222: * and optionally started.
223: * <p>
224: * Each entry if of the form <bundleID>[@ [<startlevel>] [":start"]]
225: * If the startlevel is omitted then the framework will use the default start level for the bundle.
226: * If the "start" tag is added then the bundle will be marked as started after being installed.
227: * </p>
228: * <p>
229: * The list computed is based on the <b>osgi.bundles</b> key found in the config.ini
230: * file of the target platform. If no such key is found, then a suitable list is computed
231: * based on the target platform version.
232: * </p>
233: *
234: * @return a comma-separated list of bundles that are automatically installed
235: * and optionally started when a runtime Eclipse application is launched.
236: */
237: public static String getBundleList() {
238: return TargetPlatformHelper.getBundleList();
239: }
240:
241: }
|