1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Locale ; 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 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 (); 42 } 43 44 public IPath getPath() { 45 return fPath; 46 } 47 48 public URL getURL() throws MalformedURLException { 49 return new URL ("file:" + fPath.removeTrailingSeparator()); } 51 52 public void add(IPluginModelBase model) { 53 fPlugins.add(model); 54 } 55 56 public String [] getRelativePluginList() { 57 String [] list = new String [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 if (location.segmentCount() > 2) 63 location = location.removeFirstSegments(location.segmentCount() - 2); 64 list[i] = location.setDevice(null).makeRelative().toString(); 66 } 67 return list; 68 } 69 } 70 71 public static void createPlatformConfiguration( 72 File configLocation, 73 IPluginModelBase[] models, 74 IPluginModelBase brandingPlugin) 75 throws CoreException { 76 try { 77 IPlatformConfiguration platformConfiguration = ConfiguratorUtils.getPlatformConfiguration(null); 78 79 ArrayList sites = new ArrayList (); 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 ("file:" + configLocation.getPath())); } catch (Exception e) { 94 String 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 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 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 sites) 125 throws CoreException, MalformedURLException { 126 127 for (int i = 0; i < sites.size(); i++) { 128 LocalSite localSite = (LocalSite) sites.get(i); 129 String [] 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 { 143 String 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 [] { new URL ("file:" + plugin.getInstallLocation()) }); config.configureFeatureEntry(featureEntry); 157 } 158 } 159 160 } 161 | Popular Tags |