• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/
11 package org.eclipse.releng;
12 
13 import java.io.BufferedReader;
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.FileReader;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.util.ArrayList;
21 
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25 
26 import org.apache.tools.ant.Task;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.NamedNodeMap;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
33 
34 public class UnpackUpdateJars extends Task {
35 
36 	/**
37 	 * @param args
38 	 */
39 	//parent to plugins and features directory which contains update jars
40 	private String site;
41 	private String output;
42 	ArrayList unpackedPlugins=new ArrayList();
43 
main(String[] args)44 	public static void main(String[] args) {
45 		// TODO Auto-generated method stub
46 		UnpackUpdateJars up=new UnpackUpdateJars();
47 		up.site="C:\\updatejars\\eclipse";
48 		up.output="C:\\updatejars\\newsite";
49 		up.execute();
50 	}
51 
UnpackUpdateJars()52 	public UnpackUpdateJars(){
53 		super();
54 	}
55 
56 	//unpack features, then unpack plug-ins which are not set to unpack="false"
execute()57 	public void execute(){
58 		new File(output).mkdirs();
59 		new File(output+"/features").mkdirs();
60 		new File(output+"/plugins").mkdirs();
61 
62 		//extract features
63 		File featureDir=new File(site,"features");
64 		if (!featureDir.exists()){
65 			return;
66 		}
67 		File[] features = featureDir.listFiles();
68 		for (int i = 0; i < features.length; i++) {
69 			File feature = features[i];
70 			if (feature.getName().endsWith(".jar")) {
71 				String fileName = feature.getName();
72 				String unpackedFeatureName = fileName.substring(0, fileName.length() - 4);
73 				File unPackedFeature=new File(output+"/features/"+ unpackedFeatureName);
74 				unzip(feature, unPackedFeature);
75 				getUnpackedPluginList(new File(unPackedFeature,"feature.xml"));
76 			}
77 		}
78 
79 		//unpack plug-ins
80 		for (int i=0;i<unpackedPlugins.size();i++){
81 			File unpackedPluginDirName=new File(output+"/plugins/"+(String)unpackedPlugins.get(i));
82 			File jardPlugin=new File(site,"plugins/"+(String)unpackedPlugins.get(i)+".jar");
83 			if (jardPlugin.exists())
84 				unzip (jardPlugin,unpackedPluginDirName);
85 		}
86 	}
87 
unzip(File src, File dest)88 	public void unzip(File src, File dest) {
89 		Runtime rt = Runtime.getRuntime();
90 		String command = "unzip -qo " + src.getPath() + " -d " + dest.getPath();
91 		System.out.println("[exec] "+command);
92 		Process proc = null;
93 		try {
94 			proc = rt.exec(command);
95 		} catch (IOException e) {
96 			// TODO Auto-generated catch block
97 			e.printStackTrace();
98 		}
99 		// pick up error messages
100 		StreamHandler errorHandler = new StreamHandler(proc.getErrorStream(), "ERROR");
101 
102 		// pick up output
103 		StreamHandler outputHandler = new StreamHandler(proc.getInputStream(), "OUTPUT");
104 
105 		// kick them off
106 		errorHandler.start();
107 		outputHandler.start();
108 
109 		// capture return code
110 		int returnCode = 0;
111 		try {
112 			returnCode = proc.waitFor();
113 		} catch (InterruptedException e) {
114 			// TODO Auto-generated catch block
115 			e.printStackTrace();
116 		}
117 		if (returnCode!=0)
118 			System.out.println("returnCode: " + returnCode);
119 
120 	}
121 
122 	class StreamHandler extends Thread {
123 		InputStream is;
124 
125 		String type;
126 
StreamHandler(InputStream is, String type)127 		StreamHandler(InputStream is, String type) {
128 			this.is = is;
129 			this.type = type;
130 		}
131 
run()132 		public void run() {
133 			try {
134 				InputStreamReader isr = new InputStreamReader(is);
135 				BufferedReader br = new BufferedReader(isr);
136 				String line = null;
137 				while ((line = br.readLine()) != null)
138 					System.out.println(type + ">" + line);
139 			} catch (IOException ioe) {
140 				ioe.printStackTrace();
141 			}
142 		}
143 	}
144 
getSite()145 	public String getSite() {
146 		return site;
147 	}
148 
setSite(String site)149 	public void setSite(String site) {
150 		this.site = site;
151 	}
152 
getUnpackedPluginList(File featureXml)153 	private void getUnpackedPluginList(File featureXml) {
154 		Document aDocument=null;
155 		BufferedReader reader = null;
156 		try {
157 			reader = new BufferedReader(new FileReader(featureXml));
158 		} catch (FileNotFoundException e) {
159 			e.printStackTrace();
160 		}
161 
162 		InputSource inputSource = new InputSource(reader);
163 		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
164 		DocumentBuilder builder = null;
165 
166 		try {
167 			builder = factory.newDocumentBuilder();
168 		} catch (ParserConfigurationException e) {
169 			e.printStackTrace();
170 		}
171 
172 		try {
173 			aDocument = builder.parse(inputSource);
174 		} catch (SAXException e) {
175 			e.printStackTrace();
176 		} catch (IOException e) {
177 			e.printStackTrace();
178 		}
179 		// Get feature attributes
180 		NodeList nodeList=aDocument.getElementsByTagName("plugin");
181 		if (nodeList==null)
182 			return;
183 
184 		for (int i = 0; i < nodeList.getLength(); i++) {
185 			Node pluginNode = nodeList.item(i);
186 			NamedNodeMap aNamedNodeMap = pluginNode.getAttributes();
187 			Node idNode = aNamedNodeMap.getNamedItem("id");
188 			Node versionNode = aNamedNodeMap.getNamedItem("version");
189 			String pluginDirName = idNode.getNodeValue() + "_" + versionNode.getNodeValue();
190 			Node unpackNode = aNamedNodeMap.getNamedItem("unpack");
191 			if (unpackNode == null) {
192 				if (!unpackedPlugins.contains(pluginDirName)) {
193 					unpackedPlugins.add(pluginDirName);
194 				}
195 				continue;
196 			}
197 
198 			if (unpackNode.getNodeValue().toString().trim().toLowerCase().equals("true")) {
199 				if (!unpackedPlugins.contains(pluginDirName)){
200 					System.out.println(pluginDirName);
201 					unpackedPlugins.add(pluginDirName);
202 				}
203 				continue;
204 			}
205 			//copy file to new location
206 			File jardPlugin=new File(site,"plugins/"+pluginDirName+".jar");
207 			if (jardPlugin.exists())
208 				if (!jardPlugin.renameTo(new File(output,"plugins/"+pluginDirName+".jar")))
209 					System.out.println("Failed to move "+jardPlugin.getAbsolutePath()+" to "+output+"plugins/"+pluginDirName+".jar");
210 		}
211 	}
212 
getOutput()213 	public String getOutput() {
214 		return output;
215 	}
216 
setOutput(String output)217 	public void setOutput(String output) {
218 		this.output = output;
219 	}
220 }
221