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

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

removed the connection check, because it doesn't work on applet

  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 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.net.*;
43import java.util.*;
44import javax.swing.*;
45import javax.swing.plaf.*;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.DebugStream;
48import org.greenstone.gatherer.util.ArrayTools;
49import org.greenstone.gatherer.util.StaticStrings;
50import org.greenstone.gatherer.util.Utility;
51import org.greenstone.gatherer.util.XMLTools;
52import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
53import org.w3c.dom.*;
54
55/** This class stores the mapping between sites and servlets available for them. The info is read in from the web.xml file.
56 * @author Katherine Don, New Zealand Digital Library, University of Waikato
57 * @version
58 */
59public class ServletConfiguration {
60
61 static public String ADD_COMMAND = "?a=s&sa=a&st=collection&sn=";
62 static public String DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
63 private String gsdl3_path = "";
64 private ArrayList sites = null;
65 private HashMap mappings = null;
66
67 public ServletConfiguration(String gsdl3_path) {
68
69 this.gsdl3_path = gsdl3_path;
70
71 if (Gatherer.isGsdlRemote){
72 if (RemoteGreenstoneServer.downloadWebXMLFile().equals("")) {
73 System.err.println("Error: Could not download web.xml.");
74 System.exit(0);
75 }
76 }
77 //String web_xml_path = gsdl3_path + File.separator + "web" + File.separator + "WEB-INF"+ File.separator + "web.xml";
78 File web_xml;
79 if (Gatherer.isGsdlRemote){
80 web_xml = new File(gsdl3_path + "web.xml");
81 }else{
82 web_xml = new File(gsdl3_path + "WEB-INF"+ File.separator + "web.xml");
83 }
84 if (!web_xml.exists()) {
85 DebugStream.println("Error: no web.xml found at "+web_xml.toString());
86 return;
87 }
88
89 this.sites = new ArrayList();
90 if (Gatherer.isGsdlRemote){
91 String sites_on_server = RemoteGreenstoneServer.getSiteNames();
92 if (sites_on_server.equals("")) {
93 // !! Something went wrong : could not get names of the sites
94 System.err.println("Error: Could not get names of the sites.");
95 System.exit(0);
96 }
97
98 String[] sites_arr=sites_on_server.split("-----");
99 for (int i=0; i<sites_arr.length; i++){
100 if (!(sites_arr[i].trim().equals(""))){
101 this.sites.add(sites_arr[i].trim());
102 }
103 }
104 }else{
105 // find the sites
106 File start = new File(Utility.getSitesDir(gsdl3_path));
107 File possible_sites[] = start.listFiles();
108 ArrayTools.sort(possible_sites);
109 for (int i=0; possible_sites != null && i < possible_sites.length; i++) {
110 File site_config = new File(possible_sites[i], "siteConfig.xml");
111 if (site_config.exists()) {
112 this.sites.add(possible_sites[i].getName());
113 }
114 }
115 }
116
117 this.mappings = new HashMap();
118 Document web_config = XMLTools.parseXMLFile(web_xml.getAbsolutePath(), false);
119
120 NodeList servlet_mappings = web_config.getElementsByTagName("servlet-mapping");
121 // make a little map class
122 HashMap url_mappings = new HashMap();
123 for (int i=0; i<servlet_mappings.getLength(); i++) {
124 Element map = (Element)servlet_mappings.item(i);
125 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "servlet-name"));
126 String pattern = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "url-pattern"));
127 url_mappings.put(name, pattern);
128
129 }
130
131 NodeList servlets = web_config.getElementsByTagName("servlet");
132 for (int i=0; i<servlets.getLength(); i++) {
133 Element servlet = (Element)servlets.item(i);
134 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "servlet-name"));
135 String description = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "description"));
136 String site = getServletSite(servlet);
137
138 if (site != null) {
139 ArrayList this_site = (ArrayList)mappings.get(site);
140 if (this_site == null) {
141 this_site = new ArrayList();
142 }
143 String url = (String)url_mappings.get(name);
144 this_site.add(url);
145 mappings.put(site, this_site);
146 }
147
148 }
149
150 web_config = null;
151 url_mappings = null;
152 }
153
154 public ArrayList getSites() {
155 return this.sites;
156 }
157
158 public ArrayList getServletsForSite(String site) {
159 return (ArrayList)this.mappings.get(site);
160 }
161 public String getServletPath(String site) {
162
163 // for now, get the first one
164 ArrayList servlets = (ArrayList)mappings.get(site);
165 if(servlets == null) {
166 return null;
167 }
168 return (String)servlets.get(0);
169 }
170
171 private String getServletSite(Element servlet) {
172
173 NodeList params = servlet.getElementsByTagName("init-param");
174 if ( params == null || params.getLength() == 0) {
175 return null;
176 }
177 String site = null;
178 for (int i=0; i<params.getLength()&& site == null; i++) {
179 String p_name = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-name"));
180 if (p_name.equals("site_name")) {
181 site = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-value"));
182 }
183 }
184 return site;
185 }
186}
Note: See TracBrowser for help on using the repository browser.