- Timestamp:
- 04/17/12 13:06:10 (12 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java
r1883 r1889 17 17 package eu.clarin.sru.server; 18 18 19 import java.io.I nputStream;19 import java.io.IOException; 20 20 import java.net.URL; 21 21 import java.util.ArrayList; … … 30 30 import javax.xml.parsers.DocumentBuilder; 31 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 32 33 import javax.xml.transform.Source; 33 34 import javax.xml.transform.dom.DOMSource; … … 37 38 import javax.xml.xpath.XPath; 38 39 import javax.xml.xpath.XPathConstants; 40 import javax.xml.xpath.XPathException; 39 41 import javax.xml.xpath.XPathExpression; 40 42 import javax.xml.xpath.XPathExpressionException; … … 52 54 53 55 /** 54 * Endpoint configuration. Most of the endpoint configuration is created from 55 * the XML file. 56 * SRU server configuration. 56 57 * 57 * <p>Example:</p> 58 * <p> 59 * Example: 60 * </p> 58 61 * <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"); 60 64 * if (url == null) { 61 * throw new ServletException( "not found, url == null");65 * throw new ServletException("not found, url == null"); 62 66 * } 63 67 * 68 * // other runtime configuration, usually obtained from servlet context 64 69 * HashMap<String, String> params = new HashMap<String, String>(); 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); 66 76 * </pre> 67 77 * 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 70 81 * <code>http://www.clarin.eu/sru-server/1.0/</code> XML namespace. 71 * </p> 82 * </p> 72 83 */ 73 public final class SRU EndpointConfig {84 public final class SRUServerConfig { 74 85 public static final String SRU_TRANSPORT = "sru.transport"; 75 86 public static final String SRU_HOST = "sru.host"; … … 79 90 private static final String CONFIG_FILE_NAMESPACE_URI = 80 91 "http://www.clarin.eu/sru-server/1.0/"; 92 private static final String CONFIG_FILE_SCHEMA_URL = 93 "META-INF/sru-server-config.xsd"; 81 94 82 95 public static final class LocalizedString { … … 413 426 414 427 415 private SRU EndpointConfig(String transport, String host, String port,428 private SRUServerConfig(String transport, String host, String port, 416 429 String database, boolean echoRequests, DatabaseInfo databaseinfo, 417 430 IndexInfo indexInfo, List<SchemaInfo> schemaInfo) { … … 524 537 525 538 /** 526 * Parse the XML endpoint configuration. 539 * Parse a SRU server XML configuration file and create an configuration 540 * object from it. 527 541 * 528 542 * @param params 529 543 * additional settings 530 * @param in531 * an {@link InputSource}to the XML configuration file544 * @param configFile 545 * an {@link URL} pointing to the XML configuration file 532 546 * @return a initialized <code>SRUEndpointConfig</code> instance 533 547 * @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> 535 550 * @throws SRUConfigException 536 * if an error occur ed551 * if an error occurred 537 552 */ 538 public static SRU EndpointConfig parse(Map<String, String> params,539 InputStream in) throws SRUConfigException {553 public static SRUServerConfig parse(Map<String, String> params, 554 URL configFile) throws SRUConfigException { 540 555 if (params == null) { 541 556 throw new NullPointerException("params == null"); 542 557 } 543 if ( in== null) {558 if (configFile == null) { 544 559 throw new NullPointerException("in == null"); 545 560 } 546 561 try { 547 URL url = SRU EndpointConfig.class.getClassLoader()548 .getResource( "META-INF/endpoint-config.xsd");562 URL url = SRUServerConfig.class.getClassLoader() 563 .getResource(CONFIG_FILE_SCHEMA_URL); 549 564 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 + "\""); 552 567 } 553 568 SchemaFactory sfactory = … … 564 579 // parse input 565 580 DocumentBuilder builder = factory.newDocumentBuilder(); 566 InputSource input = new InputSource( in);581 InputSource input = new InputSource(configFile.openStream()); 567 582 input.setPublicId(CONFIG_FILE_NAMESPACE_URI); 568 583 input.setSystemId(CONFIG_FILE_NAMESPACE_URI); … … 654 669 "\" is mandatory"); 655 670 } 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 } 656 682 657 683 String database = params.get(SRU_DATABASE); … … 661 687 } 662 688 689 // cleanup: remove leading slashed 690 while (database.startsWith("/")) { 691 database = database.substring(1); 692 } 693 663 694 String s; 664 695 boolean echoRequests = false; … … 667 698 } 668 699 669 return new SRU EndpointConfig(transport, host, port, database,700 return new SRUServerConfig(transport, host, port, database, 670 701 echoRequests, databaseInfo, indexInfo, 671 702 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); 672 711 } catch (SRUConfigException e) { 673 712 throw e; 674 } catch (Exception e) {675 throw new SRUConfigException("error parsing config", e);676 713 } 677 714 }
Note: See TracChangeset
for help on using the changeset viewer.