0fdf05a227dd96f9a354f17bb29788218ac62b9c
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / repo / FilesystemSchemaCachingProvider.java
1 package org.opendaylight.yangtools.yang.model.util.repo;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStreamWriter;
10 import java.io.StringBufferInputStream;
11
12 import com.google.common.base.Function;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15
16 public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
17
18     private final File storageDirectory;
19     private final Function<I, String> transformationFunction;
20
21     public FilesystemSchemaCachingProvider(SchemaSourceProvider<I> delegate, File directory,
22             Function<I, String> transformationFunction) {
23         super(delegate);
24         this.storageDirectory = directory;
25         this.transformationFunction = transformationFunction;
26     }
27
28     @Override
29     protected synchronized Optional<InputStream> cacheSchemaSource(String moduleName, Optional<String> revision, Optional<I> source) {
30         File schemaFile = toFile(moduleName, revision);
31         try {
32             if(source.isPresent() && schemaFile.createNewFile()) {
33                 try (
34                         FileOutputStream outStream = new FileOutputStream(schemaFile);
35                         OutputStreamWriter writer = new OutputStreamWriter(outStream);
36                 ) {
37                 writer.write(transformToString(source.get()));
38                 writer.flush();
39                 } catch (IOException e) {
40                     
41                 }
42             }
43         } catch (IOException e){
44             
45         }
46         return transformToStream(source);
47     }
48
49     @SuppressWarnings("deprecation")
50     private Optional<InputStream> transformToStream(Optional<I> source) {
51         if(source.isPresent()) {
52             return Optional.<InputStream>of(new StringBufferInputStream(transformToString(source.get())));
53         }
54         return Optional.absent();
55     }
56
57     private String transformToString(I input) {
58         return transformationFunction.apply(input);
59     }
60
61     @Override
62     protected Optional<InputStream> getCachedSchemaSource(String moduleName, Optional<String> revision) {
63         File inputFile = toFile(moduleName, revision);
64         try {
65             if (inputFile.exists() && inputFile.canRead()) {
66                 InputStream stream = new FileInputStream(inputFile);
67                 return Optional.of(stream);
68             }
69         } catch (FileNotFoundException e) {
70             return Optional.absent();
71         }
72         return Optional.absent();
73     }
74
75     private File toFile(String moduleName, Optional<String> revision) {
76         return new File(storageDirectory, toYangFileName(moduleName, revision));
77     }
78
79     public static final String toYangFileName(String moduleName, Optional<String> revision) {
80         StringBuilder filename = new StringBuilder(moduleName);
81         if (revision.isPresent()) {
82             filename.append("@");
83             filename.append(revision.get());
84         }
85         filename.append(".yang");
86         return filename.toString();
87     }
88
89     private static final Function<String, String> NOOP_TRANSFORMATION = new Function<String, String>() {
90         @Override
91         public String apply(String input) {
92             return input;
93         }
94     };
95
96     public static FilesystemSchemaCachingProvider<String> createFromStringSourceProvider(
97             SchemaSourceProvider<String> liveProvider, File directory) {
98         Preconditions.checkNotNull(liveProvider);
99         Preconditions.checkNotNull(directory);
100         directory.mkdirs();
101         return new FilesystemSchemaCachingProvider<String>(liveProvider, directory,NOOP_TRANSFORMATION);
102     }
103 }