]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/Far.java
Enhance Installation, removing and creating FAR functionality.
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / far / Far.java
CommitLineData
5a24e806 1/** @file\r
2 \r
3 The file is used to create far file\r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15package org.tianocore.frameworkwizard.far;\r
16\r
17import java.io.File;\r
18import java.io.FileInputStream;\r
19import java.io.FileOutputStream;\r
20import java.io.InputStream;\r
21import java.util.ArrayList;\r
22import java.util.Enumeration;\r
23import java.util.Iterator;\r
24import java.util.List;\r
25import java.util.Map;\r
26import java.util.Set;\r
27import java.util.jar.JarEntry;\r
28import java.util.jar.JarFile;\r
29import java.util.jar.JarOutputStream;\r
30\r
31import org.tianocore.frameworkwizard.common.Tools;\r
32import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
33import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
34import org.tianocore.frameworkwizard.workspace.Workspace;\r
35\r
36public class Far {\r
37 //\r
38 // Class member Mainfest\r
39 //\r
40 public Mainfest mainfest = null;\r
41\r
42 //\r
43 // Jar file\r
44 //\r
45 private JarFile jf = null;\r
46\r
47 private File jarFile = null;\r
48\r
49 //\r
50 // Jar outputStream.\r
51 //\r
52 static public JarOutputStream jos = null;\r
53\r
54 public Far(File jarFile) {\r
55 this.jarFile = jarFile;\r
56 }\r
57\r
58 //\r
59 // For install/updat jar\r
60 //\r
61 public Far(JarFile farFile) throws Exception {\r
62 jf = farFile;\r
63 this.mainfest = new Mainfest(getMainfestFile());\r
64 }\r
1898a0c3 65 \r
66 public void creatFar (List<PackageIdentification> pkgList,\r
67 List<PlatformIdentification> plfList, Set<String> fileFilter,\r
68 FarHeader fHeader) throws Exception{\r
5a24e806 69 jos = new JarOutputStream(new FileOutputStream(jarFile));\r
70\r
71 //\r
72 // Generate Manifest and get file lists\r
73 //\r
74 this.mainfest = new Mainfest();\r
75 this.mainfest.setFarHeader(fHeader);\r
76 this.mainfest.createManifest(pkgList, plfList, fileFilter);\r
77\r
78 this.mainfest.hibernateToFile();\r
79\r
80 //\r
81 // Write Mainifest file to JAR.\r
82 //\r
83 if (this.mainfest.mfFile != null) {\r
84 writeToJar(this.mainfest.mfFile, jos);\r
85 }\r
86\r
87 //\r
88 // Write all files to JAR\r
89 //\r
90 Set<File> files = this.mainfest.files;\r
91 Iterator<File> iter = files.iterator();\r
92 while (iter.hasNext()) {\r
93 writeToJar(iter.next(), jos);\r
94 }\r
95 jos.close();\r
96 }\r
1898a0c3 97 \r
98 private void writeToJar(File file, JarOutputStream jos) throws Exception{\r
99 byte[] buffer = new byte[(int)file.length()];\r
100 FileInputStream fInput = new FileInputStream(file);\r
101 JarEntry entry = new JarEntry(Tools.convertPathToUnixType(Tools.getRelativePath(file.getPath(),Workspace.getCurrentWorkspace())));\r
102 jos.putNextEntry(entry);\r
103 fInput.read(buffer);\r
104 jos.write(buffer);\r
105 fInput.close();\r
5a24e806 106 }\r
107\r
108 public void InstallFar(String dir) throws Exception {\r
109 List<FarFileItem> allFile = mainfest.getAllFileItem();\r
110 extract(allFile, dir);\r
111 }\r
1898a0c3 112 \r
113 public void InstallFar (Map<PlatformIdentification, File> plfMap, Map<PackageIdentification, File> pkgMap) throws Exception{\r
5a24e806 114 Set<PlatformIdentification> plfKeys = plfMap.keySet();\r
115 Iterator<PlatformIdentification> plfIter = plfKeys.iterator();\r
116 while (plfIter.hasNext()) {\r
117 PlatformIdentification item = plfIter.next();\r
118 extract(this.mainfest.getPlatformContents(item), plfMap.get(item).getPath());\r
119 }\r
120\r
121 Set<PackageIdentification> pkgKeys = pkgMap.keySet();\r
122 Iterator<PackageIdentification> pkgIter = pkgKeys.iterator();\r
123 while (pkgIter.hasNext()) {\r
124 PackageIdentification item = pkgIter.next();\r
125 installPackage(item, pkgMap.get(item));\r
126 }\r
127 jf.close();\r
128 }\r
129\r
130 public void installPackage(PackageIdentification packageId, File installPath) throws Exception {\r
131 List<FarFileItem> contents = this.mainfest.getPackageContents(packageId);\r
132 extract(contents, installPath.getPath());\r
133 }\r
134\r
135 public InputStream getMainfestFile() throws Exception {\r
136 JarEntry je = null;\r
137 for (Enumeration e = jf.entries(); e.hasMoreElements();) {\r
138 je = (JarEntry) e.nextElement();\r
139 if (je.getName().equalsIgnoreCase(Mainfest.mfFileName))\r
140 return jf.getInputStream(je);\r
141 }\r
142 return null;\r
143 }\r
144\r
145 public void createFar() {\r
146\r
147 }\r
148\r
149 public boolean hibernateToFile() {\r
150 return true;\r
151 }\r
1898a0c3 152// public static void main(String[] args){\r
153// try {\r
154// JarFile jarFile = new JarFile(new File("C:\\cvswork\\newEdk\\jar.jar.far"));\r
155// JarEntry je= jarFile.getJarEntry("MdePkg/MdePkg.spd");\r
156// InputStream is = jarFile.getInputStream(je);\r
157// byte[] buffer = new byte[1]; \r
158// File tempFile = new File("C:\\cvswork\\newEdk\\tempFile");\r
159// File tfile2 = new File("C:\\cvswork\\newEdk\\tempFile1");\r
160// FileOutputStream fos1 = new FileOutputStream(tfile2);\r
161// FileOutputStream fos = new FileOutputStream(tempFile);\r
162// int size = is.read(buffer);\r
163// int totoalSize = size;\r
164// while ( size >= 0) {\r
165// fos.write(buffer);\r
166// size = is.read(buffer);\r
167// totoalSize = totoalSize + size;\r
168// }\r
169// \r
170// \r
171//// is = jarFile.getInputStream(je);\r
172//// is.read(totalbuffer);\r
173//// fos.write(totalbuffer);\r
174// fos.close();\r
175// byte[] totalbuffer = new byte[(int)tempFile.length()];\r
176// FileInputStream fis = new FileInputStream(tempFile);\r
177// fis.read(totalbuffer);\r
178// fos1.write(totalbuffer);\r
179// fos1.close();\r
180// }catch(Exception e){\r
181// \r
182// }\r
183// }\r
184 \r
5a24e806 185 public void extract(List<FarFileItem> allFile, String dir) throws Exception {\r
186\r
187 Iterator filesItem = allFile.iterator();\r
188 FarFileItem ffItem;\r
189 JarEntry je;\r
190 new File(dir).mkdirs();\r
191 dir += File.separatorChar;\r
192 while (filesItem.hasNext()) {\r
193 try {\r
1898a0c3 194 ffItem = (FarFileItem)filesItem.next();\r
5a24e806 195 je = jf.getJarEntry(Tools.convertPathToUnixType(ffItem.getDefaultPath()));\r
196 InputStream entryStream = jf.getInputStream(je);\r
197 File file = new File(dir + ffItem.getRelativeFilename());\r
198 file.getParentFile().mkdirs();\r
199 try {\r
200 //\r
201 // Create the output file (clobbering the file if it\r
202 // exists).\r
203 //\r
204 FileOutputStream outputStream = new FileOutputStream(file);\r
1898a0c3 205 \r
5a24e806 206\r
207 try {\r
208 //\r
209 // Read the entry data and write it to the output\r
210 // file.\r
211 //\r
1898a0c3 212 byte[] buffer = new byte[1]; \r
213 File tempFile = new File("tempFile");\r
214 FileOutputStream fos = new FileOutputStream(tempFile);\r
215 int size = entryStream.read(buffer);\r
216 while ( size >= 0) {\r
217 fos.write(buffer);\r
218 size = entryStream.read(buffer);\r
219 }\r
220 \r
221 fos.close();\r
222 byte[] totalBuffer = new byte[(int)tempFile.length()];\r
223 FileInputStream fis = new FileInputStream(tempFile);\r
224 fis.read(totalBuffer);\r
225 outputStream.write(totalBuffer);\r
226 fis.close();\r
227 tempFile.delete();\r
5a24e806 228 } finally {\r
229 outputStream.close();\r
230 }\r
231 } finally {\r
232 entryStream.close();\r
233 }\r
234\r
235 } finally {\r
5a24e806 236 }\r
237 }\r
1898a0c3 238 \r
239 }\r
240 \r
241 \r
242 public void addFileToFar (File file, JarOutputStream farOuputStream, String workDir){\r
243 \r
5a24e806 244 }\r
245\r
246 /**\r
247 * \r
248 * @param pkgId\r
249 * @return\r
250 */\r
251 public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {\r
252 String[] entry = new String[2];\r
253 PackageQuery pkgQ = new PackageQuery();\r
254 List<PackageIdentification> result = new ArrayList<PackageIdentification>();\r
255\r
256 entry = this.mainfest.getPackgeSpd(pkgId);\r
257 if (entry[0] != null) {\r
258 try {\r
259 JarEntry je;\r
260 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));\r
261 List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));\r
262 Iterator item = masList.iterator();\r
263 while (item.hasNext()) {\r
264 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));\r
265 List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));\r
266 Iterator pkgItem = pkgIdList.iterator();\r
267 while (pkgItem.hasNext()) {\r
268 PackageIdentification id = (PackageIdentification) pkgItem.next();\r
269 if (!AggregationOperation.belongs(id, result)) {\r
270 result.add(id);\r
271 }\r
272 }\r
273 }\r
274 } catch (Exception e) {\r
275 System.out.println(e.getMessage());\r
276 }\r
277 }\r
278 return result;\r
279 }\r
280}\r