blob: 05397e6b1f0218909c3117a0f15d0e65a3413820 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package org.openslx.firmware;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.commons.io.FileUtils;
import org.openslx.util.Resources;
public class QemuFirmwareTestResources
{
private static final String QEMU_PREFIX_PATH = Resources.PATH_SEPARATOR + "qemu";
private static final String QEMU_PREFIX_PATH_FW = QEMU_PREFIX_PATH + Resources.PATH_SEPARATOR + "firmware";
private static final String QEMU_TEMP_PREFIX = "qemu-";
public static String getQemuFirmwareSpecPath()
{
String fwSpecDir = null;
try {
fwSpecDir = getResourceDirectory( QemuFirmwareTestResources.class, QEMU_PREFIX_PATH_FW );
} catch ( IOException e ) {
fwSpecDir = null;
}
return fwSpecDir;
}
private static String getResourceDirectory( Class<?> clazz, String resourceDir ) throws IOException
{
final String fwDirPath = resourceDir.substring( 1 ).concat( Resources.PATH_SEPARATOR );
final URL fwResource = clazz.getResource( resourceDir );
final File fwDirectory;
String fwDirectoryPath = null;
if ( fwResource != null && "jar".equals( fwResource.getProtocol() ) ) {
// create temporary directory to copy Jar files into it
fwDirectory = Files.createTempDirectory( QEMU_TEMP_PREFIX ).toFile();
// obtain file list from a directory within the Jar file
// strip out only the JAR file path
final String jarPath = fwResource.getPath().substring( 5, fwResource.getPath().indexOf( "!" ) );
final JarFile jar = new JarFile( URLDecoder.decode( jarPath, "UTF-8" ) );
// get all entries in the Jar file
final Enumeration<JarEntry> jarEntries = jar.entries();
final Set<String> fileNames = new HashSet<String>();
while ( jarEntries.hasMoreElements() ) {
final String jarEntryName = jarEntries.nextElement().getName();
if ( jarEntryName.startsWith( fwDirPath ) ) {
String jarEntry = jarEntryName.substring( fwDirPath.length() );
if ( !jarEntry.isEmpty() ) {
fileNames.add( jarEntry );
}
}
}
// copy each file from the Jar to the temporary directory
fileNames.forEach( fileName -> {
final String resourceFileName = resourceDir + Resources.PATH_SEPARATOR + fileName;
final File tempFile = new File( fwDirectory.getPath() + File.separator + fileName );
final InputStream fileInput = QemuFirmwareTestResources.class.getResourceAsStream( resourceFileName );
try {
FileUtils.copyInputStreamToFile( fileInput, tempFile );
} catch ( IOException e ) {
e.printStackTrace();
}
tempFile.deleteOnExit();
} );
fwDirectory.deleteOnExit();
} else if ( fwResource != null && "file".equals( fwResource.getProtocol() ) ) {
fwDirectory = new File( fwResource.getFile() );
} else {
fwDirectory = null;
}
try {
fwDirectoryPath = fwDirectory.toURI().toURL().getFile();
} catch ( MalformedURLException | NullPointerException e ) {
fwDirectoryPath = null;
}
return fwDirectoryPath;
}
public static File getQemuFirmwareSpecFile( String fileName )
{
final String fwSpecFilePath = QEMU_PREFIX_PATH_FW + Resources.PATH_SEPARATOR + fileName;
final URL fwSpecFileUrl = QemuFirmwareTestResources.class.getResource( fwSpecFilePath );
return new File( fwSpecFileUrl.getFile() );
}
}
|