1 | package org.greenstone.server;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.FlowLayout;
|
---|
5 | import java.awt.GridLayout;
|
---|
6 | import java.awt.event.*;
|
---|
7 | import java.net.InetAddress;
|
---|
8 | import java.net.UnknownHostException;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import javax.swing.*;
|
---|
11 | import javax.swing.event.*;
|
---|
12 |
|
---|
13 | import org.greenstone.server.BaseServerSettings;
|
---|
14 | import org.greenstone.util.ScriptReadWrite;
|
---|
15 |
|
---|
16 | public class Server2Settings extends BaseServerSettings
|
---|
17 | {
|
---|
18 | protected JComboBox prefix_combobox;
|
---|
19 | protected JRadioButton[] hostRadioButtons = new JRadioButton[4];
|
---|
20 |
|
---|
21 | // 0 to 3: 0 is resolved (hostname) from local IP, 1 is local IP address, 2 is localhost, 3 is 127.0.0.1
|
---|
22 | protected int address_resolution_method = 2;
|
---|
23 |
|
---|
24 | public Server2Settings(BaseServer server)
|
---|
25 | {
|
---|
26 | super(server);
|
---|
27 | }
|
---|
28 |
|
---|
29 | protected JPanel createServletPanel()
|
---|
30 | {
|
---|
31 | JPanel server2panel = new JPanel();
|
---|
32 | server2panel.setLayout(new BorderLayout());
|
---|
33 |
|
---|
34 | JPanel connect_panel = new JPanel(new GridLayout(4, 1));
|
---|
35 | connect_panel.setBackground(bg_color);
|
---|
36 | connect_panel.setBorder(BorderFactory.createTitledBorder(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".AddressResolutionMethod")));
|
---|
37 |
|
---|
38 | hostRadioButtons = new JRadioButton[4];
|
---|
39 | hostRadioButtons[0] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".ResolveIP"));
|
---|
40 | hostRadioButtons[1] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".LocalIP"));
|
---|
41 | hostRadioButtons[2] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".AlwaysUse")+" localhost");
|
---|
42 | hostRadioButtons[3] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".AlwaysUse")+" 127.0.0.1");
|
---|
43 |
|
---|
44 | ButtonGroup hostGroup = new ButtonGroup();
|
---|
45 | for(int i = 0; i < hostRadioButtons.length; i ++) {
|
---|
46 | connect_panel.add(hostRadioButtons[i]);
|
---|
47 | hostGroup.add(hostRadioButtons[i]);
|
---|
48 | hostRadioButtons[i].setBackground(bg_color);
|
---|
49 | }
|
---|
50 |
|
---|
51 | String addressResolutionMethod = server.config_properties.getProperty("address_resolution_method", "").trim();
|
---|
52 | if(!addressResolutionMethod.equals("")) {
|
---|
53 | this.address_resolution_method = Integer.parseInt(addressResolutionMethod);
|
---|
54 | }
|
---|
55 | hostRadioButtons[address_resolution_method].setSelected(true);
|
---|
56 |
|
---|
57 | JPanel comb_panel = new JPanel(new BorderLayout());
|
---|
58 | comb_panel.add(connect_panel, BorderLayout.CENTER);
|
---|
59 | return comb_panel;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public boolean[] onSave()
|
---|
63 | {
|
---|
64 | // superclass detects changes to port and autoenter
|
---|
65 | // handle changes to address_resolution_method here
|
---|
66 |
|
---|
67 | boolean hasChanged = false;
|
---|
68 | boolean requireRestart = false;
|
---|
69 |
|
---|
70 | for(int i = 0; i < hostRadioButtons.length; i++) {
|
---|
71 | if(hostRadioButtons[i].isSelected() && address_resolution_method != i) {
|
---|
72 | address_resolution_method = i;
|
---|
73 | hasChanged = true;
|
---|
74 | requireRestart = true;
|
---|
75 | server.reconfigRequired();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | boolean[] returnValues = { hasChanged, requireRestart };
|
---|
80 | return returnValues;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void save(ScriptReadWrite scriptReadWrite, ArrayList<String> newFileLines)
|
---|
84 | {
|
---|
85 | // write only 1 or 0 (and not true or false) for Server2
|
---|
86 | boolean auto_enter = autoEnter.isSelected();
|
---|
87 | if(autoStart != auto_enter) {
|
---|
88 | String newAutoEnter = auto_enter ? "1" : "0";
|
---|
89 | newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.AUTOSTART, newAutoEnter);
|
---|
90 | }
|
---|
91 | boolean keep_port = keepPortToggle.isSelected();
|
---|
92 | if(keepPort != keep_port) {
|
---|
93 | String newKeepPort = keep_port ? "1" : "0";
|
---|
94 | newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.KEEPPORT, newKeepPort);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // external access - onSave() would have updated this value
|
---|
98 | // Its possible values are specific to the version of Greenstone: 0 or 1 for GS2 (true or false for GS3)
|
---|
99 | newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.ALLOW_EXTERNAL_ACCESS, externalaccess ? "1" : "0");
|
---|
100 |
|
---|
101 | // work out the host (default is address_resolution_method 2: localhost)
|
---|
102 | String hostIP = "127.0.0.1";
|
---|
103 | String hosts = "";
|
---|
104 | InetAddress inetAddress = null;
|
---|
105 | try {
|
---|
106 | inetAddress = InetAddress.getLocalHost();
|
---|
107 | hosts = inetAddress.getHostName();
|
---|
108 | hostIP = inetAddress.getHostAddress(); // used for all cases unless an Exception is thrown here
|
---|
109 | } catch(UnknownHostException e) {
|
---|
110 | logger.error(e);
|
---|
111 | logger.info("Server2.java reload(): Defaulting host URL to localhost");
|
---|
112 | hostIP = "127.0.0.1";
|
---|
113 | hosts = "";
|
---|
114 | address_resolution_method = 2;
|
---|
115 | }
|
---|
116 |
|
---|
117 | newFileLines = scriptReadWrite.replaceOrAddLine(newFileLines, "hostIP", hostIP, true);
|
---|
118 | newFileLines = scriptReadWrite.replaceOrAddLine(newFileLines, "hosts", hosts, true);
|
---|
119 |
|
---|
120 | // address resolution method - onSave() would have updated
|
---|
121 | // this value (or the UnknownHostException above might have)
|
---|
122 | newFileLines = scriptReadWrite.queryReplace(newFileLines, "address_resolution_method", Integer.toString(address_resolution_method));
|
---|
123 |
|
---|
124 | }
|
---|
125 | }
|
---|