001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.lib;
025:
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.Map;
031: import org.jdom.Document;
032: import org.jdom.Element;
033: import org.jdom.JDOMException;
034:
035: //=============================================================================
036: /** Embedde Servlet Container lib */
037:
038: public class EmbeddedSCLib {
039: //---------------------------------------------------------------------------
040: //---
041: //--- Constructor
042: //---
043: //---------------------------------------------------------------------------
044:
045: public EmbeddedSCLib(String appPath) throws JDOMException,
046: IOException {
047: this .appPath = appPath;
048:
049: jetty = Lib.xml.load(appPath + JETTY_FILE);
050: webXml = Lib.xml.load(appPath + WEBXML_FILE);
051:
052: //--- retrieve 'host', 'port' and 'servlet' parameters from jetty
053:
054: for (Object call : jetty.getRootElement().getChildren("Call")) {
055: Element elCall = (Element) call;
056:
057: if ("addListener".equals(elCall.getAttributeValue("name"))) {
058: Element elNew = elCall.getChild("Arg").getChild("New");
059:
060: for (Object set : elNew.getChildren("Set")) {
061: Element elSet = (Element) set;
062:
063: if ("host".equals(elSet.getAttributeValue("name")))
064: hostElem = elSet;
065:
066: else if ("port".equals(elSet
067: .getAttributeValue("name")))
068: portElem = elSet;
069: }
070: }
071:
072: else if ("addWebApplication".equals(elCall
073: .getAttributeValue("name"))) {
074: if (servletElem == null)
075: servletElem = elCall.getChild("Arg");
076: }
077: }
078: }
079:
080: //---------------------------------------------------------------------------
081: //---
082: //--- API methods
083: //---
084: //---------------------------------------------------------------------------
085:
086: public String getHost() {
087: return (hostElem == null) ? null : hostElem.getText();
088: }
089:
090: //---------------------------------------------------------------------------
091:
092: public String getPort() {
093: return (portElem == null) ? null : portElem.getText();
094: }
095:
096: //---------------------------------------------------------------------------
097:
098: public String getServlet() {
099: //--- we have to skip the initial '/'
100: return (servletElem == null) ? null : servletElem.getText()
101: .substring(1);
102: }
103:
104: //---------------------------------------------------------------------------
105:
106: public void setHost(String host) {
107: }
108:
109: //---------------------------------------------------------------------------
110:
111: public void setPort(String port) {
112: if (portElem != null)
113: portElem.setText(port);
114: }
115:
116: //---------------------------------------------------------------------------
117:
118: public void setServlet(String name) {
119: if (servletElem != null)
120: servletElem.setText("/" + name);
121:
122: for (Object e : webXml.getRootElement().getChildren()) {
123: Element elem = (Element) e;
124:
125: if (elem.getName().equals("display-name")) {
126: elem.setText(name);
127: return;
128: }
129: }
130: }
131:
132: //---------------------------------------------------------------------------
133:
134: public void save() throws FileNotFoundException, IOException {
135: Lib.xml.save(appPath + JETTY_FILE, jetty);
136: Lib.xml.save(appPath + WEBXML_FILE, webXml);
137:
138: //--- create proper index.html file to point to correct servlet
139:
140: Map<String, String> vars = new HashMap<String, String>();
141: vars.put("$SERVLET", getServlet());
142:
143: List<String> lines = Lib.text.load(appPath + INDEX_SRC_FILE);
144: Lib.text.replace(lines, vars);
145: Lib.text.save(appPath + INDEX_DES_FILE, lines);
146: }
147:
148: //---------------------------------------------------------------------------
149: //---
150: //--- Variables
151: //---
152: //---------------------------------------------------------------------------
153:
154: private String appPath;
155: private Document jetty;
156: private Element hostElem;
157: private Element portElem;
158: private Element servletElem;
159: private Document webXml;
160:
161: private static final String JETTY_FILE = "/bin/jetty.xml";
162: private static final String WEBXML_FILE = "/web/geonetwork/WEB-INF/web.xml";
163:
164: private static final String INDEX_SRC_FILE = "/gast/data/index.html";
165: private static final String INDEX_DES_FILE = "/web/geonetwork/index.html";
166: }
167:
168: //=============================================================================
|