]>
Commit | Line | Data |
---|---|---|
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 | |
15 | package org.tianocore.frameworkwizard.far;\r | |
16 | \r | |
17 | import java.io.File;\r | |
18 | import java.io.FileInputStream;\r | |
19 | import java.io.FileOutputStream;\r | |
20 | import java.io.InputStream;\r | |
21 | import java.util.ArrayList;\r | |
22 | import java.util.Enumeration;\r | |
23 | import java.util.Iterator;\r | |
24 | import java.util.List;\r | |
25 | import java.util.Map;\r | |
26 | import java.util.Set;\r | |
27 | import java.util.jar.JarEntry;\r | |
28 | import java.util.jar.JarFile;\r | |
29 | import java.util.jar.JarOutputStream;\r | |
30 | \r | |
31 | import org.tianocore.frameworkwizard.common.Tools;\r | |
32 | import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r | |
33 | import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r | |
34 | import org.tianocore.frameworkwizard.workspace.Workspace;\r | |
35 | \r | |
36 | public 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 | |
96531299 | 229 | //\r |
230 | // Check Md5\r | |
231 | //\r | |
232 | if (!ffItem.getMd5Value().equalsIgnoreCase(FarMd5.md5(totalBuffer))){\r | |
2aee15b8 | 233 | throw new Exception (ffItem.getRelativeFilename() + " MD5 Checksum is invaild!");\r |
96531299 | 234 | }\r |
1898a0c3 | 235 | outputStream.write(totalBuffer);\r |
236 | fis.close();\r | |
237 | tempFile.delete();\r | |
5a24e806 | 238 | } finally {\r |
239 | outputStream.close();\r | |
240 | }\r | |
241 | } finally {\r | |
242 | entryStream.close();\r | |
243 | }\r | |
244 | \r | |
245 | } finally {\r | |
5a24e806 | 246 | }\r |
247 | }\r | |
ef6e2efe | 248 | \r |
249 | }\r | |
250 | \r | |
251 | public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {\r | |
252 | \r | |
5a24e806 | 253 | }\r |
254 | \r | |
255 | /**\r | |
256 | * \r | |
257 | * @param pkgId\r | |
258 | * @return\r | |
259 | */\r | |
260 | public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {\r | |
261 | String[] entry = new String[2];\r | |
262 | PackageQuery pkgQ = new PackageQuery();\r | |
263 | List<PackageIdentification> result = new ArrayList<PackageIdentification>();\r | |
264 | \r | |
265 | entry = this.mainfest.getPackgeSpd(pkgId);\r | |
ef6e2efe | 266 | if (entry == null) {\r |
267 | return result;\r | |
268 | }\r | |
5a24e806 | 269 | if (entry[0] != null) {\r |
270 | try {\r | |
271 | JarEntry je;\r | |
272 | je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));\r | |
273 | List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));\r | |
274 | Iterator item = masList.iterator();\r | |
275 | while (item.hasNext()) {\r | |
276 | je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));\r | |
277 | List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));\r | |
278 | Iterator pkgItem = pkgIdList.iterator();\r | |
279 | while (pkgItem.hasNext()) {\r | |
280 | PackageIdentification id = (PackageIdentification) pkgItem.next();\r | |
281 | if (!AggregationOperation.belongs(id, result)) {\r | |
282 | result.add(id);\r | |
283 | }\r | |
284 | }\r | |
285 | }\r | |
286 | } catch (Exception e) {\r | |
287 | System.out.println(e.getMessage());\r | |
288 | }\r | |
289 | }\r | |
290 | return result;\r | |
291 | }\r | |
292 | }\r |