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
,
67 List
<PlatformIdentification
> plfList
, Set
<String
> fileFilter
,
68 FarHeader fHeader
) throws Exception
{
69 jos
= new JarOutputStream(new FileOutputStream(jarFile
));
72 // Generate Manifest and get file lists
74 this.mainfest
= new Mainfest();
75 this.mainfest
.setFarHeader(fHeader
);
76 this.mainfest
.createManifest(pkgList
, plfList
, fileFilter
);
78 this.mainfest
.hibernateToFile();
81 // Write Mainifest file to JAR.
83 if (this.mainfest
.mfFile
!= null) {
84 writeToJar(this.mainfest
.mfFile
, jos
);
88 // Write all files to JAR
90 Set
<File
> files
= this.mainfest
.files
;
91 Iterator
<File
> iter
= files
.iterator();
92 while (iter
.hasNext()) {
93 writeToJar(iter
.next(), jos
);
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
);
108 public void InstallFar(String dir
) throws Exception
{
109 List
<FarFileItem
> allFile
= mainfest
.getAllFileItem();
110 extract(allFile
, dir
);
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());
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
));
130 public void installPackage(PackageIdentification packageId
, File installPath
) throws Exception
{
131 List
<FarFileItem
> contents
= this.mainfest
.getPackageContents(packageId
);
132 extract(contents
, installPath
.getPath());
135 public InputStream
getMainfestFile() throws Exception
{
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
);
145 public void createFar() {
149 public boolean hibernateToFile() {
152 // public static void main(String[] args){
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;
171 //// is = jarFile.getInputStream(je);
172 //// is.read(totalbuffer);
173 //// fos.write(totalbuffer);
175 // byte[] totalbuffer = new byte[(int)tempFile.length()];
176 // FileInputStream fis = new FileInputStream(tempFile);
177 // fis.read(totalbuffer);
178 // fos1.write(totalbuffer);
180 // }catch(Exception e){
185 public void extract(List
<FarFileItem
> allFile
, String dir
) throws Exception
{
187 Iterator filesItem
= allFile
.iterator();
190 new File(dir
).mkdirs();
191 dir
+= File
.separatorChar
;
192 while (filesItem
.hasNext()) {
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();
201 // Create the output file (clobbering the file if it
204 FileOutputStream outputStream
= new FileOutputStream(file
);
209 // Read the entry data and write it to the output
212 byte[] buffer
= new byte[1];
213 File tempFile
= new File("tempFile");
214 FileOutputStream fos
= new FileOutputStream(tempFile
);
215 int size
= entryStream
.read(buffer
);
218 size
= entryStream
.read(buffer
);
222 byte[] totalBuffer
= new byte[(int)tempFile
.length()];
223 FileInputStream fis
= new FileInputStream(tempFile
);
224 fis
.read(totalBuffer
);
225 outputStream
.write(totalBuffer
);
229 outputStream
.close();
242 public void addFileToFar (File file
, JarOutputStream farOuputStream
, String workDir
){
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
>();
256 entry
= this.mainfest
.getPackgeSpd(pkgId
);
257 if (entry
[0] != null) {
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
)) {
274 } catch (Exception e
) {
275 System
.out
.println(e
.getMessage());