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

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

download web.xml and get names of sites if Gatherer.isGsdlRemote

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