]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/Far.java
1. Support to Create/Update/Delete/Install far file
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / far / Far.java
CommitLineData
5a24e806 1/** @file\r
2 \r
3 The file is used to create far file\r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15package org.tianocore.frameworkwizard.far;\r
16\r
17import java.io.File;\r
18import java.io.FileInputStream;\r
19import java.io.FileOutputStream;\r
20import java.io.InputStream;\r
21import java.util.ArrayList;\r
22import java.util.Enumeration;\r
23import java.util.Iterator;\r
24import java.util.List;\r
25import java.util.Map;\r
26import java.util.Set;\r
27import java.util.jar.JarEntry;\r
28import java.util.jar.JarFile;\r
29import java.util.jar.JarOutputStream;\r
30\r
31import org.tianocore.frameworkwizard.common.Tools;\r
32import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
33import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
34import org.tianocore.frameworkwizard.workspace.Workspace;\r
35\r
36public class Far {\r
37 //\r
38 // Class member Mainfest\r
39 //\r
40 public Mainfest mainfest = null;\r
41\r
42 //\r
43 // Jar file\r
44 //\r
45 private JarFile jf = null;\r
46\r
47 private File jarFile = null;\r
48\r
49 //\r
50 // Jar outputStream.\r
51 //\r
52 static public JarOutputStream jos = null;\r
53\r
54 public Far(File jarFile) {\r
55 this.jarFile = jarFile;\r
56 }\r
57\r
58 //\r
59 // For install/updat jar\r
60 //\r
61 public Far(JarFile farFile) throws Exception {\r
62 jf = farFile;\r
63 this.mainfest = new Mainfest(getMainfestFile());\r
64 }\r
65\r
66 public void creatFar(List<PackageIdentification> pkgList, List<PlatformIdentification> plfList,\r
67 Set<String> fileFilter, FarHeader fHeader) throws Exception {\r
68 jos = new JarOutputStream(new FileOutputStream(jarFile));\r
69\r
70 //\r
71 // Generate Manifest and get file lists\r
72 //\r
73 this.mainfest = new Mainfest();\r
74 this.mainfest.setFarHeader(fHeader);\r
75 this.mainfest.createManifest(pkgList, plfList, fileFilter);\r
76\r
77 this.mainfest.hibernateToFile();\r
78\r
79 //\r
80 // Write Mainifest file to JAR.\r
81 //\r
82 if (this.mainfest.mfFile != null) {\r
83 writeToJar(this.mainfest.mfFile, jos);\r
84 }\r
85\r
86 //\r
87 // Write all files to JAR\r
88 //\r
89 Set<File> files = this.mainfest.files;\r
90 Iterator<File> iter = files.iterator();\r
91 while (iter.hasNext()) {\r
92 writeToJar(iter.next(), jos);\r
93 }\r
94 jos.close();\r
95 }\r
96\r
97 private void writeToJar(File file, JarOutputStream jos) throws Exception {\r
98 byte[] buffer = new byte[(int) file.length()];\r
99 FileInputStream fInput = new FileInputStream(file);\r
100 JarEntry entry = new JarEntry(\r
101 Tools\r
102 .convertPathToUnixType(Tools\r
103 .getRelativePath(file.getPath(),\r
104 Workspace.getCurrentWorkspace())));\r
105 jos.putNextEntry(entry);\r
106 fInput.read(buffer);\r
107 jos.write(buffer);\r
108 fInput.close();\r
109 }\r
110\r
111 public void InstallFar(String dir) throws Exception {\r
112 List<FarFileItem> allFile = mainfest.getAllFileItem();\r
113 extract(allFile, dir);\r
114 }\r
115\r
116 public void InstallFar(Map<PlatformIdentification, File> plfMap, Map<PackageIdentification, File> pkgMap)\r
117 throws Exception {\r
118 Set<PlatformIdentification> plfKeys = plfMap.keySet();\r
119 Iterator<PlatformIdentification> plfIter = plfKeys.iterator();\r
120 while (plfIter.hasNext()) {\r
121 PlatformIdentification item = plfIter.next();\r
122 extract(this.mainfest.getPlatformContents(item), plfMap.get(item).getPath());\r
123 }\r
124\r
125 Set<PackageIdentification> pkgKeys = pkgMap.keySet();\r
126 Iterator<PackageIdentification> pkgIter = pkgKeys.iterator();\r
127 while (pkgIter.hasNext()) {\r
128 PackageIdentification item = pkgIter.next();\r
129 installPackage(item, pkgMap.get(item));\r
130 }\r
131 jf.close();\r
132 }\r
133\r
134 public void installPackage(PackageIdentification packageId, File installPath) throws Exception {\r
135 List<FarFileItem> contents = this.mainfest.getPackageContents(packageId);\r
136 extract(contents, installPath.getPath());\r
137 }\r
138\r
139 public InputStream getMainfestFile() throws Exception {\r
140 JarEntry je = null;\r
141 for (Enumeration e = jf.entries(); e.hasMoreElements();) {\r
142 je = (JarEntry) e.nextElement();\r
143 if (je.getName().equalsIgnoreCase(Mainfest.mfFileName))\r
144 return jf.getInputStream(je);\r
145 }\r
146 return null;\r
147 }\r
148\r
149 public void createFar() {\r
150\r
151 }\r
152\r
153 public boolean hibernateToFile() {\r
154 return true;\r
155 }\r
156\r
157 public void extract(List<FarFileItem> allFile, String dir) throws Exception {\r
158\r
159 Iterator filesItem = allFile.iterator();\r
160 FarFileItem ffItem;\r
161 JarEntry je;\r
162 new File(dir).mkdirs();\r
163 dir += File.separatorChar;\r
164 while (filesItem.hasNext()) {\r
165 try {\r
166 ffItem = (FarFileItem) filesItem.next();\r
167 // Enumeration<JarEntry> a = jf.entries();\r
168 // while (a.hasMoreElements()) {\r
169 // System.out.println("##" + a.nextElement().getName());\r
170 // }\r
171 je = jf.getJarEntry(Tools.convertPathToUnixType(ffItem.getDefaultPath()));\r
172 InputStream entryStream = jf.getInputStream(je);\r
173 File file = new File(dir + ffItem.getRelativeFilename());\r
174 file.getParentFile().mkdirs();\r
175 try {\r
176 //\r
177 // Create the output file (clobbering the file if it\r
178 // exists).\r
179 //\r
180 FileOutputStream outputStream = new FileOutputStream(file);\r
181\r
182 try {\r
183 //\r
184 // Read the entry data and write it to the output\r
185 // file.\r
186 //\r
187 int size = entryStream.available();\r
188 byte[] buffer = new byte[size];\r
189 outputStream.write(buffer);\r
190 // if (!(FarMd5.md5(buffer)).equalsIgnoreCase(ffItem.getMd5Value())){\r
191 // throw new Exception (je.getName() + " Md5 is invalided!");\r
192 // }\r
193\r
194 // System.out.println(je.getName() + " extracted.");\r
195 } finally {\r
196 outputStream.close();\r
197 }\r
198 } finally {\r
199 entryStream.close();\r
200 }\r
201\r
202 } finally {\r
203 //jf.close();\r
204 }\r
205 }\r
206\r
207 }\r
208\r
209 // public void installFarPackage (PackageIdentification pkgId, String dir) throws Exception{\r
210 // String pkgDir = null;\r
211 // List<FarFileItem> farFileList = new ArrayList<FarFileItem>();\r
212 // farFileList = this.mainfest.getPackageContents(pkgId);\r
213 // if (dir == null){\r
214 // pkgDir = this.mainfest.getPackageDefaultPath(pkgId);\r
215 // }else {\r
216 // pkgDir = dir;\r
217 // }\r
218 // extract(farFileList,pkgDir);\r
219 // }\r
220\r
221 public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {\r
222\r
223 }\r
224\r
225 /**\r
226 * \r
227 * @param pkgId\r
228 * @return\r
229 */\r
230 public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {\r
231 String[] entry = new String[2];\r
232 PackageQuery pkgQ = new PackageQuery();\r
233 List<PackageIdentification> result = new ArrayList<PackageIdentification>();\r
234\r
235 entry = this.mainfest.getPackgeSpd(pkgId);\r
236 if (entry[0] != null) {\r
237 try {\r
238 JarEntry je;\r
239 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));\r
240 List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));\r
241 Iterator item = masList.iterator();\r
242 while (item.hasNext()) {\r
243 je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));\r
244 List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));\r
245 Iterator pkgItem = pkgIdList.iterator();\r
246 while (pkgItem.hasNext()) {\r
247 PackageIdentification id = (PackageIdentification) pkgItem.next();\r
248 if (!AggregationOperation.belongs(id, result)) {\r
249 result.add(id);\r
250 }\r
251 }\r
252 }\r
253 } catch (Exception e) {\r
254 System.out.println(e.getMessage());\r
255 }\r
256 }\r
257 return result;\r
258 }\r
259}\r