]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/Far.java
1. Update release note to use jdk1.5.0_06 to take place of jdk1.5.0_04
[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
ef6e2efe 65\r
66 public void creatFar(List<PackageIdentification> pkgList, List<PlatformIdentification> plfList,\r
67 Set<String> fileFilter, FarHeader fHeader) throws Exception {\r
5a24e806 68 jos = new JarOutputStream(new FileOutputStream(jarFile));\r
69\r
70 //\r
71 // Generate Manifest and get file lists\r
72 //\r
73 this.mainfest = new Mainfest();\r
74 this.mainfest.setFarHeader(fHeader);\r
75 this.mainfest.createManifest(pkgList, plfList, fileFilter);\r
76\r
77 this.mainfest.hibernateToFile();\r
78\r
79 //\r
80 // Write Mainifest file to JAR.\r
81 //\r
82 if (this.mainfest.mfFile != null) {\r
83 writeToJar(this.mainfest.mfFile, jos);\r
84 }\r
85\r
86 //\r
87 // Write all files to JAR\r
88 //\r
89 Set<File> files = this.mainfest.files;\r
90 Iterator<File> iter = files.iterator();\r
91 while (iter.hasNext()) {\r
92 writeToJar(iter.next(), jos);\r
93 }\r
94 jos.close();\r
95 }\r
ef6e2efe 96\r
97 private void writeToJar(File file, JarOutputStream jos) throws Exception {\r
98 byte[] buffer = new byte[(int) file.length()];\r
99 FileInputStream fInput = new FileInputStream(file);\r
100 JarEntry entry = new JarEntry(\r
101 Tools\r
102 .convertPathToUnixType(Tools\r
103 .getRelativePath(file.getPath(),\r
104 Workspace.getCurrentWorkspace())));\r
105 jos.putNextEntry(entry);\r
106 fInput.read(buffer);\r
107 jos.write(buffer);\r
108 fInput.close();\r
5a24e806 109 }\r
110\r
111 public void InstallFar(String dir) throws Exception {\r
112 List<FarFileItem> allFile = mainfest.getAllFileItem();\r
113 extract(allFile, dir);\r
114 }\r
ef6e2efe 115\r
116 public void InstallFar(Map<PlatformIdentification, File> plfMap, Map<PackageIdentification, File> pkgMap)\r
117 throws Exception {\r
5a24e806 118 Set<PlatformIdentification> plfKeys = plfMap.keySet();\r
119 Iterator<PlatformIdentification> plfIter = plfKeys.iterator();\r
120 while (plfIter.hasNext()) {\r
121 PlatformIdentification item = plfIter.next();\r
122 extract(this.mainfest.getPlatformContents(item), plfMap.get(item).getPath());\r
123 }\r
124\r
125 Set<PackageIdentification> pkgKeys = pkgMap.keySet();\r
126 Iterator<PackageIdentification> pkgIter = pkgKeys.iterator();\r
127 while (pkgIter.hasNext()) {\r
128 PackageIdentification item = pkgIter.next();\r
129 installPackage(item, pkgMap.get(item));\r
130 }\r
131 jf.close();\r
132 }\r
133\r
134 public void installPackage(PackageIdentification packageId, File installPath) throws Exception {\r
135 List<FarFileItem> contents = this.mainfest.getPackageContents(packageId);\r
136 extract(contents, installPath.getPath());\r
137 }\r
138\r
139 public InputStream getMainfestFile() throws Exception {\r
140 JarEntry je = null;\r
141 for (Enumeration e = jf.entries(); e.hasMoreElements();) {\r
142 je = (JarEntry) e.nextElement();\r
143 if (je.getName().equalsIgnoreCase(Mainfest.mfFileName))\r
144 return jf.getInputStream(je);\r
145 }\r
146 return null;\r
147 }\r
148\r
149 public void createFar() {\r
150\r
151 }\r
152\r
153 public boolean hibernateToFile() {\r
154 return true;\r
155 }\r
ef6e2efe 156\r
157 // public static void main(String[] args){\r
158 // try {\r
159 // JarFile jarFile = new JarFile(new File("C:\\cvswork\\newEdk\\jar.jar.far"));\r
160 // JarEntry je= jarFile.getJarEntry("MdePkg/MdePkg.spd");\r
161 // InputStream is = jarFile.getInputStream(je);\r
162 // byte[] buffer = new byte[1]; \r
163 // File tempFile = new File("C:\\cvswork\\newEdk\\tempFile");\r
164 // File tfile2 = new File("C:\\cvswork\\newEdk\\tempFile1");\r
165 // FileOutputStream fos1 = new FileOutputStream(tfile2);\r
166 // FileOutputStream fos = new FileOutputStream(tempFile);\r
167 // int size = is.read(buffer);\r
168 // int totoalSize = size;\r
169 // while ( size >= 0) {\r
170 // fos.write(buffer);\r
171 // size = is.read(buffer);\r
172 // totoalSize = totoalSize + size;\r
173 // }\r
174 // \r
175 // \r
176 //// is = jarFile.getInputStream(je);\r
177 //// is.read(totalbuffer);\r
178 //// fos.write(totalbuffer);\r
179 // fos.close();\r
180 // byte[] totalbuffer = new byte[(int)tempFile.length()];\r
181 // FileInputStream fis = new FileInputStream(tempFile);\r
182 // fis.read(totalbuffer);\r
183 // fos1.write(totalbuffer);\r
184 // fos1.close();\r
185 // }catch(Exception e){\r
186 // \r
187 // }\r
188 // }\r
189\r
5a24e806 190 public void extract(List<FarFileItem> allFile, String dir) throws Exception {\r
191\r
192 Iterator filesItem = allFile.iterator();\r
193 FarFileItem ffItem;\r
194 JarEntry je;\r
195 new File(dir).mkdirs();\r
196 dir += File.separatorChar;\r
197 while (filesItem.hasNext()) {\r
198 try {\r
ef6e2efe 199 ffItem = (FarFileItem) filesItem.next();\r
5a24e806 200 je = jf.getJarEntry(Tools.convertPathToUnixType(ffItem.getDefaultPath()));\r
201 InputStream entryStream = jf.getInputStream(je);\r
202 File file = new File(dir + ffItem.getRelativeFilename());\r
203 file.getParentFile().mkdirs();\r
204 try {\r
205 //\r
206 // Create the output file (clobbering the file if it\r
207 // exists).\r
208 //\r
209 FileOutputStream outputStream = new FileOutputStream(file);\r
210\r
211 try {\r
212 //\r
213 // Read the entry data and write it to the output\r
214 // file.\r
215 //\r
ef6e2efe 216 byte[] buffer = new byte[1];\r
1898a0c3 217 File tempFile = new File("tempFile");\r
218 FileOutputStream fos = new FileOutputStream(tempFile);\r
219 int size = entryStream.read(buffer);\r
ef6e2efe 220 while (size >= 0) {\r
1898a0c3 221 fos.write(buffer);\r
222 size = entryStream.read(buffer);\r
223 }\r
ef6e2efe 224\r
1898a0c3 225 fos.close();\r
ef6e2efe 226 byte[] totalBuffer = new byte[(int) tempFile.length()];\r
1898a0c3 227 FileInputStream fis = new FileInputStream(tempFile);\r
228 fis.read(totalBuffer);\r
229 outputStream.write(totalBuffer);\r
230 fis.close();\r
231 tempFile.delete();\r
5a24e806 232 } finally {\r
233 outputStream.close();\r
234 }\r
235 } finally {\r
236 entryStream.close();\r
237 }\r
238\r
239 } finally {\r
5a24e806 240 }\r
241 }\r
ef6e2efe 242\r
243 }\r
244\r
245 public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {\r
246\r
5a24e806 247 }\r
248\r
249 /**\r
250 * \r
251 * @param pkgId\r
252 * @return\r
253 */\r
254 public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {\r
255 String[] entry = new String[2];\r
256 PackageQuery pkgQ = new PackageQuery();\r
257 List<PackageIdentification> result = new ArrayList<PackageIdentification>();\r
258\r
259 entry = this.mainfest.getPackgeSpd(pkgId);\r
ef6e2efe 260 if (entry == null) {\r
261 return result;\r
262 }\r
5a24e806 263 if (entry[0] != null) {\r
264 try {\r
265 JarEntry je;\r
266 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));\r
267 List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));\r
268 Iterator item = masList.iterator();\r
269 while (item.hasNext()) {\r
270 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));\r
271 List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));\r
272 Iterator pkgItem = pkgIdList.iterator();\r
273 while (pkgItem.hasNext()) {\r
274 PackageIdentification id = (PackageIdentification) pkgItem.next();\r
275 if (!AggregationOperation.belongs(id, result)) {\r
276 result.add(id);\r
277 }\r
278 }\r
279 }\r
280 } catch (Exception e) {\r
281 System.out.println(e.getMessage());\r
282 }\r
283 }\r
284 return result;\r
285 }\r
286}\r