PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/TestableJettyServer.java

https://github.com/dekellum/jetty
Java | 209 lines | 151 code | 33 blank | 25 comment | 13 complexity | c54ede9835b63f9b770fc70d0f6a0a38 MD5 | raw file
  1. // ========================================================================
  2. // Copyright (c) Webtide LLC
  3. // ------------------------------------------------------------------------
  4. // All rights reserved. This program and the accompanying materials
  5. // are made available under the terms of the Eclipse Public License v1.0
  6. // and Apache License v2.0 which accompanies this distribution.
  7. //
  8. // The Eclipse Public License is available at
  9. // http://www.eclipse.org/legal/epl-v10.html
  10. //
  11. // The Apache License v2.0 is available at
  12. // http://www.apache.org/licenses/LICENSE-2.0.txt
  13. //
  14. // You may elect to redistribute this code under either of these licenses.
  15. // ========================================================================
  16. package org.eclipse.jetty.test.support;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.net.InetAddress;
  21. import java.net.MalformedURLException;
  22. import java.net.URI;
  23. import java.net.URL;
  24. import java.net.UnknownHostException;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. import org.eclipse.jetty.http.HttpSchemes;
  31. import org.eclipse.jetty.server.Connector;
  32. import org.eclipse.jetty.server.Server;
  33. import org.eclipse.jetty.xml.XmlConfiguration;
  34. import org.junit.Assert;
  35. import org.junit.Ignore;
  36. /**
  37. * Allows for setting up a Jetty server for testing based on XML configuration files.
  38. */
  39. @Ignore
  40. public class TestableJettyServer
  41. {
  42. private List<URL> _xmlConfigurations;
  43. private final Map<String,String> _properties = new HashMap<String, String>();
  44. private Server _server;
  45. private int _serverPort;
  46. private String _scheme = HttpSchemes.HTTP;
  47. /* Popular Directories */
  48. private File baseDir;
  49. private File testResourcesDir;
  50. public TestableJettyServer() throws IOException
  51. {
  52. _xmlConfigurations = new ArrayList<URL>();
  53. Properties properties = new Properties();
  54. /* Establish Popular Directories */
  55. String baseDirPath = System.getProperty("basedir");
  56. if (baseDirPath == null)
  57. {
  58. baseDirPath = System.getProperty("user.dir",".");
  59. }
  60. baseDir = new File(baseDirPath);
  61. properties.setProperty("test.basedir",baseDir.getAbsolutePath());
  62. testResourcesDir = new File(baseDirPath,"src/test/resources".replace('/',File.separatorChar));
  63. properties.setProperty("test.resourcesdir",testResourcesDir.getAbsolutePath());
  64. File testDocRoot = new File(testResourcesDir,"docroots");
  65. properties.setProperty("test.docroot.base",testDocRoot.getAbsolutePath());
  66. File targetDir = new File(baseDir,"target");
  67. properties.setProperty("test.targetdir",targetDir.getAbsolutePath());
  68. File webappsDir = new File(targetDir,"webapps");
  69. properties.setProperty("test.webapps",webappsDir.getAbsolutePath());
  70. // Write out configuration for use by ConfigurationManager.
  71. File testConfig = new File(targetDir,"testable-jetty-server-config.properties");
  72. FileOutputStream out = new FileOutputStream(testConfig);
  73. properties.store(out,"Generated by " + TestableJettyServer.class.getName());
  74. for (Object key:properties.keySet())
  75. _properties.put(String.valueOf(key),String.valueOf(properties.get(key)));
  76. }
  77. public void addConfiguration(URL xmlConfig)
  78. {
  79. _xmlConfigurations.add(xmlConfig);
  80. }
  81. public void addConfiguration(File xmlConfigFile) throws MalformedURLException
  82. {
  83. _xmlConfigurations.add(xmlConfigFile.toURI().toURL());
  84. }
  85. public void addConfiguration(String testConfigName) throws MalformedURLException
  86. {
  87. addConfiguration(new File(testResourcesDir,testConfigName));
  88. }
  89. public void setProperty(String key, String value)
  90. {
  91. _properties.put(key,value);
  92. }
  93. public void load() throws Exception
  94. {
  95. XmlConfiguration last = null;
  96. Object[] obj = new Object[this._xmlConfigurations.size()];
  97. // Configure everything
  98. for (int i = 0; i < this._xmlConfigurations.size(); i++)
  99. {
  100. URL configURL = this._xmlConfigurations.get(i);
  101. XmlConfiguration configuration = new XmlConfiguration(configURL);
  102. if (last != null)
  103. {
  104. configuration.getIdMap().putAll(last.getIdMap());
  105. }
  106. configuration.getProperties().putAll(_properties);
  107. obj[i] = configuration.configure();
  108. last = configuration;
  109. }
  110. // Test for Server Instance.
  111. Server foundServer = null;
  112. int serverCount = 0;
  113. for (int i = 0; i < this._xmlConfigurations.size(); i++)
  114. {
  115. if (obj[i] instanceof Server)
  116. {
  117. if (obj[i].equals(foundServer))
  118. {
  119. // Identical server instance found
  120. break;
  121. }
  122. foundServer = (Server)obj[i];
  123. serverCount++;
  124. }
  125. }
  126. if (serverCount <= 0)
  127. {
  128. throw new Exception("Load failed to configure a " + Server.class.getName());
  129. }
  130. Assert.assertEquals("Server load count",1,serverCount);
  131. this._server = foundServer;
  132. this._server.setGracefulShutdown(10);
  133. }
  134. public String getScheme()
  135. {
  136. return _scheme;
  137. }
  138. public void setScheme(String scheme)
  139. {
  140. this._scheme = scheme;
  141. }
  142. public void start() throws Exception
  143. {
  144. Assert.assertNotNull("Server should not be null (failed load?)",_server);
  145. _server.start();
  146. // Find the active server port.
  147. this._serverPort = (-1);
  148. Connector connectors[] = _server.getConnectors();
  149. for (int i = 0; i < connectors.length; i++)
  150. {
  151. Connector connector = connectors[i];
  152. if (connector.getLocalPort() > 0)
  153. {
  154. this._serverPort = connector.getLocalPort();
  155. break;
  156. }
  157. }
  158. Assert.assertTrue("Server Port is between 1 and 65535. Actually <" + _serverPort + ">",(1 <= this._serverPort) && (this._serverPort <= 65535));
  159. }
  160. public int getServerPort()
  161. {
  162. return _serverPort;
  163. }
  164. public void stop() throws Exception
  165. {
  166. _server.stop();
  167. }
  168. public URI getServerURI() throws UnknownHostException
  169. {
  170. StringBuffer uri = new StringBuffer();
  171. uri.append(this._scheme).append("://");
  172. uri.append(InetAddress.getLocalHost().getHostAddress());
  173. uri.append(":").append(this._serverPort);
  174. return URI.create(uri.toString());
  175. }
  176. }