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 String autoenter_initial;
|
---|
10 | private String start_browser_initial;
|
---|
11 |
|
---|
12 | static final public String ADD_COMMAND = "a=config&cmd=add-collection&c=";
|
---|
13 | static final public String RELEASE_COMMAND = "a=config&cmd=release-collection&c=";
|
---|
14 | static final public String QUIT_COMMAND = "a=";
|
---|
15 |
|
---|
16 | static final private String AUTOENTER = "autoenter";
|
---|
17 | static final private String COLON = ":";
|
---|
18 | static final private String ENTERLIB = "enterlib";
|
---|
19 | static final private String FALSE = "0";
|
---|
20 | static final private String GSDL = "gsdl";
|
---|
21 | static final private String GSDLSITE_CFG = "gsdlsite.cfg";
|
---|
22 | static final private String PORTNUMBER = "portnumber";
|
---|
23 | static final private String SEPARATOR = "/";
|
---|
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 |
|
---|
32 | autoenter_initial = null;
|
---|
33 | start_browser_initial = null;
|
---|
34 | load();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public boolean exists() {
|
---|
38 | return gsdlsite_cfg.exists();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public String getURL() {
|
---|
42 | // URL is made from url and portnumber
|
---|
43 | String url = (String) get(URL);
|
---|
44 | if(url != null) {
|
---|
45 | StringBuffer temp = new StringBuffer(url);
|
---|
46 | temp.append(COLON);
|
---|
47 | temp.append((String)get(PORTNUMBER));
|
---|
48 | String enterlib = (String)get(ENTERLIB);
|
---|
49 | if(enterlib == null || enterlib.length() == 0) {
|
---|
50 | // Use the default /gsdl and hope for the best.
|
---|
51 | temp.append(SEPARATOR);
|
---|
52 | temp.append(GSDL);
|
---|
53 | }
|
---|
54 | else {
|
---|
55 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
56 | temp.append(SEPARATOR);
|
---|
57 | }
|
---|
58 | temp.append(enterlib);
|
---|
59 | }
|
---|
60 | enterlib = null;
|
---|
61 | url = temp.toString();
|
---|
62 | }
|
---|
63 | debug("Found Local Library Address: " + url);
|
---|
64 | return url;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void load() {
|
---|
68 | if(gsdlsite_cfg.exists()) {
|
---|
69 | debug("Load: " + gsdlsite_cfg.getAbsolutePath());
|
---|
70 | clear();
|
---|
71 | try {
|
---|
72 | BufferedReader in = new BufferedReader(new FileReader(gsdlsite_cfg));
|
---|
73 | String line = null;
|
---|
74 | while((line = in.readLine()) != null) {
|
---|
75 | String key = null;
|
---|
76 | String value = null;
|
---|
77 | int index = -1;
|
---|
78 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
79 | key = line.substring(0, index);
|
---|
80 | value = line.substring(index + 1);
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | key = line;
|
---|
84 | }
|
---|
85 | put(key, value);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | catch (Exception error) {
|
---|
89 | error.printStackTrace();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | debug("No GSDLsite.cfg file can be found!");
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
98 | public void restore() {
|
---|
99 | debug("Restore Initial Settings");
|
---|
100 | put(AUTOENTER, autoenter_initial);
|
---|
101 | put(STARTBROWSER, start_browser_initial);
|
---|
102 | remove(URL);
|
---|
103 | save();
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void set() {
|
---|
107 | debug("Set Session Settings");
|
---|
108 | if(autoenter_initial == null) {
|
---|
109 | autoenter_initial = (String) get(AUTOENTER);
|
---|
110 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
111 | }
|
---|
112 | put(AUTOENTER, TRUE);
|
---|
113 | if(start_browser_initial == null) {
|
---|
114 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
115 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
116 | }
|
---|
117 | save();
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void debug(String message) {
|
---|
121 | System.err.println(message);
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void save() {
|
---|
125 | debug("Save: " + gsdlsite_cfg.getAbsolutePath());
|
---|
126 | try {
|
---|
127 | BufferedWriter out = new BufferedWriter(new FileWriter(gsdlsite_cfg, false));
|
---|
128 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
129 | String key = (String) keys.next();
|
---|
130 | String value = (String) get(key);
|
---|
131 | out.write(key, 0, key.length());
|
---|
132 | if(value != null) {
|
---|
133 | out.write('=');
|
---|
134 | out.write(value, 0, value.length());
|
---|
135 | }
|
---|
136 | out.newLine();
|
---|
137 | }
|
---|
138 | out.flush();
|
---|
139 | out.close();
|
---|
140 | }
|
---|
141 | catch (Exception error) {
|
---|
142 | error.printStackTrace();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|