1 | package org.greenstone.gatherer.util;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.util.*;
|
---|
5 |
|
---|
6 | public class GSDLSiteConfig
|
---|
7 | extends LinkedHashMap {
|
---|
8 | private File gsdlsite_cfg;
|
---|
9 | private File glisite_cfg;
|
---|
10 | private String autoenter_initial;
|
---|
11 | private String start_browser_initial;
|
---|
12 |
|
---|
13 | static final private String AUTOENTER = "autoenter";
|
---|
14 | static final private String COLON = ":";
|
---|
15 | static final private String ENTERLIB = "enterlib";
|
---|
16 | static final private String FALSE = "0";
|
---|
17 | static final private String GLISITE_CFG = "glisite.cfg";
|
---|
18 | static final private String GSDL = "gsdl";
|
---|
19 | static final private String GSDLSITE_CFG = "gsdlsite.cfg";
|
---|
20 | static final private String LOCAL_HOST = "http://localhost";
|
---|
21 | static final private String PORTNUMBER = "portnumber";
|
---|
22 | static final private String SEPARATOR = "/";
|
---|
23 | static final private String SPECIFIC_CONFIG = "--config=";
|
---|
24 | static final private String STARTBROWSER = "start_browser";
|
---|
25 | static final private String TRUE = "1";
|
---|
26 | static final private String URL = "url";
|
---|
27 |
|
---|
28 | public GSDLSiteConfig(File server_exe) {
|
---|
29 | debug("New GSDLSiteConfig for: " + server_exe.getAbsolutePath());
|
---|
30 | gsdlsite_cfg = new File(server_exe.getParentFile(), GSDLSITE_CFG);
|
---|
31 | glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
|
---|
32 | autoenter_initial = null;
|
---|
33 | start_browser_initial = null;
|
---|
34 | if(gsdlsite_cfg.exists()) {
|
---|
35 | debug("Load: " + gsdlsite_cfg.getAbsolutePath());
|
---|
36 | clear();
|
---|
37 | try {
|
---|
38 | BufferedReader in = new BufferedReader(new FileReader(gsdlsite_cfg));
|
---|
39 | String line = null;
|
---|
40 | while((line = in.readLine()) != null) {
|
---|
41 | String key = null;
|
---|
42 | String value = null;
|
---|
43 | int index = -1;
|
---|
44 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
45 | key = line.substring(0, index);
|
---|
46 | value = line.substring(index + 1);
|
---|
47 | }
|
---|
48 | else {
|
---|
49 | key = line;
|
---|
50 | }
|
---|
51 | put(key, value);
|
---|
52 | }
|
---|
53 | in.close();
|
---|
54 | }
|
---|
55 | catch (Exception error) {
|
---|
56 | error.printStackTrace();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | else {
|
---|
60 | debug("No GSDLsite.cfg file can be found!");
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public boolean exists() {
|
---|
65 | return gsdlsite_cfg.exists();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String getLocalHostURL() {
|
---|
69 | StringBuffer url = new StringBuffer(LOCAL_HOST);
|
---|
70 | url.append(COLON);
|
---|
71 | url.append((String)get(PORTNUMBER));
|
---|
72 | String enterlib = (String)get(ENTERLIB);
|
---|
73 | if(enterlib == null || enterlib.length() == 0) {
|
---|
74 | // Use the default /gsdl and hope for the best.
|
---|
75 | url.append(SEPARATOR);
|
---|
76 | url.append(GSDL);
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
80 | url.append(SEPARATOR);
|
---|
81 | }
|
---|
82 | url.append(enterlib);
|
---|
83 | }
|
---|
84 | enterlib = null;
|
---|
85 | debug("Found Local Library Address: " + url.toString());
|
---|
86 | return url.toString();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public String getSiteConfigFilename() {
|
---|
90 | return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public String getURL() {
|
---|
94 | // URL is made from url and portnumber
|
---|
95 | String url = (String) get(URL);
|
---|
96 | if(url != null) {
|
---|
97 | StringBuffer temp = new StringBuffer(url);
|
---|
98 | temp.append(COLON);
|
---|
99 | temp.append((String)get(PORTNUMBER));
|
---|
100 | String enterlib = (String)get(ENTERLIB);
|
---|
101 | if(enterlib == null || enterlib.length() == 0) {
|
---|
102 | // Use the default /gsdl and hope for the best.
|
---|
103 | temp.append(SEPARATOR);
|
---|
104 | temp.append(GSDL);
|
---|
105 | }
|
---|
106 | else {
|
---|
107 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
108 | temp.append(SEPARATOR);
|
---|
109 | }
|
---|
110 | temp.append(enterlib);
|
---|
111 | }
|
---|
112 | enterlib = null;
|
---|
113 | url = temp.toString();
|
---|
114 | }
|
---|
115 | debug("Found Local Library Address: " + url);
|
---|
116 | return url;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void load() {
|
---|
120 | if(glisite_cfg.exists()) {
|
---|
121 | debug("Load: " + glisite_cfg.getAbsolutePath());
|
---|
122 | clear();
|
---|
123 | try {
|
---|
124 | BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
|
---|
125 | String line = null;
|
---|
126 | while((line = in.readLine()) != null) {
|
---|
127 | String key = null;
|
---|
128 | String value = null;
|
---|
129 | int index = -1;
|
---|
130 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
131 | key = line.substring(0, index);
|
---|
132 | value = line.substring(index + 1);
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | key = line;
|
---|
136 | }
|
---|
137 | put(key, value);
|
---|
138 | }
|
---|
139 | in.close();
|
---|
140 | }
|
---|
141 | catch (Exception error) {
|
---|
142 | error.printStackTrace();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | else {
|
---|
146 | debug("No GSDLsite.cfg file can be found!");
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
151 | public void restore() {
|
---|
152 | if(glisite_cfg != null) {
|
---|
153 | // Delete the file
|
---|
154 | glisite_cfg.delete();
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | debug("Restore Initial Settings");
|
---|
158 | put(AUTOENTER, autoenter_initial);
|
---|
159 | put(STARTBROWSER, start_browser_initial);
|
---|
160 | remove(URL);
|
---|
161 | save();
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void set() {
|
---|
166 | debug("Set Session Settings");
|
---|
167 | if(autoenter_initial == null) {
|
---|
168 | autoenter_initial = (String) get(AUTOENTER);
|
---|
169 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
170 | }
|
---|
171 | put(AUTOENTER, TRUE);
|
---|
172 | if(start_browser_initial == null) {
|
---|
173 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
174 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
175 | }
|
---|
176 | put(STARTBROWSER, FALSE);
|
---|
177 | save();
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void debug(String message) {
|
---|
181 | ///ystem.err.println(message);
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void save() {
|
---|
185 | //debug("Save: " + gsdlsite_cfg.getAbsolutePath());
|
---|
186 | debug("Save: " + glisite_cfg.getAbsolutePath());
|
---|
187 | try {
|
---|
188 | //BufferedWriter out = new BufferedWriter(new FileWriter(gsdlsite_cfg, false));
|
---|
189 | BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
|
---|
190 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
191 | String key = (String) keys.next();
|
---|
192 | String value = (String) get(key);
|
---|
193 | out.write(key, 0, key.length());
|
---|
194 | if(value != null) {
|
---|
195 | out.write('=');
|
---|
196 | out.write(value, 0, value.length());
|
---|
197 | }
|
---|
198 | out.newLine();
|
---|
199 | }
|
---|
200 | out.flush();
|
---|
201 | out.close();
|
---|
202 | }
|
---|
203 | catch (Exception error) {
|
---|
204 | error.printStackTrace();
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|