View Javadoc
1   package org.kuali.common.devops.cache;
2   
3   import static org.apache.commons.io.FileUtils.touch;
4   import static org.kuali.common.util.base.Exceptions.illegalState;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.util.List;
9   
10  import org.kuali.common.http.model.HttpRequestResult;
11  import org.kuali.common.http.model.HttpWaitResult;
12  
13  import com.fasterxml.jackson.annotation.JsonIgnore;
14  import com.fasterxml.jackson.databind.ObjectMapper;
15  import com.fasterxml.jackson.datatype.guava.GuavaModule;
16  
17  public class UrlFileCache extends PersistentCache<File, HttpWaitResult> {
18  
19  	private final ObjectMapper mapper = getObjectMapper();
20  
21  	@Override
22  	public void store(File file, HttpWaitResult result) {
23  		try {
24  			touch(file);
25  			mapper.writer().withDefaultPrettyPrinter().writeValue(file, result);
26  		} catch (IOException e) {
27  			throw illegalState(e);
28  		}
29  	}
30  
31  	@Override
32  	public HttpWaitResult load(File file) {
33  		try {
34  			return mapper.readValue(file, HttpWaitResult.class);
35  		} catch (IOException e) {
36  			throw illegalState(e);
37  		}
38  	}
39  
40  	protected ObjectMapper getObjectMapper() {
41  		ObjectMapper mapper = new ObjectMapper();
42  		mapper.registerModule(new GuavaModule());
43  		mapper.addMixInAnnotations(HttpWaitResult.class, MixIn.class);
44  		return mapper;
45  	}
46  
47  	private static final class MixIn {
48  		@JsonIgnore
49  		private List<HttpRequestResult> requestResults;
50  
51  	}
52  
53  }