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

Last change on this file since 13596 was 13596, checked in by mdewsnip, 17 years ago

Moved the ServletConfiguration class into the new greenstone3 package.

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