Ignore:
Timestamp:
04/17/12 13:06:10 (12 years ago)
Author:
oschonef
Message:
  • re-name some classes to better match to SRU specification components
    • SRUService -> SRUServer
    • SRUEndpointConfig -> SRUServerConfig
  • some more sanity checking in config parsing
  • some updates to java docs
File:
1 moved

Legend:

Unmodified
Added
Removed
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java

    r1883 r1889  
    1717package eu.clarin.sru.server;
    1818
    19 import java.io.InputStream;
     19import java.io.IOException;
    2020import java.net.URL;
    2121import java.util.ArrayList;
     
    3030import javax.xml.parsers.DocumentBuilder;
    3131import javax.xml.parsers.DocumentBuilderFactory;
     32import javax.xml.parsers.ParserConfigurationException;
    3233import javax.xml.transform.Source;
    3334import javax.xml.transform.dom.DOMSource;
     
    3738import javax.xml.xpath.XPath;
    3839import javax.xml.xpath.XPathConstants;
     40import javax.xml.xpath.XPathException;
    3941import javax.xml.xpath.XPathExpression;
    4042import javax.xml.xpath.XPathExpressionException;
     
    5254
    5355/**
    54  * Endpoint configuration. Most of the endpoint configuration is created from
    55  * the XML file.
     56 * SRU server configuration.
    5657 *
    57  * <p>Example:</p>
     58 * <p>
     59 * Example:
     60 * </p>
    5861 * <pre>
    59  * URL url = MySRUServlet.class.getClassLoader().getResource("META-INF/endpoint-config.xml");
     62 * URL url = MySRUServlet.class.getClassLoader()
     63 *               .getResource("META-INF/sru-server-config.xml");
    6064 * if (url == null) {
    61  *     throw new ServletException("not found, url == null");
     65 *     throw new ServletException(&quot;not found, url == null&quot;);
    6266 * }
    6367 *
     68 * // other runtime configuration, usually obtained from servlet context
    6469 * HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
    65  * SRUEndpointConfig config = SRUEndpointConfig.parse(params, url.openStream());
     70 * params.put(SRUServerConfig.SRU_TRANSPORT, "http");
     71 * params.put(SRUServerConfig.SRU_HOST, "127.0.0.1");
     72 * params.put(SRUServerConfig.SRU_PORT, "80");
     73 * params.put(SRUServerConfig.SRU_DATABASE, "sru-server");
     74 *
     75 * SRUServerConfig config = SRUServerConfig.parse(params, url);
    6676 * </pre>
    6777 *
    68  * <p>The XML configuration file must validate against the "endpoint-config.xsd"
    69  * schema bundled with the package and need to have the
     78 * <p>
     79 * The XML configuration file must validate against the "sru-server-config.xsd"
     80 * W3C schema bundled with the package and need to have the
    7081 * <code>http://www.clarin.eu/sru-server/1.0/</code> XML namespace.
    71  * </p> 
     82 * </p>
    7283 */
    73 public final class SRUEndpointConfig {
     84public final class SRUServerConfig {
    7485    public static final String SRU_TRANSPORT     = "sru.transport";
    7586    public static final String SRU_HOST          = "sru.host";
     
    7990    private static final String CONFIG_FILE_NAMESPACE_URI =
    8091            "http://www.clarin.eu/sru-server/1.0/";
     92    private static final String CONFIG_FILE_SCHEMA_URL =
     93            "META-INF/sru-server-config.xsd";
    8194
    8295    public static final class LocalizedString {
     
    413426
    414427
    415     private SRUEndpointConfig(String transport, String host, String port,
     428    private SRUServerConfig(String transport, String host, String port,
    416429            String database, boolean echoRequests, DatabaseInfo databaseinfo,
    417430            IndexInfo indexInfo, List<SchemaInfo> schemaInfo) {
     
    524537
    525538    /**
    526      * Parse the XML endpoint configuration.
     539     * Parse a SRU server XML configuration file and create an configuration
     540     * object from it.
    527541     *
    528542     * @param params
    529543     *            additional settings
    530      * @param in
    531      *            an {@link InputSource} to the XML configuration file
     544     * @param configFile
     545     *            an {@link URL} pointing to the XML configuration file
    532546     * @return a initialized <code>SRUEndpointConfig</code> instance
    533547     * @throws NullPointerException
    534      *             if <em>params</em> or <em>in</em> is <code>null</code>
     548     *             if <em>params</em> or <em>configFile</em> is
     549     *             <code>null</code>
    535550     * @throws SRUConfigException
    536      *             if an error occured
     551     *             if an error occurred
    537552     */
    538     public static SRUEndpointConfig parse(Map<String, String> params,
    539             InputStream in) throws SRUConfigException {
     553    public static SRUServerConfig parse(Map<String, String> params,
     554            URL configFile) throws SRUConfigException {
    540555        if (params == null) {
    541556            throw new NullPointerException("params == null");
    542557        }
    543         if (in == null) {
     558        if (configFile == null) {
    544559            throw new NullPointerException("in == null");
    545560        }
    546561        try {
    547             URL url = SRUEndpointConfig.class.getClassLoader()
    548                     .getResource("META-INF/endpoint-config.xsd");
     562            URL url = SRUServerConfig.class.getClassLoader()
     563                    .getResource(CONFIG_FILE_SCHEMA_URL);
    549564            if (url == null) {
    550                 throw new SRUConfigException("cannot open " +
    551                         "\"META-INF/endpoint-config.xsd\"");
     565                throw new SRUConfigException("cannot open \"" +
     566                        CONFIG_FILE_SCHEMA_URL + "\"");
    552567            }
    553568            SchemaFactory sfactory =
     
    564579            // parse input
    565580            DocumentBuilder builder = factory.newDocumentBuilder();
    566             InputSource input = new InputSource(in);
     581            InputSource input = new InputSource(configFile.openStream());
    567582            input.setPublicId(CONFIG_FILE_NAMESPACE_URI);
    568583            input.setSystemId(CONFIG_FILE_NAMESPACE_URI);
     
    654669                        "\" is mandatory");
    655670            }
     671            // sanity check
     672            try {
     673                int num = Integer.parseInt(port);
     674                if ((num < 1) && (num > 65535)) {
     675                    throw new SRUConfigException("parameter \"" + SRU_PORT +
     676                            "\" must be between 1 and 65535");
     677                }
     678            } catch (NumberFormatException e) {
     679                throw new SRUConfigException("parameter \"" + SRU_PORT +
     680                        "\" must be nummerical");
     681            }
    656682
    657683            String database = params.get(SRU_DATABASE);
     
    661687            }
    662688
     689            // cleanup: remove leading slashed
     690            while (database.startsWith("/")) {
     691                database = database.substring(1);
     692            }
     693
    663694            String s;
    664695            boolean echoRequests = false;
     
    667698            }
    668699
    669             return new SRUEndpointConfig(transport, host, port, database,
     700            return new SRUServerConfig(transport, host, port, database,
    670701                    echoRequests, databaseInfo, indexInfo,
    671702                    schemaInfo);
     703        } catch (IOException e) {
     704            throw new SRUConfigException("error reading configuration file", e);
     705        } catch (XPathException e) {
     706            throw new SRUConfigException("error parsing configuration file", e);
     707        } catch (ParserConfigurationException e) {
     708            throw new SRUConfigException("error parsing configuration file", e);
     709        } catch (SAXException e) {
     710            throw new SRUConfigException("error parsing configuration file", e);
    672711        } catch (SRUConfigException e) {
    673712            throw e;
    674         } catch (Exception e) {
    675             throw new SRUConfigException("error parsing config", e);
    676713        }
    677714    }
Note: See TracChangeset for help on using the changeset viewer.