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 JCheckBox allowConnections;
|
---|
20 | protected JRadioButton[] hostRadioButtons = new JRadioButton[4];
|
---|
21 |
|
---|
22 | // 0 to 3: 0 is resolved (hostname) from local IP, 1 is local IP address, 2 is localhost, 3 is 127.0.0.1
|
---|
23 | protected int address_resolution_method = 2;
|
---|
24 | protected int externalaccess = 0;
|
---|
25 |
|
---|
26 | public Server2Settings(BaseServer server)
|
---|
27 | {
|
---|
28 | super(server);
|
---|
29 | }
|
---|
30 |
|
---|
31 | protected JPanel createServletPanel()
|
---|
32 | {
|
---|
33 | JPanel server2panel = new JPanel();
|
---|
34 | server2panel.setLayout(new BorderLayout());
|
---|
35 |
|
---|
36 | boolean allowCons = false;
|
---|
37 | String externalAccess = server.config_properties.getProperty("externalaccess").trim();
|
---|
38 | if(externalAccess != null && externalAccess.equals("1")) {
|
---|
39 | this.externalaccess = 1;
|
---|
40 | allowCons = true;
|
---|
41 | }
|
---|
42 | allowConnections = new JCheckBox(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".ExternalAccess"), allowCons);
|
---|
43 | allowConnections.setBackground(bg_color);
|
---|
44 |
|
---|
45 | JPanel connect_panel = new JPanel(new GridLayout(4, 1));
|
---|
46 | connect_panel.setBackground(bg_color);
|
---|
47 | connect_panel.setBorder(BorderFactory.createTitledBorder(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".AddressResolutionMethod")));
|
---|
48 |
|
---|
49 | hostRadioButtons = new JRadioButton[4];
|
---|
50 | hostRadioButtons[0] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".ResolveIP"));
|
---|
51 | hostRadioButtons[1] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".LocalIP"));
|
---|
52 | hostRadioButtons[2] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".AlwaysUse")+" localhost");
|
---|
53 | hostRadioButtons[3] = new JRadioButton(server.dictionary.get(BaseServer.Property.SERVER_SETTINGS+".AlwaysUse")+" 127.0.0.1");
|
---|
54 |
|
---|
55 | ButtonGroup hostGroup = new ButtonGroup();
|
---|
56 | for(int i = 0; i < hostRadioButtons.length; i ++) {
|
---|
57 | connect_panel.add(hostRadioButtons[i]);
|
---|
58 | hostGroup.add(hostRadioButtons[i]);
|
---|
59 | hostRadioButtons[i].setBackground(bg_color);
|
---|
60 | }
|
---|
61 |
|
---|
62 | String addressResolutionMethod = server.config_properties.getProperty("address_resolution_method").trim();
|
---|
63 | if(addressResolutionMethod != null) {
|
---|
64 | this.address_resolution_method = Integer.parseInt(addressResolutionMethod);
|
---|
65 | }
|
---|
66 | hostRadioButtons[address_resolution_method].setSelected(true);
|
---|
67 |
|
---|
68 | JPanel comb_panel = new JPanel(new BorderLayout());
|
---|
69 | comb_panel.add(allowConnections, BorderLayout.NORTH);
|
---|
70 | comb_panel.add(connect_panel, BorderLayout.CENTER);
|
---|
71 | return comb_panel;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public boolean[] onSave()
|
---|
75 | {
|
---|
76 | // superclass detects changes to port and autoenter
|
---|
77 | // handle changes to address_resolution_method and externalAccess/allowConnections here
|
---|
78 |
|
---|
79 | boolean hasChanged = false;
|
---|
80 | boolean requireRestart = false;
|
---|
81 |
|
---|
82 | for(int i = 0; i < hostRadioButtons.length; i++) {
|
---|
83 | if(hostRadioButtons[i].isSelected() && address_resolution_method != i) {
|
---|
84 | address_resolution_method = i;
|
---|
85 | hasChanged = true;
|
---|
86 | requireRestart = true;
|
---|
87 | server.reconfigRequired();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | int oldExternalAccess = externalaccess;
|
---|
92 | externalaccess = allowConnections.isSelected() ? 1 : 0;
|
---|
93 | if (oldExternalAccess != externalaccess) {
|
---|
94 | hasChanged = true;
|
---|
95 | requireRestart = true;
|
---|
96 | server.reconfigRequired();
|
---|
97 | }
|
---|
98 |
|
---|
99 | boolean[] returnValues = { hasChanged, requireRestart };
|
---|
100 | return returnValues;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void save(ScriptReadWrite scriptReadWrite, ArrayList newFileLines)
|
---|
104 | {
|
---|
105 | // write only 1 or 0 (and not true or false) for Server2
|
---|
106 | boolean auto_enter = autoEnter.isSelected();
|
---|
107 | if(autoStart != auto_enter) {
|
---|
108 | String newAutoEnter = auto_enter ? "1" : "0";
|
---|
109 | newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.AUTOSTART, newAutoEnter);
|
---|
110 | }
|
---|
111 | boolean keep_port = keepPortToggle.isSelected();
|
---|
112 | if(keepPort != keep_port) {
|
---|
113 | String newKeepPort = keep_port ? "1" : "0";
|
---|
114 | newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.KEEPPORT, newKeepPort);
|
---|
115 | }
|
---|
116 |
|
---|
117 | // external access - onSave() would have updated this value
|
---|
118 | newFileLines = scriptReadWrite.queryReplace(newFileLines, "externalaccess", Integer.toString(externalaccess));
|
---|
119 |
|
---|
120 | // work out the host (default is address_resolution_method 2: localhost)
|
---|
121 | String hostIP = "127.0.0.1";
|
---|
122 | String hosts = "";
|
---|
123 | InetAddress inetAddress = null;
|
---|
124 | try {
|
---|
125 | inetAddress = InetAddress.getLocalHost();
|
---|
126 | hosts = inetAddress.getHostName();
|
---|
127 | hostIP = inetAddress.getHostAddress(); // used for all cases unless an Exception is thrown here
|
---|
128 | } catch(UnknownHostException e) {
|
---|
129 | logger.error(e);
|
---|
130 | logger.info("Server2.java reload(): Defaulting host URL to localhost");
|
---|
131 | hostIP = "127.0.0.1";
|
---|
132 | hosts = "";
|
---|
133 | address_resolution_method = 2;
|
---|
134 | }
|
---|
135 |
|
---|
136 | newFileLines = scriptReadWrite.replaceOrAddLine(newFileLines, "hostIP", hostIP, true);
|
---|
137 | newFileLines = scriptReadWrite.replaceOrAddLine(newFileLines, "hosts", hosts, true);
|
---|
138 |
|
---|
139 | // address resolution method - onSave() would have updated
|
---|
140 | // this value (or the UnknownHostException above might have)
|
---|
141 | newFileLines = scriptReadWrite.queryReplace(newFileLines, "address_resolution_method", Integer.toString(address_resolution_method));
|
---|
142 |
|
---|
143 | }
|
---|
144 | }
|
---|