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