source: gli/trunk/src/org/greenstone/gatherer/greenstone3/ServletConfiguration.java@ 14538

Last change on this file since 14538 was 14538, checked in by qq6, 17 years ago

if the server couldn't be connected in 10 seconds, a warning message is displayed and suggesting the user to terminate the current connecting

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.greenstone3;
38
39import java.awt.*;
40import java.io.*;
41import java.lang.ref.*;
42import java.lang.Thread;
43import java.net.*;
44import java.util.*;
45import javax.swing.*;
46import javax.swing.plaf.*;
47import org.greenstone.gatherer.Gatherer;
48import org.greenstone.gatherer.DebugStream;
49import org.greenstone.gatherer.util.ArrayTools;
50import org.greenstone.gatherer.util.StaticStrings;
51import org.greenstone.gatherer.util.Utility;
52import org.greenstone.gatherer.util.XMLTools;
53import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
54import org.w3c.dom.*;
55
56/** This class stores the mapping between sites and servlets available for them. The info is read in from the web.xml file.
57 * @author Katherine Don, New Zealand Digital Library, University of Waikato
58 * @version
59 */
60public class ServletConfiguration {
61
62 static public String ADD_COMMAND = "?a=s&sa=a&st=collection&sn=";
63 static public String DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
64 private String gsdl3_path = "";
65 private ArrayList sites = null;
66 private HashMap mappings = null;
67
68 public ServletConfiguration(String gsdl3_path) throws InterruptedException {
69
70 this.gsdl3_path = gsdl3_path;
71
72 if (Gatherer.isGsdlRemote){
73 // if (RemoteGreenstoneServer.downloadWebXMLFile().equals("")) {
74 //System.err.println("Error: Could not download web.xml.");
75 //System.exit(0);
76 //}
77 System.out.println("Starting download web.xml.");
78 long startTime = System.currentTimeMillis();
79 long patience = 1000 * 10;
80 Thread t = new Thread(new downloadWebXMl());
81 t.start();
82
83 System.out.println("Waiting for download thread to finish");
84 while (t.isAlive()) {
85 System.out.println("Still waiting...");
86 //Wait maximum of 1 second for MessageLoop thread to
87 //finish.
88 t.join(1000);
89 if (((System.currentTimeMillis() - startTime) > patience) &&
90 t.isAlive()) {
91 //t.interrupt();
92 //Shouldn't be long now -- wait indefinitely
93 //t.join();
94 System.out.print("Have been waiting 10 seconds for connecting to the server, do you want to terminate the connection?(y/n)");
95 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
96 try {
97 if (br.readLine().toLowerCase().startsWith("y")){
98 System.out.println("Terminate the connection! ");
99 System.exit(0);
100 }else{
101 startTime = System.currentTimeMillis();
102 };
103 } catch (IOException ioe) {
104 System.out.println("IO error trying to read your answer!");
105 System.exit(1);
106 }
107 }
108 }
109 System.out.println("The web.xml file has been downloaded");
110 }
111
112 //String web_xml_path = gsdl3_path + File.separator + "web" + File.separator + "WEB-INF"+ File.separator + "web.xml";
113 File web_xml;
114 if (Gatherer.isGsdlRemote){
115 web_xml = new File(gsdl3_path + "web.xml");
116 }else{
117 web_xml = new File(gsdl3_path + "WEB-INF"+ File.separator + "web.xml");
118 }
119 if (!web_xml.exists()) {
120 DebugStream.println("Error: no web.xml found at "+web_xml.toString());
121 return;
122 }
123
124 this.sites = new ArrayList();
125 if (Gatherer.isGsdlRemote){
126 String sites_on_server = RemoteGreenstoneServer.getSiteNames();
127 if (sites_on_server.equals("")) {
128 // !! Something went wrong : could not get names of the sites
129 System.err.println("Error: Could not get names of the sites.");
130 System.exit(0);
131 }
132
133 String[] sites_arr=sites_on_server.split("-----");
134 for (int i=0; i<sites_arr.length; i++){
135 if (!(sites_arr[i].trim().equals(""))){
136 this.sites.add(sites_arr[i].trim());
137 }
138 }
139 }else{
140 // find the sites
141 File start = new File(Utility.getSitesDir(gsdl3_path));
142 File possible_sites[] = start.listFiles();
143 ArrayTools.sort(possible_sites);
144 for (int i=0; possible_sites != null && i < possible_sites.length; i++) {
145 File site_config = new File(possible_sites[i], "siteConfig.xml");
146 if (site_config.exists()) {
147 this.sites.add(possible_sites[i].getName());
148 }
149 }
150 }
151
152 this.mappings = new HashMap();
153 Document web_config = XMLTools.parseXMLFile(web_xml.getAbsolutePath(), false);
154
155 NodeList servlet_mappings = web_config.getElementsByTagName("servlet-mapping");
156 // make a little map class
157 HashMap url_mappings = new HashMap();
158 for (int i=0; i<servlet_mappings.getLength(); i++) {
159 Element map = (Element)servlet_mappings.item(i);
160 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "servlet-name"));
161 String pattern = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "url-pattern"));
162 url_mappings.put(name, pattern);
163
164 }
165
166 NodeList servlets = web_config.getElementsByTagName("servlet");
167 for (int i=0; i<servlets.getLength(); i++) {
168 Element servlet = (Element)servlets.item(i);
169 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "servlet-name"));
170 String description = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "description"));
171 String site = getServletSite(servlet);
172
173 if (site != null) {
174 ArrayList this_site = (ArrayList)mappings.get(site);
175 if (this_site == null) {
176 this_site = new ArrayList();
177 }
178 String url = (String)url_mappings.get(name);
179 this_site.add(url);
180 mappings.put(site, this_site);
181 }
182
183 }
184
185 web_config = null;
186 url_mappings = null;
187 }
188
189 public ArrayList getSites() {
190 return this.sites;
191 }
192
193 public ArrayList getServletsForSite(String site) {
194 return (ArrayList)this.mappings.get(site);
195 }
196 public String getServletPath(String site) {
197
198 // for now, get the first one
199 ArrayList servlets = (ArrayList)mappings.get(site);
200 if(servlets == null) {
201 return null;
202 }
203 return (String)servlets.get(0);
204 }
205
206 private String getServletSite(Element servlet) {
207
208 NodeList params = servlet.getElementsByTagName("init-param");
209 if ( params == null || params.getLength() == 0) {
210 return null;
211 }
212 String site = null;
213 for (int i=0; i<params.getLength()&& site == null; i++) {
214 String p_name = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-name"));
215 if (p_name.equals("site_name")) {
216 site = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-value"));
217 }
218 }
219 return site;
220 }
221
222 private static class downloadWebXMl implements Runnable {
223 public void run() {
224 if (RemoteGreenstoneServer.downloadWebXMLFile().equals("")) {
225 System.err.println("Error: Could not download web.xml.");
226 System.exit(0);
227 }
228 }
229 }
230
231}
Note: See TracBrowser for help on using the repository browser.