KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Locale JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.ifeature.IFeature;
26 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
27 import org.eclipse.update.configurator.ConfiguratorUtils;
28 import org.eclipse.update.configurator.IPlatformConfiguration;
29
30 public class UpdateManagerHelper {
31
32     private static class LocalSite {
33         private ArrayList JavaDoc fPlugins;
34         private IPath fPath;
35     
36         public LocalSite(IPath path) {
37             if (path.getDevice() != null)
38                 fPath = path.setDevice(path.getDevice().toUpperCase(Locale.ENGLISH));
39             else
40                 fPath = path;
41             fPlugins = new ArrayList JavaDoc();
42         }
43     
44         public IPath getPath() {
45             return fPath;
46         }
47     
48         public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
49             return new URL JavaDoc("file:" + fPath.removeTrailingSeparator()); //$NON-NLS-1$
50
}
51     
52         public void add(IPluginModelBase model) {
53             fPlugins.add(model);
54         }
55     
56         public String JavaDoc[] getRelativePluginList() {
57             String JavaDoc[] list = new String JavaDoc[fPlugins.size()];
58             for (int i = 0; i < fPlugins.size(); i++) {
59                 IPluginModelBase model = (IPluginModelBase) fPlugins.get(i);
60                 IPath location = new Path(model.getInstallLocation());
61                 // defect 37319
62
if (location.segmentCount() > 2)
63                     location = location.removeFirstSegments(location.segmentCount() - 2);
64                 //31489 - entry must be relative
65
list[i] = location.setDevice(null).makeRelative().toString();
66             }
67             return list;
68         }
69     }
70
71     public static void createPlatformConfiguration(
72             File JavaDoc configLocation,
73             IPluginModelBase[] models,
74             IPluginModelBase brandingPlugin)
75             throws CoreException {
76         try {
77             IPlatformConfiguration platformConfiguration = ConfiguratorUtils.getPlatformConfiguration(null);
78     
79             // Compute local sites
80
ArrayList JavaDoc sites = new ArrayList JavaDoc();
81             for (int i = 0; i < models.length; i++) {
82                 IPath path = new Path(models[i].getInstallLocation()).removeLastSegments(2);
83                 addToSite(path, models[i], sites);
84             }
85             
86             createConfigurationEntries(platformConfiguration, sites);
87             
88             if (brandingPlugin != null)
89                 createFeatureEntries(platformConfiguration, brandingPlugin);
90             
91             platformConfiguration.refresh();
92             platformConfiguration.save(new URL JavaDoc("file:" + configLocation.getPath())); //$NON-NLS-1$
93
} catch (Exception JavaDoc e) {
94             // Wrap everything else in a core exception.
95
String JavaDoc message = e.getMessage();
96             if (message == null || message.length() == 0)
97                 message = PDECoreMessages.TargetPlatform_exceptionThrown;
98             throw new CoreException(
99                 new Status(IStatus.ERROR, PDECore.PLUGIN_ID, IStatus.ERROR, message, e));
100         }
101     }
102
103     private static void addToSite(
104         IPath path,
105         IPluginModelBase model,
106         ArrayList JavaDoc sites) {
107         if (path.getDevice() != null)
108             path = path.setDevice(path.getDevice().toUpperCase(Locale.ENGLISH));
109         for (int i = 0; i < sites.size(); i++) {
110             LocalSite localSite = (LocalSite) sites.get(i);
111             if (localSite.getPath().equals(path)) {
112                 localSite.add(model);
113                 return;
114             }
115         }
116         // First time - add site
117
LocalSite localSite = new LocalSite(path);
118         localSite.add(model);
119         sites.add(localSite);
120     }
121
122     private static void createConfigurationEntries(
123         IPlatformConfiguration config,
124         ArrayList JavaDoc sites)
125         throws CoreException, MalformedURLException JavaDoc {
126     
127         for (int i = 0; i < sites.size(); i++) {
128             LocalSite localSite = (LocalSite) sites.get(i);
129             String JavaDoc[] plugins = localSite.getRelativePluginList();
130     
131             int policy = IPlatformConfiguration.ISitePolicy.USER_INCLUDE;
132             IPlatformConfiguration.ISitePolicy sitePolicy =
133                 config.createSitePolicy(policy, plugins);
134             IPlatformConfiguration.ISiteEntry siteEntry =
135                 config.createSiteEntry(localSite.getURL(), sitePolicy);
136             config.configureSite(siteEntry);
137         }
138         config.isTransient(true);
139     }
140
141     private static void createFeatureEntries(IPlatformConfiguration config, IPluginModelBase plugin)
142                         throws MalformedURLException JavaDoc {
143         String JavaDoc id = plugin.getPluginBase().getId();
144         IFeatureModel featureModel = PDECore.getDefault().getFeatureModelManager().findFeatureModel(id);
145         if (featureModel != null) {
146             IFeature feature = featureModel.getFeature();
147             IPlatformConfiguration.IFeatureEntry featureEntry =
148                 config.createFeatureEntry(
149                     id,
150                     feature.getVersion(),
151                     id,
152                     plugin.getPluginBase().getVersion(),
153                     true,
154                     null,
155                     new URL JavaDoc[] { new URL JavaDoc("file:" + plugin.getInstallLocation()) }); //$NON-NLS-1$
156
config.configureFeatureEntry(featureEntry);
157         }
158     }
159
160 }
161
Popular Tags