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