aboutsummaryrefslogtreecommitdiff
path: root/subsonic-booter/src/main/java/net/sourceforge/subsonic/booter/agent/SettingsPanel.java
blob: c225410f8982507cb849586a35f10583c2b82dcf (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package net.sourceforge.subsonic.booter.agent;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.FormLayout;
import net.sourceforge.subsonic.booter.deployer.DeploymentStatus;
import net.sourceforge.subsonic.booter.deployer.SubsonicDeployer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Panel displaying the settings of the Subsonic service.
 *
 * @author Sindre Mehus
 */
public class SettingsPanel extends JPanel implements SubsonicListener {

    private static final Format INTEGER_FORMAT = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.UK));

    private final SubsonicAgent subsonicAgent;
    private JFormattedTextField portTextField;
    private JCheckBox httpsPortCheckBox;
    private JFormattedTextField httpsPortTextField;
    private JComboBox contextPathComboBox;
    private JFormattedTextField memoryTextField;
    private JButton defaultButton;
    private JButton saveButton;
    public SettingsPanel(SubsonicAgent subsonicAgent) {
        this.subsonicAgent = subsonicAgent;
        createComponents();
        configureComponents();
        layoutComponents();
        addBehaviour();
        readValues();
        subsonicAgent.addListener(this);
    }

    public void readValues() {
        portTextField.setValue(getPortFromOptionsFile());
        memoryTextField.setValue(getMemoryLimitFromOptionsFile());
        contextPathComboBox.setSelectedItem(getContextPathFromOptionsFile());
        int httpsPort = getHttpsPortFromOptionsFile();
        boolean httpsEnabled = httpsPort != 0;
        httpsPortTextField.setValue(httpsEnabled ? httpsPort : 4443);
        httpsPortTextField.setEnabled(httpsEnabled);
        httpsPortCheckBox.setSelected(httpsEnabled);
    }

    private int getHttpsPortFromOptionsFile() {
        try {
            String s = grep("-Dsubsonic.httpsPort=(\\d+)");
            return Integer.parseInt(s);
        } catch (Exception x) {
            x.printStackTrace();
            return SubsonicDeployer.DEFAULT_HTTPS_PORT;
        }
    }

    private int getPortFromOptionsFile() {
        try {
            String s = grep("-Dsubsonic.port=(\\d+)");
            return Integer.parseInt(s);
        } catch (Exception x) {
            x.printStackTrace();
            return SubsonicDeployer.DEFAULT_PORT;
        }
    }

    private int getMemoryLimitFromOptionsFile() {
        try {
            String s = grep("-Xmx(\\d+)m");
            return Integer.parseInt(s);
        } catch (Exception x) {
            x.printStackTrace();
            return SubsonicDeployer.DEFAULT_MEMORY_LIMIT;
        }
    }

    private String getContextPathFromOptionsFile() {
        try {
            String s = grep("-Dsubsonic.contextPath=(.*)");
            if (s == null) {
                throw new NullPointerException();
            }
            return s;
        } catch (Exception x) {
            x.printStackTrace();
            return SubsonicDeployer.DEFAULT_CONTEXT_PATH;
        }
    }

    private void createComponents() {
        portTextField = new JFormattedTextField(INTEGER_FORMAT);
        httpsPortTextField = new JFormattedTextField(INTEGER_FORMAT);
        httpsPortCheckBox = new JCheckBox("Enable https on port");
        contextPathComboBox = new JComboBox();
        memoryTextField = new JFormattedTextField(INTEGER_FORMAT);
        defaultButton = new JButton("Restore defaults");
        saveButton = new JButton("Save settings");
    }

    private void configureComponents() {
        contextPathComboBox.setEditable(true);
        contextPathComboBox.addItem("/");
        contextPathComboBox.addItem("/subsonic");
        contextPathComboBox.addItem("/music");
    }

    private void layoutComponents() {
        FormLayout layout = new FormLayout("d, 6dlu, max(d;30dlu):grow");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.append("Port number", portTextField);
        builder.append(httpsPortCheckBox, httpsPortTextField);
        builder.append("Memory limit (MB)", memoryTextField);
        builder.append("Context path", contextPathComboBox);

        setBorder(Borders.DIALOG_BORDER);

        setLayout(new BorderLayout(12, 12));
        add(builder.getPanel(), BorderLayout.CENTER);
        add(ButtonBarFactory.buildCenteredBar(defaultButton, saveButton), BorderLayout.SOUTH);
    }

    private void addBehaviour() {
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    subsonicAgent.checkElevation("-settings", getMemoryLimit() + "," + getPort() + "," + getHttpsPort() + "," + getContextPath());
                    saveSettings(getMemoryLimit(), getPort(), getHttpsPort(), getContextPath());
                } catch (Exception x) {
                    JOptionPane.showMessageDialog(SettingsPanel.this, x.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        defaultButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                portTextField.setValue(SubsonicDeployer.DEFAULT_PORT);
                httpsPortTextField.setValue(4443);
                httpsPortTextField.setEnabled(false);
                httpsPortCheckBox.setSelected(false);
                memoryTextField.setValue(SubsonicDeployer.DEFAULT_MEMORY_LIMIT);
                contextPathComboBox.setSelectedItem(SubsonicDeployer.DEFAULT_CONTEXT_PATH);
            }
        });

        httpsPortCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                httpsPortTextField.setEnabled(httpsPortCheckBox.isSelected());
            }
        });
    }

    private String getContextPath() throws SettingsException {
        String contextPath = (String) contextPathComboBox.getSelectedItem();
        if (contextPath.contains(" ") || !contextPath.startsWith("/")) {
            throw new SettingsException("Please specify a valid context path.");
        }
        return contextPath;
    }

    private int getMemoryLimit() throws SettingsException {
        int memoryLimit;
        try {
            memoryLimit = ((Number) memoryTextField.getValue()).intValue();
            if (memoryLimit < 5) {
                throw new Exception();
            }
        } catch (Exception x) {
            throw new SettingsException("Please specify a valid memory limit.", x);
        }
        return memoryLimit;
    }

    private int getPort() throws SettingsException {
        int port;
        try {
            port = ((Number) portTextField.getValue()).intValue();
            if (port < 1 || port > 65535) {
                throw new Exception();
            }
        } catch (Exception x) {
            throw new SettingsException("Please specify a valid port number.", x);
        }
        return port;
    }

    private int getHttpsPort() throws SettingsException {
        if (!httpsPortCheckBox.isSelected()) {
            return 0;
        }

        int port;
        try {
            port = ((Number) httpsPortTextField.getValue()).intValue();
            if (port < 1 || port > 65535) {
                throw new Exception();
            }
        } catch (Exception x) {
            throw new SettingsException("Please specify a valid https port number.", x);
        }
        return port;
    }

    public void saveSettings(int memoryLimit, int port, int httpsPort, String contextPath) throws SettingsException {
        File file = getOptionsFile();

        java.util.List<String> lines = readLines(file);
        java.util.List<String> newLines = new ArrayList<String>();

        boolean memoryLimitAdded = false;
        boolean portAdded = false;
        boolean httpsPortAdded = false;
        boolean contextPathAdded = false;

        for (String line : lines) {
            if (line.startsWith("-Xmx")) {
                newLines.add("-Xmx" + memoryLimit + "m");
                memoryLimitAdded = true;
            } else if (line.startsWith("-Dsubsonic.port=")) {
                newLines.add("-Dsubsonic.port=" + port);
                portAdded = true;
            } else if (line.startsWith("-Dsubsonic.httpsPort=")) {
                newLines.add("-Dsubsonic.httpsPort=" + httpsPort);
                httpsPortAdded = true;
            } else if (line.startsWith("-Dsubsonic.contextPath=")) {
                newLines.add("-Dsubsonic.contextPath=" + contextPath);
                contextPathAdded = true;
            } else {
                newLines.add(line);
            }
        }

        if (!memoryLimitAdded) {
            newLines.add("-Xmx" + memoryLimit + "m");
        }
        if (!portAdded) {
            newLines.add("-Dsubsonic.port=" + port);
        }
        if (!httpsPortAdded) {
            newLines.add("-Dsubsonic.httpsPort=" + httpsPort);
        }
        if (!contextPathAdded) {
            newLines.add("-Dsubsonic.contextPath=" + contextPath);
        }

        writeLines(file, newLines);

        JOptionPane.showMessageDialog(SettingsPanel.this,
                "Please restart Subsonic for the new settings to take effect.",
                "Settings changed", JOptionPane.INFORMATION_MESSAGE);

    }

    private File getOptionsFile() throws SettingsException {
        File file = new File("subsonic-service.exe.vmoptions");
        if (!file.isFile() || !file.exists()) {
            throw new SettingsException("File " + file.getAbsolutePath() + " not found.");
        }
        return file;
    }

    private List<String> readLines(File file) throws SettingsException {
        List<String> lines = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                lines.add(line);
            }
            return lines;
        } catch (IOException x) {
            throw new SettingsException("Failed to read from file " + file.getAbsolutePath(), x);
        } finally {
            closeQuietly(reader);
        }
    }

    private void writeLines(File file, List<String> lines) throws SettingsException {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new FileWriter(file));
            for (String line : lines) {
                writer.println(line);
            }
        } catch (IOException x) {
            throw new SettingsException("Failed to write to file " + file.getAbsolutePath(), x);
        } finally {
            closeQuietly(writer);
        }
    }

    private String grep(String regexp) throws SettingsException {
        Pattern pattern = Pattern.compile(regexp);
        File file = getOptionsFile();
        for (String line : readLines(file)) {
            Matcher matcher = pattern.matcher(line);
            if (matcher.matches()) {
                return matcher.group(1);
            }
        }
        return null;
    }

    private void closeQuietly(Reader reader) {
        if (reader == null) {
            return;
        }

        try {
            reader.close();
        } catch (IOException x) {
            // Intentionally ignored.
        }
    }

    private void closeQuietly(Writer writer) {
        if (writer == null) {
            return;
        }

        try {
            writer.close();
        } catch (IOException x) {
            // Intentionally ignored.
        }
    }

    public void notifyDeploymentStatus(DeploymentStatus deploymentStatus) {
        // Nothing here yet.
    }

    public void notifyServiceStatus(String serviceStatus) {
        // Nothing here yet.
    }

    public static class SettingsException extends Exception {

        public SettingsException(String message, Throwable cause) {
            super(message, cause);
        }

        public SettingsException(String message) {
            this(message, null);
        }

        @Override
        public String getMessage() {
            if (getCause() == null || getCause().getMessage() == null) {
                return super.getMessage();
            }
            return super.getMessage() + " " + getCause().getMessage();
        }
    }
}