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 Manifest
40 public Manifest manifest
= 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.manifest
= new Manifest(getManifestFile());
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.manifest
= new Manifest();
74 this.manifest
.setFarHeader(fHeader
);
75 this.manifest
.createManifest(pkgList
, plfList
, fileFilter
);
77 this.manifest
.hibernateToFile();
80 // Write Mainifest file to JAR.
82 if (this.manifest
.mfFile
!= null) {
83 writeToJar(this.manifest
.mfFile
, jos
);
87 // Write all files to JAR
89 Set
<File
> files
= this.manifest
.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
= manifest
.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.manifest
.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.manifest
.getPackageContents(packageId
);
136 extract(contents
, installPath
.getPath());
139 public InputStream
getManifestFile() throws Exception
{
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
);
149 public void createFar() {
153 public boolean hibernateToFile() {
157 public void extract(List
<FarFileItem
> allFile
, String dir
) throws Exception
{
159 Iterator filesItem
= allFile
.iterator();
162 new File(dir
).mkdirs();
163 dir
+= File
.separatorChar
;
164 while (filesItem
.hasNext()) {
166 ffItem
= (FarFileItem
) filesItem
.next();
167 je
= jf
.getJarEntry(Tools
.convertPathToUnixType(ffItem
.getDefaultPath()));
168 InputStream entryStream
= jf
.getInputStream(je
);
169 File file
= new File(dir
+ ffItem
.getRelativeFilename());
170 file
.getParentFile().mkdirs();
173 // Create the output file (clobbering the file if it
176 FileOutputStream outputStream
= new FileOutputStream(file
);
180 // Read the entry data and write it to the output
183 int fileLen
= (int)je
.getSize();
184 byte[] buffer
= new byte[fileLen
];
185 int len
= entryStream
.read(buffer
);
187 File tempFile
= new File("tempFile");
188 FileOutputStream fos
= new FileOutputStream(tempFile
);
189 fos
.write(buffer
, 0, len
);
190 while (len
< fileLen
){
191 int tempLen
= entryStream
.read(buffer
);
193 fos
.write(buffer
, 0, tempLen
);
196 FileInputStream fin
= new FileInputStream(tempFile
);
203 if (!ffItem
.getMd5Value().equalsIgnoreCase(FarMd5
.md5(buffer
))){
204 throw new Exception (ffItem
.getRelativeFilename() + " MD5 Checksum is invaild!");
206 outputStream
.write(buffer
);
208 outputStream
.close();
220 public void addFileToFar(File file
, JarOutputStream farOuputStream
, String workDir
) {
229 public List
<PackageIdentification
> getPackageDependencies(PackageIdentification pkgId
) {
230 String
[] entry
= new String
[2];
231 PackageQuery pkgQ
= new PackageQuery();
232 List
<PackageIdentification
> result
= new ArrayList
<PackageIdentification
>();
234 entry
= this.manifest
.getPackgeSpd(pkgId
);
238 if (entry
[0] != null) {
241 je
= jf
.getJarEntry(Tools
.convertPathToUnixType(entry
[1] + File
.separatorChar
+ entry
[0]));
242 List
<String
> masList
= pkgQ
.getPackageMsaList(jf
.getInputStream(je
));
243 Iterator item
= masList
.iterator();
244 while (item
.hasNext()) {
245 je
= jf
.getJarEntry(Tools
.convertPathToUnixType(entry
[1] + File
.separatorChar
+ item
.next()));
246 List
<PackageIdentification
> pkgIdList
= pkgQ
.getModuleDependencies(jf
.getInputStream(je
));
247 Iterator pkgItem
= pkgIdList
.iterator();
248 while (pkgItem
.hasNext()) {
249 PackageIdentification id
= (PackageIdentification
) pkgItem
.next();
250 if (!AggregationOperation
.belongs(id
, result
)) {
255 } catch (Exception e
) {
256 System
.out
.println(e
.getMessage());