]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/PackageEditor/src/org/tianocore/packaging/FrameworkPkg.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / PackageEditor / src / org / tianocore / packaging / FrameworkPkg.java
1 /** @file
2 Java class FrameworkPkg is used to do package related operations.
3
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
9
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.
12 **/
13 package org.tianocore.packaging;
14
15 import java.io.*;
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.*;
21
22 import org.tianocore.PackageListDocument;
23
24 /**
25 This class deals with package related operations
26
27 @since PackageEditor 1.0
28 **/
29 public class FrameworkPkg {
30
31 private String pkg = null;
32
33 private JarFile jf = null;
34
35 ///
36 /// where the package will be extracted to
37 ///
38 private String wkDir = null;
39
40 private String bName = null;
41
42 private String pVer = null;
43
44 private String pGuid = null;
45
46 ///
47 /// current WORKSPACE location
48 ///
49 private String wkSpace = null;
50
51 private File dbFile = null;
52
53 private DbFileContents dfc = null;
54
55 ///
56 /// relative path of FrameworkDatabase.db file
57 ///
58 final static String dbConfigFile = "Tools" + System.getProperty("file.separator") + "Conf"
59 + System.getProperty("file.separator") + "FrameworkDatabase.db";
60
61
62 public FrameworkPkg() {
63
64 }
65
66 public FrameworkPkg(String package_name, String work_space) {
67 pkg = package_name;
68 wkSpace = work_space;
69 }
70
71 /**
72 install package (*.fdp file) to dir
73
74 @param dir Destination directory
75 @retval <0> Install successfully
76 @return int
77 @throws IOException
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
84 **/
85 public int install(final String dir) throws IOException, XmlException, DirSame, BasePkgNotInstalled, VerNotEqual,
86 GuidNotEqual, SameAll {
87 wkDir = dir;
88 pre_install();
89 extract(wkDir);
90 post_install();
91 return 0;
92 }
93
94 public int uninstall() {
95
96 return 0;
97 }
98
99 /**
100 Check package info. against Frameworkdatabase.db
101
102 @throws IOException
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
109 **/
110 protected void pre_install() throws IOException, XmlException, DirSame, BasePkgNotInstalled, VerNotEqual,
111 GuidNotEqual, SameAll {
112
113 jf = new JarFile(pkg);
114
115 ManifestContents manFile = new ManifestContents(getManifestInputStream(jf));
116
117 String baseName = manFile.getBaseName();
118 String pkgVersion = manFile.getVersion();
119 String pkgGuid = manFile.getGuid();
120 bName = baseName;
121 pVer = pkgVersion;
122 pGuid = pkgGuid;
123
124 if (dbFile == null) {
125 dbFile = new File(wkSpace + System.getProperty("file.separator") + dbConfigFile);
126 }
127 //
128 // the db file should exist if base packages have been installed
129 //
130 if (!dbFile.exists()) {
131 throw new BasePkgNotInstalled();
132 }
133
134 if (dfc == null) {
135 dfc = new DbFileContents(dbFile);
136 }
137 if (dfc.checkDir(wkDir) != 0) {
138 throw new DirSame();
139 }
140
141 //
142 // Get database info into lists
143 //
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);
147
148 //
149 // throw various kind of exceptions according to query return value.
150 //
151 if (i == DbFileContents.VERSION_NOT_EQUAL) {
152
153 jf.close();
154 throw new VerNotEqual(lpSameBase);
155 }
156 if (i == DbFileContents.GUID_NOT_EQUAL) {
157
158 jf.close();
159 throw new GuidNotEqual(lpSameVersion);
160 }
161 if (i == DbFileContents.SAME_ALL) {
162 jf.close();
163 throw new SameAll(lpSameVersion);
164 }
165
166 }
167
168 /**
169 Add package info into db file.
170
171 @throws IOException
172 @throws XmlException
173 **/
174 protected void post_install() throws IOException, XmlException {
175
176 dfc.addNewPkgInfo(bName, pVer, pGuid, wkDir.substring(wkSpace.length() + 1));
177
178 }
179
180 /**
181 Extract package to dir
182
183 @param dir Destination directory
184 @throws DirSame
185 @throws IOException
186 **/
187 private void extract(String dir) throws DirSame, IOException {
188
189 new File(dir).mkdirs();
190 dir += System.getProperty("file.separator");
191 try {
192 for (Enumeration e = jf.entries(); e.hasMoreElements();) {
193 JarEntry je = (JarEntry) e.nextElement();
194
195 //
196 // jar entry contains directory only, make these directories
197 //
198 if (je.isDirectory()) {
199 new File(dir + je.getName()).mkdirs();
200 continue;
201 }
202
203 //
204 // jar entry contains relative path and file name, make relative directories
205 // under destination dir
206 //
207 int index = je.getName().lastIndexOf(System.getProperty("file.separator"));
208 if (index != -1) {
209 String dirPath = je.getName().substring(0, index);
210 new File(dir + dirPath).mkdirs();
211 }
212
213 if (je != null) {
214 //
215 // Get an input stream for this entry.
216 //
217 InputStream entryStream = jf.getInputStream(je);
218
219 try {
220 //
221 // Create the output file (clobbering the file if it exists).
222 //
223 FileOutputStream file = new FileOutputStream(dir + je.getName());
224
225 try {
226
227 byte[] buffer = new byte[1024];
228 int bytesRead;
229 //
230 // Read the entry data and write it to the output file.
231 //
232 while ((bytesRead = entryStream.read(buffer)) != -1) {
233 file.write(buffer, 0, bytesRead);
234 }
235
236 System.out.println(je.getName() + " extracted.");
237 } finally {
238 file.close();
239 }
240 } finally {
241 entryStream.close();
242 }
243 }
244
245 }
246
247 } finally {
248 jf.close();
249
250 }
251
252 }
253
254 public String getBName() {
255 return bName;
256 }
257
258 public void setBName(String name) {
259 bName = name;
260 }
261
262 public File getDbFile() {
263 return dbFile;
264 }
265
266 public void setDbFile(File dbFile) {
267 this.dbFile = dbFile;
268 }
269
270 public DbFileContents getDfc() {
271 return dfc;
272 }
273
274 public void setDfc(DbFileContents dfc) {
275 this.dfc = dfc;
276 }
277
278 public String getPGuid() {
279 return pGuid;
280 }
281
282 public void setPGuid(String guid) {
283 pGuid = guid;
284 }
285
286 public String getPVer() {
287 return pVer;
288 }
289
290 public void setPVer(String ver) {
291 pVer = ver;
292 }
293
294 public String getWkDir() {
295 return wkDir;
296 }
297
298 public void setWkDir(String wkDir) {
299 this.wkDir = wkDir;
300 }
301
302 public String getWkSpace() {
303 return wkSpace;
304 }
305
306 public void setWkSpace(String wkSpace) {
307 this.wkSpace = wkSpace;
308 }
309
310 public JarFile getJf() {
311 return jf;
312 }
313
314 public void setJf(JarFile jf) {
315 this.jf = jf;
316 }
317
318 public String getPkg() {
319 return pkg;
320 }
321
322 public void setPkg(String pkg) {
323 this.pkg = pkg;
324 }
325
326 /**
327 Get the input stream of FDPManifest.xml file from jar entry
328
329 @param jf The Jar file that contains FDPManifest.xml file
330 @return InputStream
331 @throws IOException
332 **/
333 protected InputStream getManifestInputStream(JarFile jf) throws IOException {
334 JarEntry je = null;
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);
339 }
340
341 return null;
342 }
343
344 }
345
346
347 /**
348 Various Exception classes for what happened when database info and package info
349 are compared.
350
351 @since PackageEditor 1.0
352 **/
353 class DirSame extends Exception {
354 final static long serialVersionUID = 0;
355 }
356
357 class BasePkgNotInstalled extends Exception {
358 final static long serialVersionUID = 0;
359 }
360
361 class VerNotEqual extends Exception {
362 final static long serialVersionUID = 0;
363
364 //private String version = null;
365 List<PackageListDocument.PackageList.Package> lppSameBase = null;
366
367 VerNotEqual(List<PackageListDocument.PackageList.Package> ver) {
368 lppSameBase = ver;
369 }
370
371 public List<PackageListDocument.PackageList.Package> getVersion() {
372 return lppSameBase;
373 }
374 }
375
376 class GuidNotEqual extends Exception {
377 final static long serialVersionUID = 0;
378
379 private List<PackageListDocument.PackageList.Package> lppSameVer = null;
380
381 GuidNotEqual(List<PackageListDocument.PackageList.Package> ver) {
382 lppSameVer = ver;
383 }
384
385 public List<PackageListDocument.PackageList.Package> getGuid() {
386 return lppSameVer;
387 }
388 }
389
390 class SameAll extends Exception {
391 final static long serialVersionUID = 0;
392
393 private List<PackageListDocument.PackageList.Package> version = null;
394
395 SameAll(List<PackageListDocument.PackageList.Package> ver) {
396 version = ver;
397 }
398
399 public List<PackageListDocument.PackageList.Package> getVersion() {
400 return version;
401 }
402 }