1 11 package org.eclipse.pde.internal.ui.build; 12 13 import java.io.File ; 14 import java.util.regex.Pattern ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IFolder; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.core.runtime.jobs.MultiRule; 24 import org.eclipse.pde.internal.core.PDECore; 25 import org.eclipse.pde.internal.core.exports.FeatureExportInfo; 26 import org.eclipse.pde.internal.core.exports.FeatureExportOperation; 27 import org.eclipse.pde.internal.core.exports.SiteBuildOperation; 28 import org.eclipse.pde.internal.core.ifeature.IFeature; 29 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 30 import org.eclipse.pde.internal.core.isite.ISiteFeature; 31 import org.eclipse.pde.internal.core.isite.ISiteModel; 32 import org.eclipse.pde.internal.core.site.WorkspaceSiteModel; 33 import org.eclipse.pde.internal.core.util.PatternConstructor; 34 import org.eclipse.pde.internal.ui.PDEPlugin; 35 import org.eclipse.pde.internal.ui.PDEUIMessages; 36 import org.osgi.framework.Version; 37 38 public class BuildSiteJob extends FeatureExportJob { 39 40 private static FeatureExportInfo getInfo(IFeatureModel[] models, ISiteModel siteModel) { 41 FeatureExportInfo info = new FeatureExportInfo(); 42 info.useJarFormat = true; 43 info.toDirectory = true; 44 info.destinationDirectory = siteModel.getUnderlyingResource().getParent().getLocation().toOSString(); 45 info.items = models; 46 return info; 47 } 48 49 private long fBuildTime; 50 51 private IFeatureModel[] fFeatureModels; 52 private ISiteModel fSiteModel; 53 private IContainer fSiteContainer; 54 55 public BuildSiteJob(IFeatureModel[] features, ISiteModel site) { 56 super(getInfo(features, site), PDEUIMessages.BuildSiteJob_name); 57 fFeatureModels = features; 58 fSiteModel = site; 59 fSiteContainer = site.getUnderlyingResource().getParent(); 60 setRule(MultiRule.combine(fSiteContainer.getProject(), getRule())); 61 } 62 63 protected IStatus run(IProgressMonitor monitor) { 64 fBuildTime = System.currentTimeMillis(); 65 IStatus status = super.run(monitor); 66 try { 67 fSiteContainer.refreshLocal(IResource.DEPTH_INFINITE, monitor); 68 updateSiteFeatureVersions(); 69 } catch (CoreException ce) { 70 PDECore.logException(ce); 71 } 72 return status; 73 } 74 75 76 protected FeatureExportOperation createOperation() { 77 return new SiteBuildOperation(fInfo); 78 } 79 80 private void updateSiteFeatureVersions() { 81 try { 82 for (int i = 0; i < fFeatureModels.length; i++) { 83 IFeature feature = fFeatureModels[i].getFeature(); 84 Version pvi = Version.parseVersion(feature.getVersion()); 85 86 if ("qualifier".equals(pvi.getQualifier())) { String newVersion = findBuiltVersion(feature.getId(), pvi 88 .getMajor(), pvi.getMinor(), pvi.getMicro()); 89 if (newVersion == null) { 90 continue; 91 } 92 ISiteFeature reVersionCandidate = findSiteFeature(feature,pvi); 93 if (reVersionCandidate != null) { 94 reVersionCandidate.setVersion(newVersion); 95 reVersionCandidate 96 .setURL("features/" + feature.getId() + "_" + newVersion + ".jar"); } 99 } 100 } 101 ((WorkspaceSiteModel)fSiteModel).save(); 102 } catch (CoreException ce) { 103 PDEPlugin.logException(ce); 104 } 105 } 106 107 private ISiteFeature findSiteFeature(IFeature feature, Version pvi) { 108 ISiteFeature reversionCandidate = null; 109 ISiteFeature[] siteFeatures = fSiteModel.getSite().getFeatures(); 112 for (int s = 0; s < siteFeatures.length; s++) { 113 if (siteFeatures[s].getId().equals(feature.getId()) 114 && siteFeatures[s].getVersion() 115 .equals(feature.getVersion())) { 116 return siteFeatures[s]; 117 } 118 } 119 String highestQualifier = null; 120 for (int s = 0; s < siteFeatures.length; s++) { 122 if (siteFeatures[s].getId().equals(feature.getId())) { 123 Version candidatePvi = Version.parseVersion(siteFeatures[s].getVersion()); 124 if (pvi.getMajor() == candidatePvi.getMajor() 125 && pvi.getMinor() == candidatePvi.getMinor() 126 && pvi.getMicro() == candidatePvi.getMicro()) { 127 if (reversionCandidate == null 128 || candidatePvi.getQualifier().compareTo(highestQualifier) > 0) { 129 reversionCandidate = siteFeatures[s]; 130 highestQualifier = candidatePvi.getQualifier(); 131 } 132 } 133 } 134 } 135 return reversionCandidate; 136 } 137 138 150 private String findBuiltVersion(String id, int major, int minor, int service) { 151 IFolder featuresFolder = fSiteContainer.getFolder(new Path("features")); if (!featuresFolder.exists()) { 153 return null; 154 } 155 IResource[] featureJars = null; 156 try { 157 featureJars = featuresFolder.members(); 158 } catch (CoreException ce) { 159 return null; 160 } 161 Pattern pattern = PatternConstructor.createPattern(id + "_" + major + "." + minor + "." + service + "*.jar", true); String newestName = null; 167 long newestTime = 0; 168 for (int i = 0; i < featureJars.length; i++) { 169 File file = new File (featureJars[i].getLocation().toOSString()); 170 long jarTime = file.lastModified(); 171 String jarName = featureJars[i].getName(); 172 173 if (jarTime < fBuildTime) { 174 continue; 175 } 176 if (jarTime <= newestTime) { 177 continue; 178 } 179 if (pattern.matcher(jarName).matches()) { 180 newestName = featureJars[i].getName(); 181 newestTime = jarTime; 182 } 183 } 184 if (newestName == null) { 185 return null; 186 } 187 188 return newestName.substring(id.length() + 1, newestName.length() - 4); 189 } 190 191 196 protected String getLogFoundMessage() { 197 return PDEUIMessages.BuildSiteJob_message; 198 } 199 } 200 | Popular Tags |