forked from microsoft/semantic-kernel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbeddedResourceLoader.java
156 lines (142 loc) · 5.23 KB
/
EmbeddedResourceLoader.java
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.implementation;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class for loading resources from the classpath or filesystem.
*/
public class EmbeddedResourceLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddedResourceLoader.class);
/**
* Loads a file to a string from the classpath using getResourceAsStream
*
* @param fileName Filename to read
* @param clazz Class to use for classpath loading
* @return File content
* @throws FileNotFoundException Error in case the file doesn't exist
*/
public static String readFile(String fileName, Class<?> clazz) throws FileNotFoundException {
return readFile(fileName, clazz, ResourceLocation.CLASSPATH);
}
/**
* Loads a file to a string from the classpath, classpath root or filesystem
*
* @param fileName Filename to read
* @param clazz Class to use for classpath loading
* @param locations Locations to search for the file
* @return File content
* @throws FileNotFoundException Error in case the file doesn't exist
*/
public static String readFile(String fileName, @Nullable Class<?> clazz,
ResourceLocation... locations)
throws FileNotFoundException {
List<ResourceLocation> locationsList = Arrays.stream(locations)
.collect(Collectors.toList());
Optional<String> fileContents = locationsList.stream()
.map(
type -> {
switch (type) {
case CLASSPATH:
if (clazz == null) {
throw new SKException(
"Resource location CLASSPATH requires a class");
}
return getResourceAsStream(fileName, clazz);
case CLASSPATH_ROOT:
try (InputStream inputStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(fileName)) {
return readInputStream(fileName, inputStream);
} catch (IOException e) {
// IGNORE
}
break;
case FILESYSTEM:
return readFileFromFileSystem(fileName);
default:
}
return null;
})
.filter(Objects::nonNull)
.findFirst();
if (fileContents.isPresent()) {
return fileContents.get();
}
throw new FileNotFoundException("Could not find file " + fileName);
}
@Nullable
// visible for testing
static String getResourceAsStream(String fileName, Class<?> clazz) {
try (InputStream inputStream = clazz.getResourceAsStream(fileName)) {
return readInputStream(fileName, inputStream);
} catch (Exception e) {
// IGNORE
}
return null;
}
@Nullable
// visible for testing
static String readFileFromFileSystem(String fileName) {
File file = new File(fileName);
if (file.exists()) {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return readInputStream(fileName, inputStream);
} catch (IOException e) {
// IGNORE
}
}
return null;
}
@Nullable
// visible for testing
private static String readInputStream(String fileName, InputStream inputStream)
throws FileNotFoundException {
if (inputStream == null) {
throw new FileNotFoundException("File not found: " + fileName);
}
try (BufferedReader bf = new BufferedReader(
new InputStreamReader(inputStream, java.nio.charset.StandardCharsets.UTF_8))) {
return bf.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
// IGNORE
LOGGER.trace(
MessageFormat.format(SemanticKernelResources.getString("failed.to.load.file.0"),
fileName),
e);
}
return null;
}
/**
* Enum for specifying the location of the resource
*/
public enum ResourceLocation {
/**
* Load from the classpath
*/
CLASSPATH,
/**
* Load from the filesystem
*/
FILESYSTEM,
/**
* Load from the classpath root
*/
CLASSPATH_ROOT
}
}