下面列出了java.net.URL#getFile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String getResourceFilePath(String location) {
String filePath = null;
LOG.debug("Loading " + location + " as ResourceAsString");
InputStream stream = PlatformPCSC.class.getResourceAsStream(location);
if (stream != null) {
filePath = PlatformPCSC.class.getResource(location).getFile();
} else {
File file = new File(location);
if (!file.exists()) {
try {
URL resource = new URL(location);
filePath = resource.getFile();
} catch (MalformedURLException var5) {
LOG.error("location [" + location + "] could not be retrieved as URL, classpath resource or file.");
}
} else {
filePath = location;
}
}
return filePath;
}
/**
* Resolve the given resource URL to a {@code java.io.File},
* i.e. to a file in the file system.
* @param resourceUrl the resource URL to resolve
* @param description a description of the original resource that
* the URL was created for (for example, a class path location)
* @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
*/
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
Assert.notNull(resourceUrl, "Resource URL must not be null");
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
throw new FileNotFoundException(
description + " cannot be resolved to absolute file path " +
"because it does not reside in the file system: " + resourceUrl);
}
try {
return new File(toURI(resourceUrl).getSchemeSpecificPart());
}
catch (URISyntaxException ex) {
// Fallback for URLs that are not valid URIs (should hardly ever happen).
return new File(resourceUrl.getFile());
}
}
/**
* ALF-219. Transforamtion from .html to .pdf for empty file.
* @throws Exception
*/
// The test was never run and fails on remote transformer
public void ignoreTestEmptyHtmlToEmptyPdf() throws Exception
{
if (!isOpenOfficeWorkerAvailable())
{
// no connection
System.err.println("ooWorker not available - skipping testEmptyHtmlToEmptyPdf !!");
return;
}
URL url = this.getClass().getClassLoader().getResource("misc/empty.html");
assertNotNull("URL was unexpectedly null", url);
File htmlSourceFile = new File(url.getFile());
assertTrue("Test file does not exist.", htmlSourceFile.exists());
File pdfTargetFile = TempFileProvider.createTempFile(getName() + "-target-", ".pdf");
ContentReader reader = new FileContentReader(htmlSourceFile);
reader.setMimetype(MimetypeMap.MIMETYPE_HTML);
ContentWriter writer = new FileContentWriter(pdfTargetFile);
writer.setMimetype(MimetypeMap.MIMETYPE_PDF);
transformer.transform(reader, writer);
}
@Test
public void testRoundTripPOMs()
throws Exception
{
URL resource = PomIOTest.class.getResource( filename );
assertNotNull( resource );
File pom = new File( resource.getFile() );
assertTrue( pom.exists() );
File targetFile = folder.newFile( "target.xml" );
FileUtils.copyFile( pom, targetFile );
List<Project> projects = pomIO.parseProject( targetFile );
assertNull( projects.get( 0 ).getModel().getModelEncoding() );
// We don't want this to be the execution root so that it doesn't add "Modified by" which breaks the comparison
FieldUtils.writeDeclaredField( projects.get( 0 ), "executionRoot", false, true);
HashSet<Project> changed = new HashSet<>(projects);
pomIO.rewritePOMs( changed );
assertTrue( FileUtils.contentEqualsIgnoreEOL( pom, targetFile, StandardCharsets.UTF_8.toString() ) );
assertTrue( FileUtils.contentEquals( targetFile, pom ) );
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
/**
* Given a package name, attempts to reflect to find all classes within the
* package on the local file system.
*
* @param packageName
* @return Class list
*/
public static List<Class> getClassesInPackage(String packageName) {
List<Class> classes = new ArrayList<>();
String packageNameSlashed = "/" + packageName.replace(".", "/");
// Get a File object for the package
URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
if (directoryURL == null) {
System.out.println("Could not retrieve URL resource: " + packageNameSlashed);
return classes;
}
String directoryString = directoryURL.getFile();
if (directoryString == null) {
System.out.println("Could not find directory for URL resource: " + packageNameSlashed);
return classes;
}
File directory = new File(directoryString);
if (directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (String fileName : files) {
// We are only interested in .class files
if (fileName.endsWith(".class")) {
// Remove the .class extension
fileName = fileName.substring(0, fileName.length() - 6);
try {
classes.add(Class.forName(packageName + "." + fileName));
} catch (ClassNotFoundException e) {
System.out.println(packageName + "." + fileName + " does not appear to be a valid class.");
}
}
}
} else {
System.out.println(packageName + " does not appear to exist as a valid package on the file system.");
}
return classes;
}
/**
* Helper that constructs a normalized url string
* usable as cache key.
*
* @param url a URL object
* @return the normalized string
*/
public static String normalize(final URL url) {
final StringBuilder result = new StringBuilder();
result.append(url.getProtocol())
.append("://")
.append(url.getHost())
.append(':')
.append((url.getPort() != -1) ? url.getPort() : url.getDefaultPort());
// Compare the files.
String f = url.getFile();
if (f.isEmpty()) {
result.append('/');
}
else {
if (f.indexOf('.') > 0) {
try {
f = url.toURI().normalize().toURL().getFile();
}
catch (final Exception e) {
// ignore
}
}
result.append(f);
}
return result.toString();
}
private static String getAbsolutePath(String path) throws FileNotFoundException
{
URL resource = Thread.currentThread().getContextClassLoader().getResource(path);
if (resource == null)
throw new FileNotFoundException(path);
return resource.getFile();
}
JarLoader(URL url, URLStreamHandler jarHandler,
HashMap<String, Loader> loaderMap)
throws IOException
{
super(new URL("jar", "", -1, url + "!/", jarHandler));
csu = url;
handler = jarHandler;
lmap = loaderMap;
if (!isOptimizable(url)) {
ensureOpen();
} else {
String fileName = url.getFile();
if (fileName != null) {
fileName = ParseUtil.decode(fileName);
File f = new File(fileName);
metaIndex = MetaIndex.forJar(f);
// If the meta index is found but the file is not
// installed, set metaIndex to null. A typical
// senario is charsets.jar which won't be installed
// when the user is running in certain locale environment.
// The side effect of null metaIndex will cause
// ensureOpen get called so that IOException is thrown.
if (metaIndex != null && !f.exists()) {
metaIndex = null;
}
}
// metaIndex is null when either there is no such jar file
// entry recorded in meta-index file or such jar file is
// missing in JRE. See bug 6340399.
if (metaIndex == null) {
ensureOpen();
}
}
}
public static JarFile getJarFile(final URL url) throws IOException {
final File cacheFile = getCacheFile(url, false);
synchronized (getLock(cacheFile)) {
if (!cacheFile.exists()) {
if (Urls.isLocalFile(url)) {
return new JarFile(url.getFile());
}
throw new java.io.FileNotFoundException("JAR file cannot be obtained for a URL that is not cached locally: " + url + ".");
}
cacheFile.setLastModified(System.currentTimeMillis());
return new JarFile(cacheFile);
}
}
@Test
public void testMultipleClose() throws IOException {
URL testFileUrl = getClass().getClassLoader().
getResource("recordSpanningMultipleSplits.txt.bz2");
assertNotNull("Cannot find recordSpanningMultipleSplits.txt.bz2",
testFileUrl);
File testFile = new File(testFileUrl.getFile());
Path testFilePath = new Path(testFile.getAbsolutePath());
long testFileSize = testFile.length();
Configuration conf = new Configuration();
conf.setInt(org.apache.hadoop.mapreduce.lib.input.
LineRecordReader.MAX_LINE_LENGTH, Integer.MAX_VALUE);
TaskAttemptContext context = new TaskAttemptContextImpl(conf, new TaskAttemptID());
// read the data and check whether BOM is skipped
FileSplit split = new FileSplit(testFilePath, 0, testFileSize, null);
LineRecordReader reader = new LineRecordReader();
reader.initialize(split, context);
//noinspection StatementWithEmptyBody
while (reader.nextKeyValue()) ;
reader.close();
reader.close();
BZip2Codec codec = new BZip2Codec();
codec.setConf(conf);
Set<Decompressor> decompressors = new HashSet<Decompressor>();
for (int i = 0; i < 10; ++i) {
decompressors.add(CodecPool.getDecompressor(codec));
}
assertEquals(10, decompressors.size());
}
protected String getTestFileAsString(String name) throws FileNotFoundException {
String result = null;
String url = System.getProperty("file.separator") + this.getClass().getSimpleName()
+ System.getProperty("file.separator") + name;
URL resource = this.getClass().getResource(url);
if (resource != null) {
Scanner s = new Scanner(new File(resource.getFile()));
s.useDelimiter("\\Z");
result = (s.hasNext() ? s.next() : "");
s.close();
}
return result;
}
/**
* Creates a new file container with an url.
*
* @param file The file as an url.
* @param isSpoiler Whether the file is to be marked as spoiler.
*/
public FileContainer(URL file, boolean isSpoiler) {
fileAsBufferedImage = null;
fileAsFile = null;
fileAsIcon = null;
fileAsUrl = file;
fileAsByteArray = null;
fileAsInputStream = null;
fileTypeOrName = (isSpoiler ? "SPOILER_" : "") + file.getFile();
}
private ContentReader openQuickTestFile(String filename)
{
URL url = getClass().getClassLoader().getResource("quick/" + filename);
if(url == null)
{
fail("Quick test file \"" + filename + "\" wasn't found");
}
File file = new File(url.getFile());
return new FileContentReader(file);
}
private static void getByPackage(String packageName, Set<String> contents, String fileSuffix, TransformHandler handler) throws Exception {
Enumeration<URL> baseURLs = ResourceScanner.class.getClassLoader().getResources(packageName);
URL baseURL = null;
while (baseURLs.hasMoreElements()) {
baseURL = baseURLs.nextElement();
if (baseURL != null) {
String protocol = baseURL.getProtocol();
String basePath = baseURL.getFile();
//TODO xml和class 检测
if ("jar".equals(protocol)) {
//TODO check jar包内资源扫描可能出现的问题
//实际运行中看到了这样的形式的jar目录 /BOOT-INF/classes!/com ,classes是一个目录,去掉后面的!,原因还没弄明白.
basePath = basePath.replace("classes!", "classes");
String[] paths = basePath.split("jar!/");
if (paths.length == 2) {
findFileInJar(paths[0] + "jar!/", paths[1], contents);
} else if (paths.length > 2) {
int index = basePath.lastIndexOf("jar!/") + "jar".length();
String lastJarPath = basePath.substring(0, index);
String packagepath = basePath.substring(index + "!/".length(), basePath.length());
findFileInJarWithinJar(lastJarPath, packagepath, contents);
}
} else {
getFromFile(basePath, contents, fileSuffix, handler);
}
}
}
}
private static void loadPropertiesFromJarFile(URL url,Map<String, List<String>> allFileMap) throws UnsupportedEncodingException, IOException {
System.out.println(">>loadPropertiesFromJarFile,origin:" + url.toString());
String jarFilePath = url.getFile();
if(jarFilePath.contains("war!")){
jarFilePath = StringUtils.splitByWholeSeparator(jarFilePath, "war!")[0] + "war";
}else if(jarFilePath.contains("jar!")){
jarFilePath = StringUtils.splitByWholeSeparator(jarFilePath, "jar!")[0] + "jar";
}
jarFilePath = jarFilePath.substring("file:".length());
jarFilePath = java.net.URLDecoder.decode(jarFilePath, "UTF-8");
System.out.println(">>loadPropertiesFromJarFile,real:" + jarFilePath);
JarFile jarFile = new JarFile(jarFilePath);
String fileExt = null;
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if(entry.getName().endsWith(".properties") || entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml")){
if(entry.getName().contains("i18n"))continue;
if(entry.getName().endsWith("pom.properties"))continue;
fileExt = entry.getName().substring(entry.getName().lastIndexOf("."));
if(!allFileMap.containsKey(fileExt)){
allFileMap.put(fileExt, new ArrayList<>());
}
allFileMap.get(fileExt).add(entry.getName());
}
}
Set<String> fileExts = allFileMap.keySet();
for (String key : fileExts) {
parseConfigSortFiles(allFileMap.get(key), key, jarFile);
}
jarFile.close();
}
@Test
public void test_AppendTemplateWithSheet1() throws Exception {
IValueMeta vmi = new ValueMetaString( "new_row" );
ExcelOutputData data = new ExcelOutputData();
data.fieldnrs = new int[] { 0 };
RowMeta rowMetaToBeReturned = Mockito.spy( new RowMeta() );
rowMetaToBeReturned.addValueMeta( 0, vmi );
data.previousMeta = rowMetaToBeReturned;
ExcelOutput excelOutput =
Mockito.spy( new ExcelOutput( helper.transformMeta, data, 0, helper.pipelineMeta, helper.pipeline ) );
excelOutput.first = false;
Object[] row = { new Date() };
doReturn( row ).when( excelOutput ).getRow();
doReturn( rowMetaToBeReturned ).when( excelOutput ).getInputRowMeta();
String excelFileFullPath = buildFilePath();
File excelFile = new File( excelFileFullPath );
excelFile.deleteOnExit();
URL excelTemplateResource = this.getClass().getResource( "excel-template-withSheet1.xls" );
String templateFullPath = excelTemplateResource.getFile();
ExcelOutputMeta meta = createTransformMeta( excelFileFullPath, templateFullPath, true );
excelOutput.init();
excelOutput.init();
excelOutput.dispose();
excelOutput.init();
excelOutput.init();
excelOutput.dispose();
excelOutput.init();
excelOutput.init();
excelOutput.dispose();
excelOutput.init();
excelOutput.init();
excelOutput.dispose();
Workbook workbook = Workbook.getWorkbook( excelFile );
Assert.assertEquals( 3, workbook.getSheets().length );
// The existing sheets should be intact
Assert.assertEquals( 4, workbook.getSheet( "SheetA" ).getRows() );
Assert.assertEquals( 5, workbook.getSheet( "SheetB" ).getRows() );
// There're already 4 rows
Assert.assertEquals( 8, workbook.getSheet( CREATED_SHEET_NAME ).getRows() );
}
@Test
public void testGroovyExpressionTransformer()
throws IOException {
URL resource = AbstractRecordExtractorTest.class.getClassLoader()
.getResource("data/expression_transformer/groovy_expression_transformer.json");
File schemaFile = new File(resource.getFile());
Schema pinotSchema = Schema.fromFile(schemaFile);
ExpressionTransformer expressionTransformer = new ExpressionTransformer(pinotSchema);
DataTypeTransformer dataTypeTransformer = new DataTypeTransformer(pinotSchema);
// test functions from schema
GenericRow genericRow = new GenericRow();
genericRow.putValue("user_id", 1L);
genericRow.putValue("firstName", "John");
genericRow.putValue("lastName", "Denver");
genericRow.putValue("bids", Arrays.asList(10, 20));
HashMap<String, String> map1 = new HashMap<>(); // keys in Map from avro are always in STRING
map1.put("30", "foo");
map1.put("200", "bar");
genericRow.putValue("map1", map1);
HashMap<String, Integer> map2 = new HashMap<>();
map2.put("k1", 10);
map2.put("k2", 20);
genericRow.putValue("map2", map2);
genericRow.putValue("cost", 1000.0);
genericRow.putValue("timestamp", 1574000000000L);
// expression transformer
expressionTransformer.transform(genericRow);
// extract userId
Assert.assertEquals(genericRow.getValue("userId"), 1L);
// concat fullName
Assert.assertEquals(genericRow.getValue("fullName"), "John Denver");
Assert.assertTrue(((List) genericRow.getValue("bids")).containsAll(Arrays.asList(10, 20)));
// find max bid from bids
Assert.assertEquals(genericRow.getValue("maxBid"), 20);
// Backward compatible way to support MAP - __KEYS indicates keys of map1
ArrayList map1__keys = (ArrayList) genericRow.getValue("map1__KEYS");
Assert.assertEquals(map1__keys.get(0), "200");
Assert.assertEquals(map1__keys.get(1), "30");
// Backward compatible way to support MAP - __VALUES indicates values of map1
ArrayList map1__values = (ArrayList) genericRow.getValue("map1__VALUES");
Assert.assertEquals(map1__values.get(0), "bar");
Assert.assertEquals(map1__values.get(1), "foo");
// handle Map through transform functions
ArrayList map2__keys = (ArrayList) genericRow.getValue("map2_keys");
Assert.assertEquals(map2__keys.get(0), "k1");
Assert.assertEquals(map2__keys.get(1), "k2");
ArrayList map2__values = (ArrayList) genericRow.getValue("map2_values");
Assert.assertEquals(map2__values.get(0), 10);
Assert.assertEquals(map2__values.get(1), 20);
Assert.assertEquals(genericRow.getValue("cost"), 1000.0);
// calculate hoursSinceEpoch
Assert.assertEquals(genericRow.getValue("hoursSinceEpoch").toString(), "437222.2222222222");
// data type transformer
dataTypeTransformer.transform(genericRow);
Assert.assertEquals(genericRow.getValue("userId"), 1L);
Assert.assertEquals(genericRow.getValue("fullName"), "John Denver");
Assert.assertEquals(((Integer[]) genericRow.getValue("bids")), new Integer[]{10, 20});
Assert.assertEquals(genericRow.getValue("maxBid"), 20);
Integer[] map1Keys = (Integer[]) genericRow.getValue("map1__KEYS");
Assert.assertEquals(map1Keys[0].intValue(), 200);
Assert.assertEquals(map1Keys[1].intValue(), 30);
// Convert to INT array
Object[] map1Values = (Object[]) genericRow.getValue("map1__VALUES");
Assert.assertEquals(map1Values[0], "bar");
Assert.assertEquals(map1Values[1], "foo");
// handle Map through transform functions
Object[] map2Keys = (Object[]) genericRow.getValue("map2_keys");
Assert.assertEquals(map2Keys[0], "k1");
Assert.assertEquals(map2Keys[1], "k2");
Object[] map2Values = (Object[]) genericRow.getValue("map2_values");
Assert.assertEquals(map2Values[0], 10);
Assert.assertEquals(map2Values[1], 20);
Assert.assertEquals(genericRow.getValue("cost"), 1000.0);
// convert to LONG
Assert.assertEquals(genericRow.getValue("hoursSinceEpoch"), 437222L);
}
/**
* 获取文件夹下所有的国际化文件名
*
* @param folderName 文件名
* @return
* @throws IOException
*/
private String[] getAllBaseNames(final String folderName) throws IOException {
URL url = Thread.currentThread().getContextClassLoader()
.getResource(folderName);
if (null == url) {
throw new RuntimeException("无法获取资源文件路径");
}
List<String> baseNames = new ArrayList<>();
if (url.getProtocol().equalsIgnoreCase("file")) {
// 文件夹形式,用File获取资源路径
File file = new File(url.getFile());
if (file.exists() && file.isDirectory()) {
baseNames = Files.walk(file.toPath())
.filter(path -> path.toFile().isFile())
.map(Path::toString)
.map(path -> path.substring(path.indexOf(folderName)))
.map(this::getI18FileName)
.distinct()
.collect(Collectors.toList());
} else {
logger.error("指定的baseFile不存在或者不是文件夹");
}
} else if (url.getProtocol().equalsIgnoreCase("jar")) {
// jar包形式,用JarEntry获取资源路径
String jarPath = url.getFile().substring(url.getFile().indexOf(":") + 2, url.getFile().indexOf("!"));
JarFile jarFile = new JarFile(new File(jarPath));
List<String> baseJars = jarFile.stream()
.map(ZipEntry::toString)
.filter(jar -> jar.endsWith(folderName + "/")).collect(Collectors.toList());
if (baseJars.isEmpty()) {
logger.info("不存在{}资源文件夹", folderName);
return new String[0];
}
baseNames = jarFile.stream().map(ZipEntry::toString)
.filter(jar -> baseJars.stream().anyMatch(jar::startsWith))
.filter(jar -> jar.endsWith(".properties"))
.map(jar -> jar.substring(jar.indexOf(folderName)))
.map(this::getI18FileName)
.distinct()
.collect(Collectors.toList());
}
return baseNames.toArray(new String[0]);
}
/**
* Create test table and pipe
*/
@Before
public void beforeAll() throws Exception
{
//get test file path
URL resource = SimpleIngestIT.class.getResource(TEST_FILE_NAME);
testFilePath = resource.getFile();
//create stage, pipe, and table
Random rand = new Random();
Long num = Math.abs(rand.nextLong());
tableName = "ingest_sdk_test_table_" + num;
pipeName = "ingest_sdk_test_pipe_" + num;
stageName = "ingest_sdk_test_stage_" + num;
TestUtils.executeQuery(
"create or replace table " + tableName + " (str string, num int)"
);
TestUtils.executeQuery(
"create or replace stage " + stageName
);
TestUtils.executeQuery(
"create or replace pipe " + pipeName + " as copy into " + tableName +
" from @" + stageName
);
}