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