2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/eplv10.html
8 package org.opendaylight.yangtools.yang.model.util.repo;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStreamWriter;
17 import java.io.StringBufferInputStream;
19 import com.google.common.base.Function;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Preconditions;
23 public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
25 private final File storageDirectory;
26 private final Function<I, String> transformationFunction;
28 public FilesystemSchemaCachingProvider(AdvancedSchemaSourceProvider<I> delegate, File directory,
29 Function<I, String> transformationFunction) {
31 this.storageDirectory = directory;
32 this.transformationFunction = transformationFunction;
36 protected synchronized Optional<InputStream> cacheSchemaSource(SourceIdentifier identifier, Optional<I> source) {
37 File schemaFile = toFile(identifier);
39 if (source.isPresent() && schemaFile.createNewFile()) {
40 try (FileOutputStream outStream = new FileOutputStream(schemaFile);
41 OutputStreamWriter writer = new OutputStreamWriter(outStream);) {
42 writer.write(transformToString(source.get()));
44 } catch (IOException e) {
48 } catch (IOException e) {
51 return transformToStream(source);
54 @SuppressWarnings("deprecation")
55 private Optional<InputStream> transformToStream(Optional<I> source) {
56 if (source.isPresent()) {
57 return Optional.<InputStream> of(new StringBufferInputStream(transformToString(source.get())));
59 return Optional.absent();
62 private String transformToString(I input) {
63 return transformationFunction.apply(input);
67 protected Optional<InputStream> getCachedSchemaSource(SourceIdentifier identifier) {
68 File inputFile = toFile(identifier);
70 if (inputFile.exists() && inputFile.canRead()) {
71 InputStream stream = new FileInputStream(inputFile);
72 return Optional.of(stream);
74 } catch (FileNotFoundException e) {
75 return Optional.absent();
77 return Optional.absent();
80 private File toFile(SourceIdentifier identifier) {
81 return new File(storageDirectory, identifier.toYangFilename());
84 private static final Function<String, String> NOOP_TRANSFORMATION = new Function<String, String>() {
86 public String apply(String input) {
91 public static FilesystemSchemaCachingProvider<String> createFromStringSourceProvider(
92 SchemaSourceProvider<String> liveProvider, File directory) {
93 Preconditions.checkNotNull(liveProvider);
94 Preconditions.checkNotNull(directory);
96 return new FilesystemSchemaCachingProvider<String>(
97 SchemaSourceProviders.toAdvancedSchemaSourceProvider(liveProvider),//