TestVersionTracker.java revision 402794e73aed8611d62eb4b01cd155e2d76fcb87
1/*******************************************************************************
2 * Copyright (c) 2000, 2006 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 *******************************************************************************/
11package org.eclipse.releng.generators;
12/**
13 * This class finds the version of a plug-in, or fragment listed in a feature
14 * and writes <element>=<element>_<version> for each in a properties file.
15 * The file produced from this task can be loaded by an Ant script to find files in the
16 * binary versions of plugins and fragments.
17 */
18
19import java.io.BufferedInputStream;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.FileNotFoundException;
23import java.io.FileWriter;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.PrintWriter;
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Enumeration;
30import java.util.Hashtable;
31import java.util.StringTokenizer;
32
33import javax.xml.parsers.DocumentBuilder;
34import javax.xml.parsers.DocumentBuilderFactory;
35import javax.xml.parsers.ParserConfigurationException;
36import javax.xml.parsers.SAXParser;
37import javax.xml.parsers.SAXParserFactory;
38
39import org.apache.tools.ant.Task;
40import org.eclipse.osgi.framework.util.Headers;
41import org.eclipse.osgi.util.ManifestElement;
42import org.osgi.framework.BundleException;
43import org.osgi.framework.Constants;
44import org.w3c.dom.Document;
45import org.w3c.dom.NamedNodeMap;
46import org.w3c.dom.Node;
47import org.w3c.dom.NodeList;
48import org.xml.sax.Attributes;
49import org.xml.sax.InputSource;
50import org.xml.sax.SAXException;
51import org.xml.sax.SAXParseException;
52import org.xml.sax.helpers.DefaultHandler;
53
54public class TestVersionTracker extends Task{
55
56	private Hashtable elements;
57
58	//fields to hold temporary values for parsing
59	private SAXParser parser;
60
61	//the feature to from which to collect version information
62	private String featureId;
63
64	//buildDirectory
65	private String buildDirectory;
66
67	//the path to the file in which to write the results
68	private String outputFile;
69
70	public static void main(String[] args) {
71		TestVersionTracker tracker =
72		new TestVersionTracker();
73		tracker.buildDirectory="D:/src";
74		tracker.featureId="org.eclipse.sdk.tests";
75		tracker.outputFile="d:/eclipse-testing/test.properties";
76		tracker.execute();
77
78	}
79
80	public TestVersionTracker(){
81		super();
82	}
83
84
85	public void execute() {
86		elements = new Hashtable();
87
88		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
89		try {
90			parser = saxParserFactory.newSAXParser();
91		} catch (ParserConfigurationException e) {
92		  	e.printStackTrace();
93		} catch (SAXException e) {
94			e.printStackTrace();
95		}
96
97		parse(buildDirectory+"/features/"+featureId+"/build.xml",new FeatureHandler());
98		getTestPluginProperties();
99		writeProperties(outputFile, true);
100	}
101
102    public void parse(String xmlFile,DefaultHandler handler){
103         try {
104          parser.parse(xmlFile,handler);
105        } catch (SAXException e) {
106            System.err.println (e);
107        } catch (IOException e) {
108            System.err.println (e);
109        }
110    }
111
112    private void getTestPluginProperties(){
113    	//set prerequisites list for each test plugin
114    	Enumeration keys=elements.keys();
115    	while (keys.hasMoreElements()){
116    		String id=keys.nextElement().toString();
117    		TestPlugin testPlugin=(TestPlugin)elements.get(id);
118    		testPlugin.getPrerequisitePlugins(id);
119    		testPlugin.setHasPerformanceTarget();
120    	}
121    }
122
123    private class FeatureHandler extends DefaultHandler{
124    	//  Start Element Event Handler
125    	public void startElement(
126		 String uri,
127		 String local,
128			String qName,
129			Attributes atts) {
130
131    		if (qName.equals("eclipse.idReplacer")) {
132    			try{
133    				String pluginIds = atts.getValue("pluginIds");
134
135    				//get pluginIDs and versions from generated build.xml.  Create TestPlugin objects
136    				StringTokenizer tokenizer= new StringTokenizer(pluginIds,",");
137    				while (tokenizer.hasMoreTokens()){
138    					String idtmp=tokenizer.nextToken();
139    					String id=idtmp.substring(0, idtmp.indexOf(":"));
140    					String version=tokenizer.nextToken();
141    					TestPlugin testPlugin=new TestPlugin(id,version);
142    					elements.put(id,testPlugin);
143    				}
144    			} catch (Exception e){
145    				e.printStackTrace();
146       			}
147 			}
148    	}
149    }
150
151    private class TestPlugin {
152    	String id;
153    	String version;
154    	boolean hasPerformanceTarget=false;
155    	ArrayList prerequisitePlugins=new ArrayList();
156    	File testXml;
157
158    	TestPlugin(String id, String version){
159    		this.id=id;
160    		this.version=version;
161    		this.testXml=new File(buildDirectory,"plugins/"+id+"/test.xml");
162    	}
163
164    	private void getPrerequisitePlugins(String id) {
165			Headers headers = null;
166			String value = null;
167			File manifest = new File(buildDirectory, "plugins/" + id + "/META-INF/MANIFEST.MF");
168			ManifestElement[] manifestElements=null;
169			if (manifest.exists()) {
170				try {
171					headers = Headers.parseManifest(new FileInputStream(manifest));
172					if (headers.get(Constants.REQUIRE_BUNDLE)==null)
173						return;
174					value = headers.get(Constants.REQUIRE_BUNDLE).toString();
175					manifestElements = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, value);
176
177				} catch (BundleException e) {
178					e.printStackTrace();
179				} catch (FileNotFoundException e) {
180					e.printStackTrace();
181				}
182
183				for (int i = 0; i < manifestElements.length; i++) {
184					String name = manifestElements[i].getValue();
185					if (elements.containsKey(name)){
186						if (!prerequisitePlugins.contains(name)){
187							boolean prereqAdded=prerequisitePlugins.add(name);
188							if (prereqAdded){
189								getPrerequisitePlugins(name);
190							}
191						}
192					}
193				}
194			}
195			getPrerequisitePluginsFromPluginXml(id);
196		}
197
198    	 /**
199		 * Returns the required list of plug-ins from plugin.xml
200		 */
201		private void getPrerequisitePluginsFromPluginXml(String id) {
202			File pluginXml = new File(buildDirectory, "/plugins/" + id + "/plugin.xml");
203			if (!pluginXml.exists())
204				return;
205
206			InputStream is = null;
207			Document doc = null;
208			try {
209				is = new BufferedInputStream(new FileInputStream(pluginXml));
210				InputSource inputSource = new InputSource(is);
211
212				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
213
214				DocumentBuilder builder = null;
215
216				try {
217					builder = factory.newDocumentBuilder();
218				} catch (ParserConfigurationException e1) {
219					e1.printStackTrace();
220				}
221
222				try {
223					doc = builder.parse(inputSource);
224				} catch (SAXParseException e) {
225					e.printStackTrace();
226				}
227
228			} catch (IOException e) {
229				e.printStackTrace();
230			} catch (SAXException e) {
231				e.printStackTrace();
232			} finally {
233				try {
234					is.close();
235				} catch (IOException e) {
236				}
237			}
238			// Find the fragment's plugins's name, id and version
239			NodeList nodeList = doc.getElementsByTagName("import");
240			if (nodeList == null || nodeList.getLength() == 0) {
241				return;
242			}
243			for (int i = 0; i < nodeList.getLength(); i++) {
244				Node node = nodeList.item(i);
245				NamedNodeMap map = node.getAttributes();
246				Node namedItem = map.getNamedItem("plugin");
247				String name = namedItem.getNodeValue();
248				if (namedItem == null) {
249					continue;
250				} else {
251					if (elements.containsKey(name)) {
252						if (!prerequisitePlugins.contains(name)) {
253							boolean prereqAdded = prerequisitePlugins.add(name);
254							if (prereqAdded) {
255								getPrerequisitePlugins(name);
256							}
257						}
258					}
259				}
260			}
261		}
262
263   	 /**
264		 * Returns the required list of plug-ins from plugin.xml
265		 */
266		private void setHasPerformanceTarget() {
267			File testXml = new File(buildDirectory, "/plugins/" + id + "/test.xml");
268			if (!testXml.exists())
269				return;
270
271			InputStream is = null;
272			Document doc = null;
273			try {
274				is = new BufferedInputStream(new FileInputStream(testXml));
275				InputSource inputSource = new InputSource(is);
276
277				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
278
279				DocumentBuilder builder = null;
280
281				try {
282					builder = factory.newDocumentBuilder();
283				} catch (ParserConfigurationException e1) {
284					e1.printStackTrace();
285				}
286
287				try {
288					doc = builder.parse(inputSource);
289				} catch (SAXParseException e) {
290					e.printStackTrace();
291				}
292
293			} catch (IOException e) {
294				e.printStackTrace();
295			} catch (SAXException e) {
296				e.printStackTrace();
297			} finally {
298				try {
299					is.close();
300				} catch (IOException e) {
301				}
302			}
303			// Find a target named "performance"
304			NodeList nodeList = doc.getElementsByTagName("target");
305			if (nodeList == null || nodeList.getLength() == 0) {
306				return;
307			}
308			for (int i = 0; i < nodeList.getLength(); i++) {
309				Node node = nodeList.item(i);
310				NamedNodeMap map = node.getAttributes();
311				Node namedItem = map.getNamedItem("name");
312				String name = namedItem.getNodeValue();
313				if (namedItem == null) {
314					continue;
315				} else {
316					if (name.equals("performance")){
317						hasPerformanceTarget=true;
318						return;
319					}
320				}
321			}
322		}
323
324    	public String toString(){
325    		String keyPrefix=id+"_"+version;
326    		String performanceProperty=hasPerformanceTarget?id+".has.performance.target="+hasPerformanceTarget+"\n":"";
327    		return id+"="+keyPrefix+"\n"+
328    			performanceProperty+
329    			id+".prerequisite.testplugins="+getPrerequisiteList()+"\n";
330    	}
331
332    	private String getPrerequisiteList(){
333    		String prerequisites="";
334    		for (int i=0;i<prerequisitePlugins.size();i++){
335    			prerequisites=prerequisites.concat("**/${"+prerequisitePlugins.get(i)+"}** ");
336    		}
337    		return prerequisites;
338    	}
339    }
340
341	public void writeProperties(String propertiesFile,boolean append){
342
343		try{
344
345		PrintWriter writer = new PrintWriter(new FileWriter(propertiesFile,append));
346
347			Object[] keys = elements.keySet().toArray();
348			Arrays.sort(keys);
349			for (int i=0;i<keys.length;i++){
350				Object key = keys[i];
351				writer.println(((TestPlugin)elements.get(key)).toString());
352				writer.flush();
353			}
354			writer.close();
355
356		} catch (IOException e){
357			System.out.println("Unable to write to file "+propertiesFile);
358		}
359
360
361	}
362
363
364	/**
365	 * @return Returns the outputFilePath.
366	 */
367	public String getOutputFilePath() {
368		return outputFile;
369	}
370
371	/**
372	 * @param outputFilePath The outputFilePath to set.
373	 */
374	public void setOutputFilePath(String outputFilePath) {
375		this.outputFile = outputFilePath;
376	}
377
378	public String getBuildDirectory() {
379		return buildDirectory;
380	}
381
382	public void setBuildDirectory(String buildDirectory) {
383		this.buildDirectory = buildDirectory;
384	}
385
386	public String getFeatureId() {
387		return featureId;
388	}
389
390	public void setFeatureId(String featureId) {
391		this.featureId = featureId;
392	}
393
394	public String getOutputFile() {
395		return outputFile;
396	}
397
398	public void setOutputFile(String outputFile) {
399		this.outputFile = outputFile;
400	}
401
402
403
404
405}
406