3 The file is used to create far file
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
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.
15 package org
.tianocore
.frameworkwizard
.far
;
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
;
27 import java
.util
.jar
.JarEntry
;
28 import java
.util
.jar
.JarFile
;
29 import java
.util
.jar
.JarOutputStream
;
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
;
38 // Class member Mainfest
40 public Mainfest mainfest
= null;
45 private JarFile jf
= null;
47 private File jarFile
= null;
52 static public JarOutputStream jos
= null;
54 public Far(File jarFile
) {
55 this.jarFile
= jarFile
;
59 // For install/updat jar
61 public Far(JarFile farFile
) throws Exception
{
63 this.mainfest
= new Mainfest(getMainfestFile());
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
));
71 // Generate Manifest and get file lists
73 this.mainfest
= new Mainfest();
74 this.mainfest
.setFarHeader(fHeader
);
75 this.mainfest
.createManifest(pkgList
, plfList
, fileFilter
);
77 this.mainfest
.hibernateToFile();
80 // Write Mainifest file to JAR.
82 if (this.mainfest
.mfFile
!= null) {
83 writeToJar(this.mainfest
.mfFile
, jos
);
87 // Write all files to JAR
89 Set
<File
> files
= this.mainfest
.files
;
90 Iterator
<File
> iter
= files
.iterator();
91 while (iter
.hasNext()) {
92 writeToJar(iter
.next(), jos
);
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(
102 .convertPathToUnixType(Tools
103 .getRelativePath(file
.getPath(),
104 Workspace
.getCurrentWorkspace())));
105 jos
.putNextEntry(entry
);
111 public void InstallFar(String dir
) throws Exception
{
112 List
<FarFileItem
> allFile
= mainfest
.getAllFileItem();
113 extract(allFile
, dir
);
116 public void InstallFar(Map
<PlatformIdentification
, File
> plfMap
, Map
<PackageIdentification
, File
> pkgMap
)
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());
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
));
134 public void installPackage(PackageIdentification packageId
, File installPath
) throws Exception
{
135 List
<FarFileItem
> contents
= this.mainfest
.getPackageContents(packageId
);
136 extract(contents
, installPath
.getPath());
139 public InputStream
getMainfestFile() throws Exception
{
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
);
149 public void createFar() {
153 public boolean hibernateToFile() {
157 // public static void main(String[] args){
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;
176 //// is = jarFile.getInputStream(je);
177 //// is.read(totalbuffer);
178 //// fos.write(totalbuffer);
180 // byte[] totalbuffer = new byte[(int)tempFile.length()];
181 // FileInputStream fis = new FileInputStream(tempFile);
182 // fis.read(totalbuffer);
183 // fos1.write(totalbuffer);
185 // }catch(Exception e){
190 public void extract(List
<FarFileItem
> allFile
, String dir
) throws Exception
{
192 Iterator filesItem
= allFile
.iterator();
195 new File(dir
).mkdirs();
196 dir
+= File
.separatorChar
;
197 while (filesItem
.hasNext()) {
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();
206 // Create the output file (clobbering the file if it
209 FileOutputStream outputStream
= new FileOutputStream(file
);
213 // Read the entry data and write it to the output
216 byte[] buffer
= new byte[1];
217 File tempFile
= new File("tempFile");
218 FileOutputStream fos
= new FileOutputStream(tempFile
);
219 int size
= entryStream
.read(buffer
);
222 size
= entryStream
.read(buffer
);
226 byte[] totalBuffer
= new byte[(int) tempFile
.length()];
227 FileInputStream fis
= new FileInputStream(tempFile
);
228 fis
.read(totalBuffer
);
229 outputStream
.write(totalBuffer
);
233 outputStream
.close();
245 public void addFileToFar(File file
, JarOutputStream farOuputStream
, String workDir
) {
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
>();
259 entry
= this.mainfest
.getPackgeSpd(pkgId
);
263 if (entry
[0] != null) {
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
)) {
280 } catch (Exception e
) {
281 System
.out
.println(e
.getMessage());