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
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 Mainfest
39 //
40 public Mainfest mainfest = 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.mainfest = new Mainfest(getMainfestFile());
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.mainfest = new Mainfest();
74 this.mainfest.setFarHeader(fHeader);
75 this.mainfest.createManifest(pkgList, plfList, fileFilter);
76
77 this.mainfest.hibernateToFile();
78
79 //
80 // Write Mainifest file to JAR.
81 //
82 if (this.mainfest.mfFile != null) {
83 writeToJar(this.mainfest.mfFile, jos);
84 }
85
86 //
87 // Write all files to JAR
88 //
89 Set<File> files = this.mainfest.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 = mainfest.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.mainfest.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.mainfest.getPackageContents(packageId);
136 extract(contents, installPath.getPath());
137 }
138
139 public InputStream getMainfestFile() throws Exception {
140 JarEntry je = null;
141 for (Enumeration e = jf.entries(); e.hasMoreElements();) {
142 je = (JarEntry) e.nextElement();
143 if (je.getName().equalsIgnoreCase(Mainfest.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 outputStream.write(totalBuffer);
230 fis.close();
231 tempFile.delete();
232 } finally {
233 outputStream.close();
234 }
235 } finally {
236 entryStream.close();
237 }
238
239 } finally {
240 }
241 }
242
243 }
244
245 public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {
246
247 }
248
249 /**
250 *
251 * @param pkgId
252 * @return
253 */
254 public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {
255 String[] entry = new String[2];
256 PackageQuery pkgQ = new PackageQuery();
257 List<PackageIdentification> result = new ArrayList<PackageIdentification>();
258
259 entry = this.mainfest.getPackgeSpd(pkgId);
260 if (entry == null) {
261 return result;
262 }
263 if (entry[0] != null) {
264 try {
265 JarEntry je;
266 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));
267 List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));
268 Iterator item = masList.iterator();
269 while (item.hasNext()) {
270 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));
271 List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));
272 Iterator pkgItem = pkgIdList.iterator();
273 while (pkgItem.hasNext()) {
274 PackageIdentification id = (PackageIdentification) pkgItem.next();
275 if (!AggregationOperation.belongs(id, result)) {
276 result.add(id);
277 }
278 }
279 }
280 } catch (Exception e) {
281 System.out.println(e.getMessage());
282 }
283 }
284 return result;
285 }
286 }