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