]>
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 | |
214b0d19 | 38 | // Class member Manifest\r |
5a24e806 | 39 | //\r |
214b0d19 | 40 | public Manifest manifest = null;\r |
5a24e806 | 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 | |
214b0d19 | 63 | this.manifest = new Manifest(getManifestFile());\r |
5a24e806 | 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 | |
214b0d19 | 73 | this.manifest = new Manifest();\r |
74 | this.manifest.setFarHeader(fHeader);\r | |
75 | this.manifest.createManifest(pkgList, plfList, fileFilter);\r | |
5a24e806 | 76 | \r |
214b0d19 | 77 | this.manifest.hibernateToFile();\r |
5a24e806 | 78 | \r |
79 | //\r | |
80 | // Write Mainifest file to JAR.\r | |
81 | //\r | |
214b0d19 | 82 | if (this.manifest.mfFile != null) {\r |
83 | writeToJar(this.manifest.mfFile, jos);\r | |
5a24e806 | 84 | }\r |
85 | \r | |
86 | //\r | |
87 | // Write all files to JAR\r | |
88 | //\r | |
214b0d19 | 89 | Set<File> files = this.manifest.files;\r |
5a24e806 | 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 | |
214b0d19 | 112 | List<FarFileItem> allFile = manifest.getAllFileItem();\r |
5a24e806 | 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 | |
214b0d19 | 122 | extract(this.manifest.getPlatformContents(item), plfMap.get(item).getPath());\r |
5a24e806 | 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 | |
214b0d19 | 135 | List<FarFileItem> contents = this.manifest.getPackageContents(packageId);\r |
5a24e806 | 136 | extract(contents, installPath.getPath());\r |
137 | }\r | |
138 | \r | |
214b0d19 | 139 | public InputStream getManifestFile() throws Exception {\r |
5a24e806 | 140 | JarEntry je = null;\r |
141 | for (Enumeration e = jf.entries(); e.hasMoreElements();) {\r | |
142 | je = (JarEntry) e.nextElement();\r | |
214b0d19 | 143 | if (je.getName().equalsIgnoreCase(Manifest.mfFileName))\r |
5a24e806 | 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 |
5a24e806 | 157 | public void extract(List<FarFileItem> allFile, String dir) throws Exception {\r |
158 | \r | |
159 | Iterator filesItem = allFile.iterator();\r | |
160 | FarFileItem ffItem;\r | |
161 | JarEntry je;\r | |
162 | new File(dir).mkdirs();\r | |
163 | dir += File.separatorChar;\r | |
164 | while (filesItem.hasNext()) {\r | |
165 | try {\r | |
ef6e2efe | 166 | ffItem = (FarFileItem) filesItem.next();\r |
5a24e806 | 167 | je = jf.getJarEntry(Tools.convertPathToUnixType(ffItem.getDefaultPath()));\r |
168 | InputStream entryStream = jf.getInputStream(je);\r | |
169 | File file = new File(dir + ffItem.getRelativeFilename());\r | |
170 | file.getParentFile().mkdirs();\r | |
171 | try {\r | |
172 | //\r | |
173 | // Create the output file (clobbering the file if it\r | |
174 | // exists).\r | |
175 | //\r | |
176 | FileOutputStream outputStream = new FileOutputStream(file);\r | |
177 | \r | |
178 | try {\r | |
179 | //\r | |
180 | // Read the entry data and write it to the output\r | |
181 | // file.\r | |
182 | //\r | |
046d7d4f | 183 | int fileLen = (int)je.getSize();\r |
184 | byte[] buffer = new byte[fileLen];\r | |
185 | int len = entryStream.read(buffer);\r | |
186 | if (len < fileLen){\r | |
187 | File tempFile = new File("tempFile");\r | |
188 | FileOutputStream fos = new FileOutputStream(tempFile);\r | |
189 | fos.write(buffer, 0, len);\r | |
190 | while (len < fileLen){\r | |
191 | int tempLen = entryStream.read(buffer);\r | |
192 | len = len + tempLen;\r | |
193 | fos.write(buffer, 0, tempLen);\r | |
194 | }\r | |
195 | fos.close();\r | |
196 | FileInputStream fin = new FileInputStream(tempFile);\r | |
197 | fin.read(buffer);\r | |
198 | tempFile.delete();\r | |
1898a0c3 | 199 | }\r |
96531299 | 200 | //\r |
201 | // Check Md5\r | |
202 | //\r | |
046d7d4f | 203 | if (!ffItem.getMd5Value().equalsIgnoreCase(FarMd5.md5(buffer))){\r |
2aee15b8 | 204 | throw new Exception (ffItem.getRelativeFilename() + " MD5 Checksum is invaild!");\r |
96531299 | 205 | }\r |
046d7d4f | 206 | outputStream.write(buffer);\r |
5a24e806 | 207 | } finally {\r |
208 | outputStream.close();\r | |
209 | }\r | |
210 | } finally {\r | |
211 | entryStream.close();\r | |
212 | }\r | |
213 | \r | |
214 | } finally {\r | |
5a24e806 | 215 | }\r |
216 | }\r | |
ef6e2efe | 217 | \r |
218 | }\r | |
219 | \r | |
220 | public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {\r | |
221 | \r | |
5a24e806 | 222 | }\r |
223 | \r | |
224 | /**\r | |
225 | * \r | |
226 | * @param pkgId\r | |
227 | * @return\r | |
228 | */\r | |
229 | public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {\r | |
230 | String[] entry = new String[2];\r | |
231 | PackageQuery pkgQ = new PackageQuery();\r | |
232 | List<PackageIdentification> result = new ArrayList<PackageIdentification>();\r | |
233 | \r | |
214b0d19 | 234 | entry = this.manifest.getPackgeSpd(pkgId);\r |
ef6e2efe | 235 | if (entry == null) {\r |
236 | return result;\r | |
237 | }\r | |
5a24e806 | 238 | if (entry[0] != null) {\r |
239 | try {\r | |
240 | JarEntry je;\r | |
241 | je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));\r | |
242 | List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));\r | |
243 | Iterator item = masList.iterator();\r | |
244 | while (item.hasNext()) {\r | |
245 | je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));\r | |
246 | List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));\r | |
247 | Iterator pkgItem = pkgIdList.iterator();\r | |
248 | while (pkgItem.hasNext()) {\r | |
249 | PackageIdentification id = (PackageIdentification) pkgItem.next();\r | |
250 | if (!AggregationOperation.belongs(id, result)) {\r | |
251 | result.add(id);\r | |
252 | }\r | |
253 | }\r | |
254 | }\r | |
255 | } catch (Exception e) {\r | |
256 | System.out.println(e.getMessage());\r | |
257 | }\r | |
258 | }\r | |
259 | return result;\r | |
260 | }\r | |
261 | }\r |