aboutsummaryrefslogtreecommitdiffstats
path: root/config-model-api/src/main/java/com/yahoo/config/application/api/ApplicationFile.java
blob: 97336b2bca0545ed174aa57924844c6187f35a81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application.api;

import com.yahoo.path.Path;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * An application file represents a file within an application package. This class can be used to traverse the entire
 * application package file structure, as well as read and write files to it, and create directories.
 *
 * @author Ulf Lilleengen
 */
public abstract class ApplicationFile implements Comparable<ApplicationFile> {

    private static final String metaDir = ".meta";
    public static final String ContentStatusNew = "new";
    public static final String ContentStatusChanged = "changed";
    public static final String ContentStatusDeleted = "deleted";
    protected final Path path;
    private static final PathFilter defaultFilter = path1 -> true;

    protected ApplicationFile(Path path) {
        this.path = path;
    }

    /**
     * Check whether or not this file is a directory.
     *
     * @return true if it is, false if not.
     */
    public abstract boolean isDirectory();

    /**
     * Test whether or not this file exists.
     *
     * @return true if it exists, false if not.
     */
    public abstract boolean exists();

    /**
     * Create a {@link Reader} for the contents of this file.
     *
     * @return A {@link Reader} that should be closed after use.
     * @throws FileNotFoundException if the file is not found.
     */
    public abstract Reader createReader() throws FileNotFoundException;


    /**
     * Create an {@link InputStream} for the contents of this file.
     *
     * @return An {@link InputStream} that should be closed after use.
     * @throws FileNotFoundException if the file is not found.
     */
    public abstract InputStream createInputStream() throws FileNotFoundException;

    /**
     * Create a directory at the path represented by this file. Parent directories will
     * be automatically created.
     *
     * @return this
     * @throws IllegalArgumentException if the directory already exists.
     */
    public abstract ApplicationFile createDirectory();

    /**
     * Write the contents from this reader to this file. Any existing content will be overwritten!
     *
     * @param input A reader pointing to the content that should be written.
     * @return this
     */
    public abstract ApplicationFile writeFile(Reader input);

    /**
     * Appends the given string to this text file.
     *
     * @return this
     */
    public abstract ApplicationFile appendFile(String value);

    /**
     * List the files under this directory. If this is file, an empty list is returned.
     * Only immediate files/subdirectories are returned.
     *
     * @return a list of files in this directory.
     */
    public List<ApplicationFile> listFiles() {
        return listFiles(defaultFilter);
    }

    /**
     * List the files under this directory. If this is file, an empty list is returned.
     * Only immediate files/subdirectories are returned.
     *
     * @param filter A filter functor for filtering path names
     * @return a list of files in this directory.
     */
    public abstract List<ApplicationFile> listFiles(PathFilter filter);

    /**
     * List the files in this directory, optionally list files for subdirectories recursively as well.
     *
     * @param recurse Set to true if all files in the directory tree should be returned.
     * @return a list of files in this directory.
     */
    public List<ApplicationFile> listFiles(boolean recurse) {
        List<ApplicationFile> ret = new ArrayList<>();
        List<ApplicationFile> files = listFiles();
        ret.addAll(files);
        if (recurse) {
            for (ApplicationFile file : files) {
                if (file.isDirectory()) {
                    ret.addAll(file.listFiles(recurse));
                }
            }
        }
        return ret;
    }

    /**
     * Delete the file pointed to by this. If it is a non-empty directory, the operation will throw.
     *
     * @return this.
     * @throws RuntimeException if the file is a directory and not empty.
     */
    public abstract ApplicationFile delete();

    /**
     * Get the path that this file represents.
     *
     * @return a Path
     */
    public Path getPath() {
        return path;
    }

    @Override
    public String toString() {
        return path.toString();
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof ApplicationFile) {
            return path.equals(((ApplicationFile) other).path);
        }
        return false;
    }

    protected Path getMetaPath() {
        if (path.toString().equals("")) {
            return Path.fromString(metaDir).append(".root");
        } else {
            return path.getParentPath().append(metaDir).append(path.getName());
        }
    }

    public abstract MetaData getMetaData();

    public abstract long getSize();

    public static class MetaData {

        public String status = "unknown";
        public String md5 = "";

        public MetaData() {  }

        public MetaData(String status, String md5) {
            this.status = status;
            this.md5= md5;
        }

        public String getStatus() {
            return status;
        }

        public String getMd5() {
            return md5;
        }
    }

    public interface PathFilter {
        boolean accept(Path path);
    }

}