aboutsummaryrefslogtreecommitdiff
path: root/subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/Util.java
blob: 31a1be71ae833198f9f3b8b81e78cb3c4dce3370 (plain)
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
package net.sourceforge.subsonic.backend;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.Reader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2010 (C) Sindre Mehus
 */

/**
 * @author Sindre Mehus
 */
public class Util {

    private static final File BACKEND_HOME = new File("/var/subsonic-backend");
    private static final Logger LOG = Logger.getLogger(Util.class);

    private Util() {
    }

    public static synchronized File getBackendHome() {
        if (!BACKEND_HOME.exists() || !BACKEND_HOME.isDirectory()) {
            boolean success = BACKEND_HOME.mkdirs();
            if (!success) {
                String message = "The directory " + BACKEND_HOME + " does not exist. Please create it and make it writable.";
                LOG.error(message);
                throw new RuntimeException(message);
            }
        }
        return BACKEND_HOME;
    }

    public static String getPassword(String filename) throws IOException {
        File pwdFile = new File(getBackendHome(), filename);
        Reader reader = new FileReader(pwdFile);
        try {
            return StringUtils.trimToNull(IOUtils.toString(reader));
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }
}