View Javadoc

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.List;
9   import java.util.Properties;
10  
11  import org.apache.commons.io.FileUtils;
12  import org.apache.commons.io.IOUtils;
13  import org.apache.commons.lang.StringUtils;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  import org.springframework.core.io.DefaultResourceLoader;
17  import org.springframework.core.io.Resource;
18  import org.springframework.core.io.ResourceLoader;
19  import org.springframework.util.PropertyPlaceholderHelper;
20  
21  public class ResourceUtil {
22  
23      private static final Logger log = LoggerFactory.getLogger(ResourceUtil.class);
24      public static final String PREFIX = "${";
25      public static final String SUFFIX = "}";
26  
27      public String getProperty(Properties props, String defaultPrefix, String envPrefix, String key) {
28          String defaultValue = props.getProperty(defaultPrefix + "." + key);
29          String environmentValue = props.getProperty(envPrefix + "." + key);
30          if (!StringUtils.isBlank(environmentValue)) {
31              // Use the environment value if one is provided
32              return environmentValue;
33          } else {
34              // Otherwise fall through to the default value
35              return defaultValue;
36          }
37      }
38  
39      public String getSystemProperty(String property, String defaultValue) {
40          String s = System.getProperty(property);
41          if (StringUtils.isBlank(s)) {
42              return defaultValue;
43          } else {
44              return s.trim();
45          }
46  
47      }
48  
49      public void validate(ResourceItem item) {
50          boolean validExisting = isValidExistingResource(item);
51          if (validExisting) {
52              return;
53          }
54          createOrVerifyAsNeeded(item);
55      }
56  
57      protected void createOrVerifyAsNeeded(ResourceItem item) {
58          if (item.isLocalDirectory()) {
59              createLocalDirectory(item.getDestination());
60              setSystemProperty(item.getSystemProperty(), item.getDestination().getAbsolutePath());
61              return;
62          }
63  
64          String path = item.getDestination().getAbsolutePath();
65          if (exists(path)) {
66              log.info("Found existing resource " + path);
67              setSystemProperty(item.getSystemProperty(), path);
68              return;
69          }
70  
71          String location = item.getSourceLocation();
72          boolean sourceExists = exists(location);
73          if (!sourceExists) {
74              throw new IllegalArgumentException(location + " does not exist");
75          }
76          try {
77              log.info("Creating " + path + " from " + location);
78              if (item.isFilter()) {
79                  log.info("Filtering content with " + item.getProperties().size() + " properties");
80                  filterThenCopy(location, item.getDestination(), item.getProperties());
81              } else {
82                  copy(location, item.getDestination());
83              }
84              setSystemProperty(item.getSystemProperty(), path);
85          } catch (IOException e) {
86              throw new IllegalStateException("Unable to create " + path);
87          }
88      }
89  
90      public void setSystemProperty(String property, String value) {
91          log.info("Setting " + property + "=" + value);
92          System.setProperty(property, value);
93      }
94  
95      protected void filterThenCopy(String location, File file, Properties properties) throws IOException {
96          InputStream in = null;
97          try {
98              in = getInputStream(location);
99              String content = IOUtils.toString(in);
100             String filtered = getFilteredContent(content, properties);
101             FileUtils.writeStringToFile(file, filtered);
102         } finally {
103             IOUtils.closeQuietly(in);
104         }
105     }
106 
107     protected void copy(String location, File file) throws IOException {
108         InputStream in = null;
109         OutputStream out = null;
110         try {
111             in = getInputStream(location);
112             out = FileUtils.openOutputStream(file);
113             IOUtils.copy(in, out);
114         } finally {
115             IOUtils.closeQuietly(in);
116             IOUtils.closeQuietly(out);
117         }
118     }
119 
120     protected boolean isValidExistingResource(ResourceItem item) {
121 
122         // Extract the the value of the system property
123         String value = System.getProperty(item.getSystemProperty());
124 
125         // No system property was set for this item, we are done
126         if (StringUtils.isBlank(value)) {
127             return false;
128         }
129 
130         // Does the resource exist?
131         boolean exists = exists(value);
132 
133         boolean valid = exists;
134         if (item.isLocalDirectory()) {
135             valid = exists && isLocalDirectory(value);
136         }
137 
138         if (valid) {
139             // The resource is valid, we are done
140             return true;
141         }
142 
143         // They supplied a system property pointing to a required resource that
144         // does not exist
145         String msg = item.getSystemProperty() + "=" + value + " is required, but does not exist";
146         throw new IllegalArgumentException(msg);
147     }
148 
149     public Properties getPropertiesFromSystemProperty(String systemProperty) throws IOException {
150         String location = System.getProperty(systemProperty);
151         if (StringUtils.isBlank(location)) {
152             return new Properties();
153         }
154         boolean exists = exists(location);
155         if (!exists) {
156             log.info("Skipping property loading from '" + location + "' as it does not exist");
157             return new Properties();
158         }
159         return getProperties(location);
160     }
161 
162     public Properties getProperties(List<String> locations) throws IOException {
163         Properties props = new Properties();
164         for (String location : locations) {
165             Properties p = getProperties(location);
166             props.putAll(p);
167         }
168         return props;
169     }
170 
171     public Properties getProperties(String location) throws IOException {
172         InputStream in = null;
173         try {
174             in = getInputStream(location);
175             Properties props = new Properties();
176             log.info("Loading properties from " + location);
177             props.load(in);
178             return props;
179         } finally {
180             IOUtils.closeQuietly(in);
181         }
182     }
183 
184     /**
185      *
186      */
187     public InputStream getInputStream(String location) throws IOException {
188         File file = new File(location);
189         if (file.exists()) {
190             return new FileInputStream(file);
191         }
192         ResourceLoader loader = new DefaultResourceLoader();
193         Resource resource = loader.getResource(location);
194         return resource.getInputStream();
195     }
196 
197     public void createLocalDirectory(File dir) {
198         try {
199             FileUtils.forceMkdir(dir);
200         } catch (IOException e) {
201             throw new IllegalStateException("Unable to create " + dir.getAbsolutePath());
202         }
203     }
204 
205     public boolean isLocalDirectory(String location) {
206         File file = new File(location);
207         return file.isDirectory();
208     }
209 
210     /**
211      * Return true if <code>location</code> is a file on the local file system or points to a resource Spring's resource loading API can
212      * understand and locate. For example, <code>classpath:config.properties</code>. Return false otherwise.
213      */
214     public boolean exists(String location) {
215         File file = new File(location);
216         if (file.exists()) {
217             return true;
218         }
219         ResourceLoader loader = new DefaultResourceLoader();
220         Resource resource = loader.getResource(location);
221         return resource.exists();
222     }
223 
224     public String getFilteredContent(String s, Properties properties) {
225         PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper(PREFIX, SUFFIX);
226         String filtered = pph.replacePlaceholders(s, properties);
227         return filtered;
228     }
229 
230     public File getFile(String location) throws IOException {
231         ResourceLoader loader = new DefaultResourceLoader();
232         Resource resource = loader.getResource(location);
233         return resource.getFile();
234     }
235 
236     public void copyDirectory(String srcLoc, File dstDir) throws IOException {
237         File srcDir = getFile(srcLoc);
238         FileUtils.copyDirectory(srcDir, dstDir);
239     }
240 
241 }