summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/cmpc/app/ConfigFactory.java
blob: f1c46855bdb5fa49c55b65b27ef0a487597434bd (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
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
/**
 * ======================== legal notice ======================
 * 
 * File:      ConfigFactory.java
 * Created:   
 * Author:    <a href="mailto:geronimo013@gmx.de">Geronimo</a>
 * Project:   cmpc - a java frontend (client) part of compound media player
 *                   uses external players to play the media
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any workstation
 * without the need to export or mount shares. cmps is an easy to use backend
 * with a (ready to use) HTML-interface. Additionally the backend supports
 * authentication via HTTP-digest authorization.
 * cmpc is a client with vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved!
 * published under Creative Commons by-sa
 * For details see http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
package de.schwarzrot.cmpc.app;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.schwarzrot.cmpc.domain.Config;
import de.schwarzrot.cmpc.domain.Media;
import de.schwarzrot.cmpc.domain.MediaServer;
import de.schwarzrot.cmpc.domain.PlayerDefinition;


public class ConfigFactory {
    protected static final Pattern SETUP_MASK = Pattern.compile("^\\s*(\\S+)\\s*=\\s*(.+)$");


    public ConfigFactory(File configDirectory) {
        baseDir = configDirectory;
    }


    public Config getConfig() {
        if (config == null) {
            config = readConfig();
        }
        return config;
    }


    protected Config readConfig() {
        File setupFile = new File(baseDir, "srclient.conf");
        BufferedReader br = null;
        Config rv = new Config();

        if (setupFile.exists()) {
            try {
                br = new BufferedReader(new FileReader(setupFile));
                String line, trimmedLine;

                while ((line = br.readLine()) != null) {
                    trimmedLine = line.trim();

                    if (trimmedLine.isEmpty() || trimmedLine.startsWith("#"))
                        continue;
                    Matcher m = SETUP_MASK.matcher(trimmedLine);

                    if (m.matches()) {
                        String key = m.group(1);
                        String value = m.group(2);

                        if (key.compareToIgnoreCase("server") == 0) {
                            String[] parts = value.split("\\s*\\:\\s*");
                            MediaServer ms = null;

                            if (parts.length > 1) {
                                ms = new MediaServer();
                                ms.setHostName(parts[0]);
                                ms.setPort(Integer.decode(parts[1]));

                                rv.addServer(ms);
                            }
                        } else if (key.compareToIgnoreCase("player") == 0) {
                            String[] parts = value.split("\\s*\\:\\s*");
                            PlayerDefinition pd = null;

                            if (parts.length > 1) {
                                pd = new PlayerDefinition();
                                String[] cmdline = parts[1].split("\\s+");

                                pd.setExecutable(cmdline[0]);

                                for (int i = 1; i < cmdline.length; ++i) {
                                    pd.addParameter(cmdline[i]);
                                }
                                if (parts[0].compareTo("*") == 0) {
                                    // default player
                                    for (Media.SupportedMediaType cmt : Media.SupportedMediaType.values())
                                        rv.putPlayer(cmt, pd);
                                } else {
                                    for (Media.SupportedMediaType cmt : Media.SupportedMediaType.values()) {
                                        if (parts[0].compareTo(cmt.name()) == 0)
                                            rv.putPlayer(cmt, pd);
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Throwable t) {
                t.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
        return rv;
    }
    private Config config;
    private File baseDir;
}