+++ /dev/null
-/** @file\r
- \r
- The file is used to create far file\r
- \r
- Copyright (c) 2006, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
- \r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
- \r
- **/\r
-package org.tianocore.frameworkwizard.far;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.InputStream;\r
-import java.util.ArrayList;\r
-import java.util.Enumeration;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.jar.JarEntry;\r
-import java.util.jar.JarFile;\r
-import java.util.jar.JarOutputStream;\r
-\r
-import org.tianocore.frameworkwizard.common.Tools;\r
-import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
-import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
-import org.tianocore.frameworkwizard.workspace.Workspace;\r
-\r
-public class Far {\r
- //\r
- // Class member Manifest\r
- //\r
- public Manifest manifest = null;\r
-\r
- //\r
- // Jar file\r
- //\r
- private JarFile jf = null;\r
-\r
- private File jarFile = null;\r
-\r
- //\r
- // Jar outputStream.\r
- //\r
- static public JarOutputStream jos = null;\r
-\r
- public Far(File jarFile) {\r
- this.jarFile = jarFile;\r
- }\r
-\r
- //\r
- // For install/updat jar\r
- //\r
- public Far(JarFile farFile) throws Exception {\r
- jf = farFile;\r
- this.manifest = new Manifest(getManifestFile());\r
- }\r
-\r
- public void creatFar(List<PackageIdentification> pkgList, List<PlatformIdentification> plfList,\r
- Set<String> fileFilter, FarHeader fHeader) throws Exception {\r
- jos = new JarOutputStream(new FileOutputStream(jarFile));\r
-\r
- //\r
- // Generate Manifest and get file lists\r
- //\r
- this.manifest = new Manifest();\r
- this.manifest.setFarHeader(fHeader);\r
- this.manifest.createManifest(pkgList, plfList, fileFilter);\r
-\r
- this.manifest.hibernateToFile();\r
-\r
- //\r
- // Write Mainifest file to JAR.\r
- //\r
- if (this.manifest.mfFile != null) {\r
- writeToJar(this.manifest.mfFile, jos);\r
- }\r
-\r
- //\r
- // Write all files to JAR\r
- //\r
- Set<File> files = this.manifest.files;\r
- Iterator<File> iter = files.iterator();\r
- while (iter.hasNext()) {\r
- writeToJar(iter.next(), jos);\r
- }\r
- jos.close();\r
- }\r
-\r
- private void writeToJar(File file, JarOutputStream jos) throws Exception {\r
- byte[] buffer = new byte[(int) file.length()];\r
- FileInputStream fInput = new FileInputStream(file);\r
- JarEntry entry = new JarEntry(\r
- Tools\r
- .convertPathToUnixType(Tools\r
- .getRelativePath(file.getPath(),\r
- Workspace.getCurrentWorkspace())));\r
- jos.putNextEntry(entry);\r
- fInput.read(buffer);\r
- jos.write(buffer);\r
- fInput.close();\r
- }\r
-\r
- public void InstallFar(String dir) throws Exception {\r
- List<FarFileItem> allFile = manifest.getAllFileItem();\r
- extract(allFile, dir);\r
- }\r
-\r
- public void InstallFar(Map<PlatformIdentification, File> plfMap, Map<PackageIdentification, File> pkgMap)\r
- throws Exception {\r
- Set<PlatformIdentification> plfKeys = plfMap.keySet();\r
- Iterator<PlatformIdentification> plfIter = plfKeys.iterator();\r
- while (plfIter.hasNext()) {\r
- PlatformIdentification item = plfIter.next();\r
- extract(this.manifest.getPlatformContents(item), plfMap.get(item).getPath());\r
- }\r
-\r
- Set<PackageIdentification> pkgKeys = pkgMap.keySet();\r
- Iterator<PackageIdentification> pkgIter = pkgKeys.iterator();\r
- while (pkgIter.hasNext()) {\r
- PackageIdentification item = pkgIter.next();\r
- installPackage(item, pkgMap.get(item));\r
- }\r
- jf.close();\r
- }\r
-\r
- public void installPackage(PackageIdentification packageId, File installPath) throws Exception {\r
- List<FarFileItem> contents = this.manifest.getPackageContents(packageId);\r
- extract(contents, installPath.getPath());\r
- }\r
-\r
- public InputStream getManifestFile() throws Exception {\r
- JarEntry je = null;\r
- for (Enumeration e = jf.entries(); e.hasMoreElements();) {\r
- je = (JarEntry) e.nextElement();\r
- if (je.getName().equalsIgnoreCase(Manifest.mfFileName))\r
- return jf.getInputStream(je);\r
- }\r
- return null;\r
- }\r
-\r
- public void createFar() {\r
-\r
- }\r
-\r
- public boolean hibernateToFile() {\r
- return true;\r
- }\r
-\r
- public void extract(List<FarFileItem> allFile, String dir) throws Exception {\r
-\r
- Iterator filesItem = allFile.iterator();\r
- FarFileItem ffItem;\r
- JarEntry je;\r
- new File(dir).mkdirs();\r
- dir += File.separatorChar;\r
- while (filesItem.hasNext()) {\r
- try {\r
- ffItem = (FarFileItem) filesItem.next();\r
- je = jf.getJarEntry(Tools.convertPathToUnixType(ffItem.getDefaultPath()));\r
- InputStream entryStream = jf.getInputStream(je);\r
- File file = new File(dir + ffItem.getRelativeFilename());\r
- file.getParentFile().mkdirs();\r
- try {\r
- //\r
- // Create the output file (clobbering the file if it\r
- // exists).\r
- //\r
- FileOutputStream outputStream = new FileOutputStream(file);\r
-\r
- try {\r
- //\r
- // Read the entry data and write it to the output\r
- // file.\r
- //\r
- int fileLen = (int)je.getSize();\r
- byte[] buffer = new byte[fileLen];\r
- int len = entryStream.read(buffer);\r
- if (len < fileLen){\r
- File tempFile = new File("tempFile");\r
- FileOutputStream fos = new FileOutputStream(tempFile);\r
- fos.write(buffer, 0, len);\r
- while (len < fileLen){\r
- int tempLen = entryStream.read(buffer);\r
- len = len + tempLen;\r
- fos.write(buffer, 0, tempLen);\r
- }\r
- fos.close();\r
- FileInputStream fin = new FileInputStream(tempFile);\r
- fin.read(buffer);\r
- tempFile.delete();\r
- }\r
- //\r
- // Check Md5\r
- //\r
- if (!ffItem.getMd5Value().equalsIgnoreCase(FarMd5.md5(buffer))){\r
- throw new Exception (ffItem.getRelativeFilename() + " MD5 Checksum is invaild!");\r
- }\r
- outputStream.write(buffer);\r
- } finally {\r
- outputStream.close();\r
- }\r
- } finally {\r
- entryStream.close();\r
- }\r
-\r
- } finally {\r
- }\r
- }\r
-\r
- }\r
-\r
- public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {\r
-\r
- }\r
-\r
- /**\r
- * \r
- * @param pkgId\r
- * @return\r
- */\r
- public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {\r
- String[] entry = new String[2];\r
- PackageQuery pkgQ = new PackageQuery();\r
- List<PackageIdentification> result = new ArrayList<PackageIdentification>();\r
-\r
- entry = this.manifest.getPackgeSpd(pkgId);\r
- if (entry == null) {\r
- return result;\r
- }\r
- if (entry[0] != null) {\r
- try {\r
- JarEntry je;\r
- je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));\r
- List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));\r
- Iterator item = masList.iterator();\r
- while (item.hasNext()) {\r
- je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));\r
- List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));\r
- Iterator pkgItem = pkgIdList.iterator();\r
- while (pkgItem.hasNext()) {\r
- PackageIdentification id = (PackageIdentification) pkgItem.next();\r
- if (!AggregationOperation.belongs(id, result)) {\r
- result.add(id);\r
- }\r
- }\r
- }\r
- } catch (Exception e) {\r
- System.out.println(e.getMessage());\r
- }\r
- }\r
- return result;\r
- }\r
-}\r