1 package org.kuali.ole.utility;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.util.Properties;
9
10 import org.apache.commons.io.FileUtils;
11 import org.apache.commons.io.IOUtils;
12 import org.apache.commons.lang.StringUtils;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.core.io.DefaultResourceLoader;
16 import org.springframework.core.io.Resource;
17 import org.springframework.core.io.ResourceLoader;
18
19 public class ResourceUtil {
20
21 private static final Logger log = LoggerFactory.getLogger(ResourceUtil.class);
22
23 public String getProperty(Properties props, String defaultPrefix, String envPrefix, String key) {
24 String defaultValue = props.getProperty(defaultPrefix + "." + key);
25 String environmentValue = props.getProperty(envPrefix + "." + key);
26 if (!StringUtils.isBlank(environmentValue)) {
27
28 return environmentValue;
29 } else {
30
31 return defaultValue;
32 }
33 }
34
35 public String getSystemProperty(String property, String defaultValue) {
36 String s = System.getProperty(property);
37 if (StringUtils.isBlank(s)) {
38 return defaultValue;
39 } else {
40 return s.trim();
41 }
42
43 }
44
45 public void validate(ResourceItem item) {
46 boolean validExisting = isValidExistingResource(item);
47 if (validExisting) {
48 return;
49 }
50 createOrVerifyAsNeeded(item);
51 }
52
53 protected void createOrVerifyAsNeeded(ResourceItem item) {
54 if (item.isLocalDirectory()) {
55 createLocalDirectory(item.getDestination());
56 setSystemProperty(item.getSystemProperty(), item.getDestination().getAbsolutePath());
57 return;
58 }
59
60 String path = item.getDestination().getAbsolutePath();
61 if (exists(path)) {
62 log.info("Found existing resource " + path);
63 setSystemProperty(item.getSystemProperty(), path);
64 return;
65 }
66
67 String location = item.getSourceLocation();
68 boolean sourceExists = exists(location);
69 if (!sourceExists) {
70 throw new IllegalArgumentException(location + " does not exist");
71 }
72 try {
73 log.info("Creating " + path + " from " + location);
74 copy(location, item.getDestination());
75 setSystemProperty(item.getSystemProperty(), path);
76 } catch (IOException e) {
77 throw new IllegalStateException("Unable to create " + path);
78 }
79 }
80
81 public void setSystemProperty(String property, String value) {
82 log.info("Setting " + property + "=" + value);
83 System.setProperty(property, value);
84 }
85
86 protected void copy(String location, File file) throws IOException {
87 InputStream in = null;
88 OutputStream out = null;
89 try {
90 in = getInputStream(location);
91 out = FileUtils.openOutputStream(file);
92 IOUtils.copy(in, out);
93 } finally {
94 IOUtils.closeQuietly(in);
95 IOUtils.closeQuietly(out);
96 }
97 }
98
99 protected boolean isValidExistingResource(ResourceItem item) {
100
101
102 String value = System.getProperty(item.getSystemProperty());
103
104
105 if (StringUtils.isBlank(value)) {
106 return false;
107 }
108
109
110 boolean exists = exists(value);
111
112 boolean valid = exists;
113 if (item.isLocalDirectory()) {
114 valid = exists && isLocalDirectory(value);
115 }
116
117 if (valid) {
118
119 return true;
120 }
121
122
123 String msg = item.getSystemProperty() + "=" + value + " is required, but does not exist";
124 throw new IllegalArgumentException(msg);
125 }
126
127 public Properties getPropertiesFromSystemProperty(String systemProperty) throws IOException {
128 String location = System.getProperty(systemProperty);
129 if (StringUtils.isBlank(location)) {
130 return new Properties();
131 }
132 boolean exists = exists(location);
133 if (!exists) {
134 log.info("Skipping property loading from '" + location + "' as it does not exist");
135 return new Properties();
136 }
137 return getProperties(location);
138 }
139
140 public Properties getProperties(String location) throws IOException {
141 InputStream in = null;
142 try {
143 in = getInputStream(location);
144 Properties props = new Properties();
145 log.info("Loading properties from " + location);
146 props.load(in);
147 return props;
148 } finally {
149 IOUtils.closeQuietly(in);
150 }
151 }
152
153
154
155
156 public InputStream getInputStream(String location) throws IOException {
157 File file = new File(location);
158 if (file.exists()) {
159 return new FileInputStream(file);
160 }
161 ResourceLoader loader = new DefaultResourceLoader();
162 Resource resource = loader.getResource(location);
163 return resource.getInputStream();
164 }
165
166 public void createLocalDirectory(File dir) {
167 try {
168 FileUtils.forceMkdir(dir);
169 } catch (IOException e) {
170 throw new IllegalArgumentException("Unable to create " + dir.getAbsolutePath());
171 }
172 }
173
174 public boolean isLocalDirectory(String location) {
175 File file = new File(location);
176 return file.isDirectory();
177 }
178
179
180
181
182
183
184 public boolean exists(String location) {
185 File file = new File(location);
186 if (file.exists()) {
187 return true;
188 }
189 ResourceLoader loader = new DefaultResourceLoader();
190 Resource resource = loader.getResource(location);
191 return resource.exists();
192 }
193
194 public File getFile(String location) {
195 try {
196 ResourceLoader loader = new DefaultResourceLoader();
197 Resource resource = loader.getResource(location);
198 return resource.getFile();
199 } catch (IOException e) {
200 throw new IllegalArgumentException(e);
201 }
202 }
203
204 public void copyDirectory(String srcLoc, File dstDir) {
205 try {
206 File srcDir = getFile(srcLoc);
207 FileUtils.copyDirectory(srcDir, dstDir);
208 } catch (IOException e) {
209 throw new IllegalArgumentException(e);
210 }
211 }
212
213 }