source: trunk/gli/src/org/greenstone/gatherer/util/GSDLSiteConfig.java@ 8622

Last change on this file since 8622 was 8622, checked in by mdewsnip, 19 years ago

Tidied up some more local library stuff.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1package org.greenstone.gatherer.util;
2
3import java.io.*;
4import java.util.*;
5
6public 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 }
54 catch (Exception error) {
55 error.printStackTrace();
56 }
57 }
58 else {
59 debug("No GSDLsite.cfg file can be found!");
60 }
61 }
62
63 public boolean exists() {
64 return gsdlsite_cfg.exists();
65 }
66
67 public String getLocalHostURL() {
68 StringBuffer url = new StringBuffer(LOCAL_HOST);
69 url.append(COLON);
70 url.append((String)get(PORTNUMBER));
71 String enterlib = (String)get(ENTERLIB);
72 if(enterlib == null || enterlib.length() == 0) {
73 // Use the default /gsdl and hope for the best.
74 url.append(SEPARATOR);
75 url.append(GSDL);
76 }
77 else {
78 if(!enterlib.startsWith(SEPARATOR)) {
79 url.append(SEPARATOR);
80 }
81 url.append(enterlib);
82 }
83 enterlib = null;
84 debug("Found Local Library Address: " + url.toString());
85 return url.toString();
86 }
87
88 public String getSiteConfigFilename() {
89 return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
90 }
91
92 public String getURL() {
93 // URL is made from url and portnumber
94 String url = (String) get(URL);
95 if(url != null) {
96 StringBuffer temp = new StringBuffer(url);
97 temp.append(COLON);
98 temp.append((String)get(PORTNUMBER));
99 String enterlib = (String)get(ENTERLIB);
100 if(enterlib == null || enterlib.length() == 0) {
101 // Use the default /gsdl and hope for the best.
102 temp.append(SEPARATOR);
103 temp.append(GSDL);
104 }
105 else {
106 if(!enterlib.startsWith(SEPARATOR)) {
107 temp.append(SEPARATOR);
108 }
109 temp.append(enterlib);
110 }
111 enterlib = null;
112 url = temp.toString();
113 }
114 debug("Found Local Library Address: " + url);
115 return url;
116 }
117
118 public void load() {
119 if(glisite_cfg.exists()) {
120 debug("Load: " + glisite_cfg.getAbsolutePath());
121 clear();
122 try {
123 BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
124 String line = null;
125 while((line = in.readLine()) != null) {
126 String key = null;
127 String value = null;
128 int index = -1;
129 if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
130 key = line.substring(0, index);
131 value = line.substring(index + 1);
132 }
133 else {
134 key = line;
135 }
136 put(key, value);
137 }
138 }
139 catch (Exception error) {
140 error.printStackTrace();
141 }
142 }
143 else {
144 debug("No GSDLsite.cfg file can be found!");
145 }
146 }
147
148 /** Restore the autoenter value to its initial value, and remove url if present. */
149 public void restore() {
150 if(glisite_cfg != null) {
151 // Delete the file
152 glisite_cfg.delete();
153 }
154 else {
155 debug("Restore Initial Settings");
156 put(AUTOENTER, autoenter_initial);
157 put(STARTBROWSER, start_browser_initial);
158 remove(URL);
159 save();
160 }
161 }
162
163 public void set() {
164 debug("Set Session Settings");
165 if(autoenter_initial == null) {
166 autoenter_initial = (String) get(AUTOENTER);
167 debug("Remember autoenter was: " + autoenter_initial);
168 }
169 put(AUTOENTER, TRUE);
170 if(start_browser_initial == null) {
171 start_browser_initial = (String) get(STARTBROWSER);
172 debug("Remember start_browser was: " + start_browser_initial);
173 }
174 put(STARTBROWSER, FALSE);
175 save();
176 }
177
178 private void debug(String message) {
179 ///ystem.err.println(message);
180 }
181
182 private void save() {
183 //debug("Save: " + gsdlsite_cfg.getAbsolutePath());
184 debug("Save: " + glisite_cfg.getAbsolutePath());
185 try {
186 //BufferedWriter out = new BufferedWriter(new FileWriter(gsdlsite_cfg, false));
187 BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
188 for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
189 String key = (String) keys.next();
190 String value = (String) get(key);
191 out.write(key, 0, key.length());
192 if(value != null) {
193 out.write('=');
194 out.write(value, 0, value.length());
195 }
196 out.newLine();
197 }
198 out.flush();
199 out.close();
200 }
201 catch (Exception error) {
202 error.printStackTrace();
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.