1 package org.lsst.ccs.bootstrap.resources;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7
8
9
10
11
12
13 public class ResourceDirectory {
14
15 private File resourceDirectoryFile = null;
16 private List<String> resources = new ArrayList<>();
17 private String fileSeparator = System.getProperty("file.separator");
18 private boolean isDistributionDir = false;
19
20 ResourceDirectory(String resourceDirectoryPath, boolean isDistributionDir) {
21 resourceDirectoryFile = new File(resourceDirectoryPath);
22 this.isDistributionDir = isDistributionDir;
23 loadResources(resourceDirectoryFile, fileSeparator);
24 }
25
26
27 private void loadResources(File parent, String relPath) {
28 if (parent.exists() && parent.isDirectory()) {
29 String[] listOfResources = parent.list();
30 for( String resource : listOfResources ) {
31
32 File f = new File(parent,resource);
33 if ( f.isDirectory() ) {
34 loadResources(f,relPath+resource+fileSeparator);
35 } else {
36 String resourceName = relPath+resource;
37 if ( resourceName.startsWith(fileSeparator) ) {
38 resourceName = resourceName.substring(1);
39 }
40 resources.add(resourceName);
41 }
42 }
43 } else {
44 throw new IllegalArgumentException("The provided path: "+parent.getAbsolutePath()+" does not exist");
45 }
46 }
47
48 public boolean isDistributionDir() {
49 return isDistributionDir;
50 }
51
52 public boolean hasResource(String resourceName) {
53 return resources.contains(resourceName);
54 }
55
56 public String getResouceDirectoryPath() {
57 String resourceDirPath = resourceDirectoryFile.getAbsolutePath();
58 if ( ! resourceDirPath.endsWith(fileSeparator) ) {
59 resourceDirPath += fileSeparator;
60 }
61 return resourceDirPath;
62 }
63
64 public List<String> getResources() {
65 return resources;
66 }
67
68
69 }