]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/PackageEditor/src/org/tianocore/packaging/FrameworkPkg.java
2 Java class FrameworkPkg is used to do package related operations.
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 package org
.tianocore
.packaging
;
16 import java
.util
.Enumeration
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
19 import java
.util
.jar
.*;
20 import org
.apache
.xmlbeans
.*;
22 import org
.tianocore
.PackageListDocument
;
25 This class deals with package related operations
27 @since PackageEditor 1.0
29 public class FrameworkPkg
{
31 private String pkg
= null;
33 private JarFile jf
= null;
36 /// where the package will be extracted to
38 private String wkDir
= null;
40 private String bName
= null;
42 private String pVer
= null;
44 private String pGuid
= null;
47 /// current WORKSPACE location
49 private String wkSpace
= null;
51 private File dbFile
= null;
53 private DbFileContents dfc
= null;
56 /// relative path of FrameworkDatabase.db file
58 final static String dbConfigFile
= "Tools" + System
.getProperty("file.separator") + "Conf"
59 + System
.getProperty("file.separator") + "FrameworkDatabase.db";
62 public FrameworkPkg() {
66 public FrameworkPkg(String package_name
, String work_space
) {
72 install package (*.fdp file) to dir
74 @param dir Destination directory
75 @retval <0> Install successfully
78 @throws XmlException Xml file exception
79 @throws DirSame One package already installed to dir
80 @throws BasePkgNotInstalled Some package must be installed first
81 @throws VerNotEqual At least one package info with same base name but version different
82 @throws GuidNotEqual At least one package info with same base name and version but guid different
83 @throws SameAll At least one package info with same base name, version and guid same
85 public int install(final String dir
) throws IOException
, XmlException
, DirSame
, BasePkgNotInstalled
, VerNotEqual
,
86 GuidNotEqual
, SameAll
{
94 public int uninstall() {
100 Check package info. against Frameworkdatabase.db
103 @throws XmlException Xml file exception
104 @throws DirSame One package already installed to dir
105 @throws BasePkgNotInstalled Some package must be installed first
106 @throws VerNotEqual At least one package info with same base name but version different
107 @throws GuidNotEqual At least one package info with same base name and version but guid different
108 @throws SameAll At least one package info with same base name, version and guid same
110 protected void pre_install() throws IOException
, XmlException
, DirSame
, BasePkgNotInstalled
, VerNotEqual
,
111 GuidNotEqual
, SameAll
{
113 jf
= new JarFile(pkg
);
115 ManifestContents manFile
= new ManifestContents(getManifestInputStream(jf
));
117 String baseName
= manFile
.getBaseName();
118 String pkgVersion
= manFile
.getVersion();
119 String pkgGuid
= manFile
.getGuid();
124 if (dbFile
== null) {
125 dbFile
= new File(wkSpace
+ System
.getProperty("file.separator") + dbConfigFile
);
128 // the db file should exist if base packages have been installed
130 if (!dbFile
.exists()) {
131 throw new BasePkgNotInstalled();
135 dfc
= new DbFileContents(dbFile
);
137 if (dfc
.checkDir(wkDir
) != 0) {
142 // Get database info into lists
144 List
<PackageListDocument
.PackageList
.Package
> lpSameBase
= new LinkedList
<PackageListDocument
.PackageList
.Package
>();
145 List
<PackageListDocument
.PackageList
.Package
> lpSameVersion
= new LinkedList
<PackageListDocument
.PackageList
.Package
>();
146 int i
= dfc
.query(baseName
, pkgVersion
, pkgGuid
, lpSameBase
, lpSameVersion
);
149 // throw various kind of exceptions according to query return value.
151 if (i
== DbFileContents
.VERSION_NOT_EQUAL
) {
154 throw new VerNotEqual(lpSameBase
);
156 if (i
== DbFileContents
.GUID_NOT_EQUAL
) {
159 throw new GuidNotEqual(lpSameVersion
);
161 if (i
== DbFileContents
.SAME_ALL
) {
163 throw new SameAll(lpSameVersion
);
169 Add package info into db file.
174 protected void post_install() throws IOException
, XmlException
{
176 dfc
.addNewPkgInfo(bName
, pVer
, pGuid
, wkDir
.substring(wkSpace
.length() + 1));
181 Extract package to dir
183 @param dir Destination directory
187 private void extract(String dir
) throws DirSame
, IOException
{
189 new File(dir
).mkdirs();
190 dir
+= System
.getProperty("file.separator");
192 for (Enumeration e
= jf
.entries(); e
.hasMoreElements();) {
193 JarEntry je
= (JarEntry
) e
.nextElement();
196 // jar entry contains directory only, make these directories
198 if (je
.isDirectory()) {
199 new File(dir
+ je
.getName()).mkdirs();
204 // jar entry contains relative path and file name, make relative directories
205 // under destination dir
207 int index
= je
.getName().lastIndexOf(System
.getProperty("file.separator"));
209 String dirPath
= je
.getName().substring(0, index
);
210 new File(dir
+ dirPath
).mkdirs();
215 // Get an input stream for this entry.
217 InputStream entryStream
= jf
.getInputStream(je
);
221 // Create the output file (clobbering the file if it exists).
223 FileOutputStream file
= new FileOutputStream(dir
+ je
.getName());
227 byte[] buffer
= new byte[1024];
230 // Read the entry data and write it to the output file.
232 while ((bytesRead
= entryStream
.read(buffer
)) != -1) {
233 file
.write(buffer
, 0, bytesRead
);
236 System
.out
.println(je
.getName() + " extracted.");
254 public String
getBName() {
258 public void setBName(String name
) {
262 public File
getDbFile() {
266 public void setDbFile(File dbFile
) {
267 this.dbFile
= dbFile
;
270 public DbFileContents
getDfc() {
274 public void setDfc(DbFileContents dfc
) {
278 public String
getPGuid() {
282 public void setPGuid(String guid
) {
286 public String
getPVer() {
290 public void setPVer(String ver
) {
294 public String
getWkDir() {
298 public void setWkDir(String wkDir
) {
302 public String
getWkSpace() {
306 public void setWkSpace(String wkSpace
) {
307 this.wkSpace
= wkSpace
;
310 public JarFile
getJf() {
314 public void setJf(JarFile jf
) {
318 public String
getPkg() {
322 public void setPkg(String pkg
) {
327 Get the input stream of FDPManifest.xml file from jar entry
329 @param jf The Jar file that contains FDPManifest.xml file
333 protected InputStream
getManifestInputStream(JarFile jf
) throws IOException
{
335 for (Enumeration e
= jf
.entries(); e
.hasMoreElements();) {
336 je
= (JarEntry
) e
.nextElement();
337 if (je
.getName().contains("FDPManifest.xml"))
338 return jf
.getInputStream(je
);
348 Various Exception classes for what happened when database info and package info
351 @since PackageEditor 1.0
353 class DirSame
extends Exception
{
354 final static long serialVersionUID
= 0;
357 class BasePkgNotInstalled
extends Exception
{
358 final static long serialVersionUID
= 0;
361 class VerNotEqual
extends Exception
{
362 final static long serialVersionUID
= 0;
364 //private String version = null;
365 List
<PackageListDocument
.PackageList
.Package
> lppSameBase
= null;
367 VerNotEqual(List
<PackageListDocument
.PackageList
.Package
> ver
) {
371 public List
<PackageListDocument
.PackageList
.Package
> getVersion() {
376 class GuidNotEqual
extends Exception
{
377 final static long serialVersionUID
= 0;
379 private List
<PackageListDocument
.PackageList
.Package
> lppSameVer
= null;
381 GuidNotEqual(List
<PackageListDocument
.PackageList
.Package
> ver
) {
385 public List
<PackageListDocument
.PackageList
.Package
> getGuid() {
390 class SameAll
extends Exception
{
391 final static long serialVersionUID
= 0;
393 private List
<PackageListDocument
.PackageList
.Package
> version
= null;
395 SameAll(List
<PackageListDocument
.PackageList
.Package
> ver
) {
399 public List
<PackageListDocument
.PackageList
.Package
> getVersion() {