|
8 | 8 | import org.springframework.stereotype.Service;
|
9 | 9 |
|
10 | 10 | import java.io.BufferedReader;
|
| 11 | +import java.io.File; |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.FileNotFoundException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
11 | 16 | import java.io.IOException;
|
12 | 17 | import java.nio.file.Files;
|
13 | 18 | import java.nio.file.Paths;
|
@@ -291,18 +296,66 @@ private Map<String,String> cloneProperties() {
|
291 | 296 | );
|
292 | 297 | }
|
293 | 298 |
|
294 |
| - private String readFile(String fileName) { |
295 |
| - if (fileName != null && !fileName.isEmpty()) { |
296 |
| - try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) { |
297 |
| - return br.lines().map(String::trim).collect(Collectors.joining("")); |
298 |
| - } catch (Exception e) { |
299 |
| - log.error("Error reading frontend config file: {}", e.getMessage()); |
300 |
| - return null; |
| 299 | + /** |
| 300 | + * Find the file, either on the file system or in a .jar, and return as an InputStream. |
| 301 | + * @propertiesFileName: the file path |
| 302 | + * @return: a valid InputStream (not null), otherwise throws FileNotFoundException |
| 303 | + * TECH: file system locations have precedence over classpath |
| 304 | + * REF: based on getResourceStream() in WebServletContextListener.java |
| 305 | + */ |
| 306 | + private InputStream locateFile(String filePath) throws FileNotFoundException { |
| 307 | + // try absolute or relative to working directory |
| 308 | + File file = new File(filePath); |
| 309 | + if (file.exists()) { |
| 310 | + // throws if is a directory or cannot be opened |
| 311 | + log.info("Found frontend config file: {}", file.getAbsolutePath()); |
| 312 | + return new FileInputStream(file); |
| 313 | + } |
| 314 | + |
| 315 | + // try relative to PORTAL_HOME |
| 316 | + String home = System.getenv("PORTAL_HOME"); |
| 317 | + if (home != null) { |
| 318 | + file = new File(Paths.get(home, filePath).toString()); |
| 319 | + if (file.exists()) { |
| 320 | + log.info("Found frontend config file: {}", file.getAbsolutePath()); |
| 321 | + return new FileInputStream(file); |
301 | 322 | }
|
| 323 | + } |
| 324 | + |
| 325 | + // try resource (e.g. app.jar) |
| 326 | + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath); |
| 327 | + if (inputStream != null) { |
| 328 | + log.info("Found frontend config resource: {}", filePath); |
| 329 | + return inputStream; |
| 330 | + } else { |
| 331 | + throw new FileNotFoundException("File not found in system or classpath: " + filePath); |
302 | 332 | }
|
303 |
| - return null; |
304 | 333 | }
|
305 |
| - |
| 334 | + |
| 335 | + /** |
| 336 | + * Read the file, either on the file system or in a .jar, and return the content as a single-line string. |
| 337 | + * @propertiesFileName: the file path |
| 338 | + */ |
| 339 | + private String readFile(String propertiesFileName) { |
| 340 | + if (propertiesFileName == null || propertiesFileName.isEmpty()) { |
| 341 | + return null; |
| 342 | + } |
| 343 | + |
| 344 | + // strip off classpath prefix and always check all locations (ClassLoader and file system) |
| 345 | + String filePath = propertiesFileName.startsWith("classpath:") |
| 346 | + ? propertiesFileName.substring("classpath:".length()) |
| 347 | + : propertiesFileName; |
| 348 | + |
| 349 | + try { |
| 350 | + InputStream inputStream = locateFile(filePath); |
| 351 | + BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); |
| 352 | + return br.lines().map(String::trim).collect(Collectors.joining("")); |
| 353 | + } catch (Exception e) { |
| 354 | + log.error("Error reading frontend config file: {}", e.getMessage()); |
| 355 | + return null; |
| 356 | + } |
| 357 | + } |
| 358 | + |
306 | 359 | public String getFrontendUrl(String propertyValue) {
|
307 | 360 | String frontendUrlRuntime = env.getProperty("frontend.url.runtime", "");
|
308 | 361 | if (frontendUrlRuntime.length() > 0) {
|
|
0 commit comments