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.internal.core;
011:
012: import java.io.File;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.ArrayList;
016: import java.util.Locale;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.core.runtime.Status;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.core.ifeature.IFeature;
025: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
026: import org.eclipse.update.configurator.ConfiguratorUtils;
027: import org.eclipse.update.configurator.IPlatformConfiguration;
028:
029: public class UpdateManagerHelper {
030:
031: private static class LocalSite {
032: private ArrayList fPlugins;
033: private IPath fPath;
034:
035: public LocalSite(IPath path) {
036: if (path.getDevice() != null)
037: fPath = path.setDevice(path.getDevice().toUpperCase(
038: Locale.ENGLISH));
039: else
040: fPath = path;
041: fPlugins = new ArrayList();
042: }
043:
044: public IPath getPath() {
045: return fPath;
046: }
047:
048: public URL getURL() throws MalformedURLException {
049: return new URL("file:" + fPath.removeTrailingSeparator()); //$NON-NLS-1$
050: }
051:
052: public void add(IPluginModelBase model) {
053: fPlugins.add(model);
054: }
055:
056: public String[] getRelativePluginList() {
057: String[] list = new String[fPlugins.size()];
058: for (int i = 0; i < fPlugins.size(); i++) {
059: IPluginModelBase model = (IPluginModelBase) fPlugins
060: .get(i);
061: IPath location = new Path(model.getInstallLocation());
062: // defect 37319
063: if (location.segmentCount() > 2)
064: location = location.removeFirstSegments(location
065: .segmentCount() - 2);
066: //31489 - entry must be relative
067: list[i] = location.setDevice(null).makeRelative()
068: .toString();
069: }
070: return list;
071: }
072: }
073:
074: public static void createPlatformConfiguration(File configLocation,
075: IPluginModelBase[] models, IPluginModelBase brandingPlugin)
076: throws CoreException {
077: try {
078: IPlatformConfiguration platformConfiguration = ConfiguratorUtils
079: .getPlatformConfiguration(null);
080:
081: // Compute local sites
082: ArrayList sites = new ArrayList();
083: for (int i = 0; i < models.length; i++) {
084: IPath path = new Path(models[i].getInstallLocation())
085: .removeLastSegments(2);
086: addToSite(path, models[i], sites);
087: }
088:
089: createConfigurationEntries(platformConfiguration, sites);
090:
091: if (brandingPlugin != null)
092: createFeatureEntries(platformConfiguration,
093: brandingPlugin);
094:
095: platformConfiguration.refresh();
096: platformConfiguration.save(new URL(
097: "file:" + configLocation.getPath())); //$NON-NLS-1$
098: } catch (Exception e) {
099: // Wrap everything else in a core exception.
100: String message = e.getMessage();
101: if (message == null || message.length() == 0)
102: message = PDECoreMessages.TargetPlatform_exceptionThrown;
103: throw new CoreException(new Status(IStatus.ERROR,
104: PDECore.PLUGIN_ID, IStatus.ERROR, message, e));
105: }
106: }
107:
108: private static void addToSite(IPath path, IPluginModelBase model,
109: ArrayList sites) {
110: if (path.getDevice() != null)
111: path = path.setDevice(path.getDevice().toUpperCase(
112: Locale.ENGLISH));
113: for (int i = 0; i < sites.size(); i++) {
114: LocalSite localSite = (LocalSite) sites.get(i);
115: if (localSite.getPath().equals(path)) {
116: localSite.add(model);
117: return;
118: }
119: }
120: // First time - add site
121: LocalSite localSite = new LocalSite(path);
122: localSite.add(model);
123: sites.add(localSite);
124: }
125:
126: private static void createConfigurationEntries(
127: IPlatformConfiguration config, ArrayList sites)
128: throws CoreException, MalformedURLException {
129:
130: for (int i = 0; i < sites.size(); i++) {
131: LocalSite localSite = (LocalSite) sites.get(i);
132: String[] plugins = localSite.getRelativePluginList();
133:
134: int policy = IPlatformConfiguration.ISitePolicy.USER_INCLUDE;
135: IPlatformConfiguration.ISitePolicy sitePolicy = config
136: .createSitePolicy(policy, plugins);
137: IPlatformConfiguration.ISiteEntry siteEntry = config
138: .createSiteEntry(localSite.getURL(), sitePolicy);
139: config.configureSite(siteEntry);
140: }
141: config.isTransient(true);
142: }
143:
144: private static void createFeatureEntries(
145: IPlatformConfiguration config, IPluginModelBase plugin)
146: throws MalformedURLException {
147: String id = plugin.getPluginBase().getId();
148: IFeatureModel featureModel = PDECore.getDefault()
149: .getFeatureModelManager().findFeatureModel(id);
150: if (featureModel != null) {
151: IFeature feature = featureModel.getFeature();
152: IPlatformConfiguration.IFeatureEntry featureEntry = config
153: .createFeatureEntry(
154: id,
155: feature.getVersion(),
156: id,
157: plugin.getPluginBase().getVersion(),
158: true,
159: null,
160: new URL[] { new URL(
161: "file:" + plugin.getInstallLocation()) }); //$NON-NLS-1$
162: config.configureFeatureEntry(featureEntry);
163: }
164: }
165:
166: }
|