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.geonet.services.util.z3950;
025:
026: import com.k_int.z3950.server.ZServer;
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.util.Properties;
030: import jeeves.constants.Jeeves;
031: import jeeves.resources.dbms.Dbms;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.Xml;
034: import org.fao.geonet.GeonetContext;
035: import org.fao.geonet.constants.Geonet;
036: import org.fao.geonet.kernel.setting.SettingManager;
037: import org.jdom.Document;
038: import org.jdom.Element;
039:
040: //=============================================================================
041:
042: /** Z3950 server
043: */
044:
045: public class Server {
046: private static ZServer _server;
047:
048: //--------------------------------------------------------------------------
049:
050: /** initializes the server
051: */
052: public static void init(String host, String port, String appPath,
053: String schemaMappings, ServiceContext context) {
054: try {
055: //--- fix schema-mappings.xml file
056:
057: String tempSchema = appPath + Jeeves.Path.XML
058: + schemaMappings + ".tem";
059: String realSchema = appPath + Jeeves.Path.XML
060: + schemaMappings;
061:
062: fixSchemaFile(tempSchema, realSchema);
063:
064: //--- fix repositories.xml file
065:
066: String tempRepo = appPath + Jeeves.Path.XML
067: + "repositories.xml" + ".tem";
068: String realRepo = appPath + Jeeves.Path.XML
069: + "repositories.xml";
070:
071: fixRepositoriesFile(tempRepo, realRepo, host, port);
072:
073: //--- normal processing
074:
075: String evaluator = "org.fao.geonet.services.util.z3950.GNSearchable";
076: String configurator = "com.k_int.IR.Syntaxes.Conversion.XMLConfigurator";
077:
078: Properties props = new Properties();
079: props.setProperty("port", port);
080: props.setProperty("evaluator", evaluator);
081: props.setProperty("XSLConverterConfiguratorClassName",
082: configurator);
083: props.setProperty("ConvertorConfigFile", realSchema);
084: props.put("srvContext", context);
085:
086: _server = new ZServer(props);
087: _server.start();
088: } catch (Exception e) {
089: //--- Z39.50 must start even if there are problems
090: context.warning("Cannot start Z39.50 server : "
091: + e.getMessage());
092: }
093: }
094:
095: //--------------------------------------------------------------------------
096:
097: /** ends the server
098: */
099: public static void end() {
100: if (_server != null)
101: _server.shutdown(0); // shutdown type is not used in current implementation
102: }
103:
104: //--------------------------------------------------------------------------
105: //---
106: //--- Private methods
107: //---
108: //--------------------------------------------------------------------------
109:
110: private static void fixSchemaFile(String src, String des)
111: throws Exception {
112: Element root = Xml.loadFile(src);
113: Element temSrc = root.getChild("templatesource");
114:
115: String dir = new File(src).getParentFile().getAbsolutePath()
116: + "/mappings";
117:
118: temSrc.setAttribute("directory", dir);
119:
120: FileOutputStream os = new FileOutputStream(des);
121: Xml.writeResponse(new Document(root), os);
122: os.close();
123: }
124:
125: //--------------------------------------------------------------------------
126:
127: private static void fixRepositoriesFile(String src, String des,
128: String host, String z3950port) throws Exception {
129: Element root = Xml.loadFile(src);
130: Element repo = root.getChild("Repository");
131:
132: for (Object o : repo.getChildren("RepositoryProperty")) {
133: Element rp = (Element) o;
134: String name = rp.getAttributeValue("name");
135:
136: if ("ServiceHost".equals(name))
137: rp.setAttribute("value", host);
138:
139: else if ("ServicePort".equals(name))
140: rp.setAttribute("value", z3950port);
141: }
142:
143: FileOutputStream os = new FileOutputStream(des);
144: Xml.writeResponse(new Document(root), os);
145: os.close();
146: }
147: }
148:
149: //=============================================================================
|