1 package org.lsst.ccs.bootstrap.resources;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.ListIterator;
11 import java.util.Properties;
12 import java.util.Set;
13 import java.util.StringTokenizer;
14 import org.lsst.ccs.bootstrap.Bootstrap;
15 import org.lsst.ccs.bootstrap.BootstrapUtils;
16
17
18
19
20
21
22 public class ResourcesUtils {
23
24 private static final String FILE_SEPARATOR = System.getProperty("file.separator");
25
26
27
28
29
30
31
32
33
34
35
36 public static List<String> getResourcesInResourcesTreeByExtension(ResourcesTree tree, String extension, int depth) {
37 List<String> resources = new ArrayList();
38 for (ResourceDirectory dir : tree.getResourceDirectoryList()) {
39 for (String resource : dir.getResources()) {
40 if (extension != null && !resource.endsWith("." + extension)) {
41 continue;
42 }
43
44 if (depth > -1) {
45 StringTokenizer t = new StringTokenizer(resource, FILE_SEPARATOR);
46 if (t.countTokens() > depth + 1) {
47 continue;
48 }
49 }
50
51 if (!resources.contains(resource)) {
52 resources.add(resource);
53 }
54 }
55 }
56 return resources;
57 }
58
59
60
61
62
63
64
65 public static List<String> getAllResourcesInResourcesTree(ResourcesTree tree) {
66 return getResourcesInResourcesTreeByExtension(tree, null, -1);
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 public static Properties getMergedPropertyFile(ResourcesTree tree, String fileName) {
81 return getMergedPropertyFile(tree, fileName, false);
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public static Properties getMergedPropertyFile(ResourcesTree tree, String fileName, boolean useSystem) {
98 return getMergedProperties(tree, new String[]{fileName}, useSystem, null);
99
100 }
101
102 public static Properties getMergedProperties(ResourcesTree tree, String[] properties, boolean useSystem, ResourcesTreeProperties parentProps) {
103
104
105 ResourcesTreeProperties props = parentProps;
106 if (useSystem) {
107 if (Bootstrap.verbose() && !Bootstrap.isQuiet()) {
108 System.out.println("*** Adding System Properties to the properties chain");
109 }
110 props = new ResourcesTreeProperties("SystemProperties", null, props);
111 props.putAll(System.getProperties());
112 }
113
114
115 try {
116 InputStream defaultsIn = Bootstrap.getLoaderClass().getResourceAsStream("/ccsDefaults.properties");
117 if (defaultsIn != null) {
118 if (Bootstrap.verbose() && !Bootstrap.isQuiet()) {
119 System.out.println("*** Adding Properties " + BootstrapUtils.getDistributionResourcesDirectory() + "ccsDefaults.properties to properties chain");
120 }
121 Properties parent = props;
122 props = new ResourcesTreeProperties("ccsDefaults", BootstrapUtils.getDistributionResourcesDirectory(), parent);
123 props.load(defaultsIn);
124 String applicationName = Bootstrap.getBootstrapApplication();
125 props.put("org.lsst.ccs.application.name", applicationName == null ? "" : applicationName);
126 }
127 } catch (IOException ioe) {
128 throw new RuntimeException(ioe);
129 }
130
131 List<ResourceDirectory> resourceList = tree.getResourceDirectoryList();
132 ListIterator<ResourceDirectory> reverseIterator = resourceList.listIterator(resourceList.size());
133 while (reverseIterator.hasPrevious()) {
134 ResourceDirectory dir = reverseIterator.previous();
135 for (int i = 0; i < properties.length; i++) {
136
137 String propertyFile = properties[i];
138 if (propertyFile != null) {
139 if (!propertyFile.endsWith(".properties")) {
140 propertyFile += ".properties";
141 }
142
143
144 String fileLocation = null;
145 if (dir.hasResource(propertyFile)) {
146 fileLocation = propertyFile;
147 }
148 if (propertyFile.contains(FILE_SEPARATOR) && fileLocation == null) {
149 propertyFile = propertyFile.substring(propertyFile.lastIndexOf(FILE_SEPARATOR) + 1);
150 if (dir.hasResource(propertyFile)) {
151 fileLocation = propertyFile;
152 }
153 }
154
155 if (fileLocation != null) {
156 FileInputStream in = null;
157 try {
158 in = new FileInputStream(new File(dir.getResouceDirectoryPath(), fileLocation));
159 Properties parent = props;
160 props = new ResourcesTreeProperties(fileLocation, dir.getResouceDirectoryPath(), parent);
161 props.load(in);
162 if (Bootstrap.verbose() && !Bootstrap.isQuiet()) {
163 System.out.println("*** Adding Properties " + dir.getResouceDirectoryPath() + fileLocation + " to properties chain");
164 }
165 } catch (IOException ioe) {
166 throw new RuntimeException(ioe);
167 } finally {
168 if (in != null) {
169 try {
170 in.close();
171 } catch (IOException e) {
172 throw new RuntimeException(e);
173 }
174 }
175 }
176 }
177 }
178 }
179 }
180 return props;
181 }
182
183 public static void printProperties(Properties props) {
184 System.out.println("*****************");
185 if (props instanceof ResourcesTreeProperties) {
186 ResourcesTreeProperties p = (ResourcesTreeProperties) props;
187 String output = "Properties from " + p.getPropertyFileName();
188 if (p.getResourceDirectory() != null) {
189 output += " in resource directory " + p.getResourceDirectory();
190 }
191 System.out.println(output);
192 } else {
193 System.out.println("External System properties");
194 }
195 for (Object key : props.keySet()) {
196 System.out.println("\t" + key + " = " + props.getProperty((String) key));
197 }
198 if (props instanceof ResourcesTreeProperties) {
199 ResourcesTreeProperties p = (ResourcesTreeProperties) props;
200 if (p.hasParent()) {
201 printProperties(p.getParent());
202 }
203 }
204 }
205
206 public static void loadKeySetForProperties(Properties props, Set<Object> set) {
207 Set<Object> propsKey = props.keySet();
208 for (Object obj : propsKey) {
209 if (!set.contains(obj)) {
210 set.add(obj);
211 }
212 }
213 if (props instanceof ResourcesTreeProperties && ((ResourcesTreeProperties) props).getParent() != null) {
214 loadKeySetForProperties(((ResourcesTreeProperties) props).getParent(), set);
215 }
216 }
217
218 public static Properties getFlatPropertiesObject(Properties props) {
219 Properties newProps = new Properties();
220 Set<Object> keys = BootstrapResourceUtils.getAllKeysInProperties(props);
221 for (Object key : keys) {
222 newProps.put(key, props.getProperty((String) key));
223 }
224 return newProps;
225 }
226
227 public static InputStream getResourceFromResourceTree(ResourcesTree tree, String resourceName) throws FileNotFoundException {
228 List<ResourceDirectory> resourceList = tree.getResourceDirectoryList();
229
230 for (ResourceDirectory dir : resourceList) {
231 if (dir.hasResource(resourceName)) {
232 if (Bootstrap.verbose() && !Bootstrap.isQuiet()) {
233 System.out.println("*** Found resource " + dir.getResouceDirectoryPath() + resourceName);
234 }
235 return new FileInputStream(dir.getResouceDirectoryPath() + resourceName);
236 } else {
237 if (resourceName.contains(FILE_SEPARATOR)) {
238 resourceName = resourceName.substring(resourceName.lastIndexOf(FILE_SEPARATOR) + 1);
239 if (dir.hasResource(resourceName)) {
240 if (Bootstrap.verbose() && !Bootstrap.isQuiet()) {
241 System.out.println("*** Found resource " + dir.getResouceDirectoryPath() + resourceName);
242 }
243 return new FileInputStream(dir.getResouceDirectoryPath() + resourceName);
244 }
245 }
246 }
247 }
248
249 return null;
250 }
251
252 }