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 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 // Enumeration<JarEntry> a = jf.entries();
168 // while (a.hasMoreElements()) {
169 // System.out.println("##" + a.nextElement().getName());
171 je
= jf
.getJarEntry(Tools
.convertPathToUnixType(ffItem
.getDefaultPath()));
172 InputStream entryStream
= jf
.getInputStream(je
);
173 File file
= new File(dir
+ ffItem
.getRelativeFilename());
174 file
.getParentFile().mkdirs();
177 // Create the output file (clobbering the file if it
180 FileOutputStream outputStream
= new FileOutputStream(file
);
184 // Read the entry data and write it to the output
187 int size
= entryStream
.available();
188 byte[] buffer
= new byte[size
];
189 outputStream
.write(buffer
);
190 // if (!(FarMd5.md5(buffer)).equalsIgnoreCase(ffItem.getMd5Value())){
191 // throw new Exception (je.getName() + " Md5 is invalided!");
194 // System.out.println(je.getName() + " extracted.");
196 outputStream
.close();
209 // public void installFarPackage (PackageIdentification pkgId, String dir) throws Exception{
210 // String pkgDir = null;
211 // List<FarFileItem> farFileList = new ArrayList<FarFileItem>();
212 // farFileList = this.mainfest.getPackageContents(pkgId);
214 // pkgDir = this.mainfest.getPackageDefaultPath(pkgId);
218 // extract(farFileList,pkgDir);
221 public void addFileToFar(File file
, JarOutputStream farOuputStream
, String workDir
) {
230 public List
<PackageIdentification
> getPackageDependencies(PackageIdentification pkgId
) {
231 String
[] entry
= new String
[2];
232 PackageQuery pkgQ
= new PackageQuery();
233 List
<PackageIdentification
> result
= new ArrayList
<PackageIdentification
>();
235 entry
= this.mainfest
.getPackgeSpd(pkgId
);
236 if (entry
[0] != null) {
239 je
= jf
.getJarEntry(Tools
.convertPathToUnixType(entry
[1] + File
.separatorChar
+ entry
[0]));
240 List
<String
> masList
= pkgQ
.getPackageMsaList(jf
.getInputStream(je
));
241 Iterator item
= masList
.iterator();
242 while (item
.hasNext()) {
243 je
= jf
.getJarEntry(Tools
.convertPathToUnixType(entry
[1] + File
.separatorChar
+ item
.next()));
244 List
<PackageIdentification
> pkgIdList
= pkgQ
.getModuleDependencies(jf
.getInputStream(je
));
245 Iterator pkgItem
= pkgIdList
.iterator();
246 while (pkgItem
.hasNext()) {
247 PackageIdentification id
= (PackageIdentification
) pkgItem
.next();
248 if (!AggregationOperation
.belongs(id
, result
)) {
253 } catch (Exception e
) {
254 System
.out
.println(e
.getMessage());