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

Last change on this file since 37412 was 37412, checked in by davidb, 14 months ago

Whitespace alignment

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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 site_to_servlet_list_map = null;
66 private HashMap servlet_to_site_map = null;
67
68 public ServletConfiguration(String gsdl3_path) {
69
70 this.gsdl3_path = gsdl3_path;
71
72 if (Gatherer.isGsdlRemote){
73 if (Gatherer.remoteGreenstoneServer.downloadWebXMLFile().equals("")) {
74 System.err.println("ServletConfiguration Error: Could not download web.xml.");
75 System.exit(0);
76 }
77 }
78 //String web_xml_path = gsdl3_path + File.separator + "web" + File.separator + "WEB-INF"+ File.separator + "web.xml";
79 File web_xml;
80 if (Gatherer.isGsdlRemote){
81 web_xml = new File(gsdl3_path + "web.xml");
82 }else{
83 web_xml = new File(gsdl3_path + "WEB-INF"+ File.separator + "web.xml");
84 }
85 if (!web_xml.exists()) {
86 DebugStream.println("Error: no web.xml found at "+web_xml.toString());
87 return;
88 }
89
90 this.sites = new ArrayList();
91 if (Gatherer.isGsdlRemote){
92 String sites_on_server = Gatherer.remoteGreenstoneServer.getSiteNames();
93 if (sites_on_server.equals("")) {
94 // !! Something went wrong : could not get names of the sites
95 System.err.println("ServletConfiguration Error: Could not get names of the sites.");
96 System.exit(0);
97 }
98
99 String[] sites_arr=sites_on_server.split("-----");
100 for (int i=0; i<sites_arr.length; i++){
101 if (!(sites_arr[i].trim().equals(""))){
102 this.sites.add(sites_arr[i].trim());
103 }
104 }
105 }else{
106 // find the sites
107 File start = new File(Utility.getSitesDir(gsdl3_path));
108 File possible_sites[] = start.listFiles();
109 ArrayTools.sort(possible_sites);
110 for (int i=0; possible_sites != null && i < possible_sites.length; i++) {
111 File site_config = new File(possible_sites[i], "siteConfig.xml");
112 if (site_config.exists()) {
113 this.sites.add(possible_sites[i].getName());
114 }
115 }
116 }
117
118 this.site_to_servlet_list_map = new HashMap();
119 this.servlet_to_site_map = new HashMap();
120 Document web_config = XMLTools.parseXMLFile(web_xml.getAbsolutePath(), false);
121
122 NodeList servlet_mappings = web_config.getElementsByTagName("servlet-mapping");
123 // make a little map class
124 HashMap url_mappings = new HashMap();
125 for (int i=0; i<servlet_mappings.getLength(); i++) {
126 Element map = (Element)servlet_mappings.item(i);
127 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "servlet-name"));
128 String pattern = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "url-pattern"));
129 url_mappings.put(name, pattern);
130
131 }
132
133 NodeList servlets = web_config.getElementsByTagName("servlet");
134 for (int i=0; i<servlets.getLength(); i++) {
135 Element servlet = (Element)servlets.item(i);
136 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "servlet-name"));
137 String description = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "description"));
138 String site = getServletSite(servlet);
139 if (site != null) {
140 ArrayList this_site = (ArrayList)site_to_servlet_list_map.get(site);
141 if (this_site == null) {
142 this_site = new ArrayList();
143 }
144 String url = (String)url_mappings.get(name);
145 if (url != null) { // if its null, there was no mapping for the servlet
146 if(url.endsWith("/*")) { // truncate trailing /*, e.g. /library/* becomes /library
147 url = url.substring(0, url.length()-2);
148 }
149 this_site.add(url);
150 this.site_to_servlet_list_map.put(site, this_site);
151 this.servlet_to_site_map.put(url, site);
152 }
153 }
154
155 }
156
157 web_config = null;
158 url_mappings = null;
159 }
160
161 public ArrayList getSites() {
162 return this.sites;
163 }
164
165 public ArrayList getServletsForSite(String site) {
166 return (ArrayList)this.site_to_servlet_list_map.get(site);
167 }
168 public String getServletPath(String site) {
169
170 // for now, get the first one
171 ArrayList servlets = (ArrayList)this.site_to_servlet_list_map.get(site);
172 if(servlets == null) {
173 return null;
174 }
175 return (String)servlets.get(0);
176 }
177
178 public String getSiteForServlet(String servlet) {
179 return (String)this.servlet_to_site_map.get(servlet);
180 }
181
182 private String getServletSite(Element servlet) {
183
184 NodeList params = servlet.getElementsByTagName("init-param");
185 if ( params == null || params.getLength() == 0) {
186 return null;
187 }
188 String site = null;
189 for (int i=0; i<params.getLength()&& site == null; i++) {
190 String p_name = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-name"));
191 if (p_name.equals("site_name")) {
192 site = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-value"));
193 }
194 }
195 return site;
196 }
197}
Note: See TracBrowser for help on using the repository browser.