KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > build > BuildSiteJob


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.ui.build;
12
13 import java.io.File JavaDoc;
14 import java.util.regex.Pattern JavaDoc;
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())) { //$NON-NLS-1$
87
String JavaDoc 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() + "_" //$NON-NLS-1$ //$NON-NLS-2$
97
+ newVersion + ".jar"); //$NON-NLS-1$
98
}
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         // first see if version with qualifier being qualifier is present among
110
// site features
111
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 JavaDoc highestQualifier = null;
120         // then find feature with the highest qualifier
121
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     /**
139      * Finds the highest version from feature jars. ID and version components
140      * are constant. Qualifier varies
141      *
142      * @param builtJars
143      * candidate jars in format id_version.jar
144      * @param id
145      * @param major
146      * @param minor
147      * @param service
148      * @return
149      */

150     private String JavaDoc findBuiltVersion(String JavaDoc id, int major, int minor, int service) {
151         IFolder featuresFolder = fSiteContainer.getFolder(new Path("features")); //$NON-NLS-1$
152
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 JavaDoc pattern = PatternConstructor.createPattern(id + "_" //$NON-NLS-1$
162
+ major + "." //$NON-NLS-1$
163
+ minor + "." //$NON-NLS-1$
164
+ service + "*.jar", true); //$NON-NLS-1$
165
// finding the newest feature archive
166
String JavaDoc newestName = null;
167         long newestTime = 0;
168         for (int i = 0; i < featureJars.length; i++) {
169             File JavaDoc file = new File JavaDoc(featureJars[i].getLocation().toOSString());
170             long jarTime = file.lastModified();
171             String JavaDoc 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     /*
192      * (non-Javadoc)
193      *
194      * @see org.eclipse.pde.internal.ui.wizards.exports.FeatureExportJob#getLogFoundMessage()
195      */

196     protected String JavaDoc getLogFoundMessage() {
197         return PDEUIMessages.BuildSiteJob_message;
198     }
199 }
200
Popular Tags