All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.earldouglas.WebappComponentsConfiguration Maven / Gradle / Ivy

package com.earldouglas;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/** Specifies server settings and components locations for running a webapp in-place. */
public class WebappComponentsConfiguration {

  /** The hostname to use for the server, e.g. "localhost" */
  public final String hostname;

  /** The port to use for the server, e.g. 8080 */
  public final int port;

  /**
   * The context path to use for the webapp.
   *
   * 

For the root context path, use the empty string "". */ public final String contextPath; /** * An empty directory that Tomcat requires to run. Represents the resources directory, but it can * be any empty directory. */ public final File emptyWebappDir; /** * An empty directory that Tomcat requires to run. Represents the WEB-INF/classes directory, but * it can be any empty directory. */ public final File emptyClassesDir; /** * The map of resources to serve. * *

The mapping is from source to destination, where: * *

    *
  • source is the relative path within the webapp (e.g. index.html, WEB-INF/web.xml) *
  • destination is the file on disk to serve *
*/ public final Map resourceMap; /** * Read configuration from a file at the specified location. * * @param configurationFilename the configuration filename to load * @throws IOException if something goes wrong * @return WebappComponentsConfiguration a loaded configuration */ public static WebappComponentsConfiguration load(final String configurationFilename) throws IOException { return WebappComponentsConfiguration.load(new File(configurationFilename)); } private static Map parseResourceMap(final String raw) throws IOException { final Map resourceMap = new HashMap(); final String[] rows = raw.split(","); for (int rowIndex = 0; rowIndex < rows.length; rowIndex++) { final String[] columns = rows[rowIndex].split("->"); resourceMap.put(columns[0], new File(columns[1])); } return resourceMap; } /** * Read configuration from a file at the specified location. * *

The format of the file is a Properties file with the following fields: * *

    *
  • hostname *
  • port *
  • contextPath *
  • emptyWebappDir *
  • emptyClassesDir *
  • resourceMap *
* * The resourceMap field is a list, concatenated by commas, of source/destination pairs, delimited * by ->. * *

Example: * *

   * hostname=localhost
   * port=8989
   * contextPath=
   * emptyWebappDir=target/empty
   * emptyClassesDir=target/empty
   * resourceMap=bar.html->src/test/fakeproject/src/main/webapp/bar.html,\
   *             foo.html->src/test/fakeproject/src/main/webapp/foo.html,\
   *             baz/raz.css->src/test/fakeproject/src/main/webapp/baz/raz.css
   * 
* * @param configurationFile the configuration file to load * @throws IOException if something goes wrong * @return WebappComponentsConfiguration a loaded configuration */ public static WebappComponentsConfiguration load(final File configurationFile) throws IOException { final InputStream inputStream = new FileInputStream(configurationFile); final Properties properties = new Properties(); properties.load(inputStream); final Map resourceMap = parseResourceMap(properties.getProperty("resourceMap")); return new WebappComponentsConfiguration( properties.getProperty("hostname"), Integer.parseInt(properties.getProperty("port")), properties.getProperty("contextPath"), new File(properties.getProperty("emptyWebappDir")), new File(properties.getProperty("emptyClassesDir")), resourceMap); } /** * Construct a new configuration from the given parameters. * * @param hostname the hostname to use for the server, e.g. "localhost" * @param port the port to use for the server, e.g. 8080 * @param contextPath the context path to use for the webapp, e.g. "" * @param emptyWebappDir an empty directory that Tomcat requires to run * @param emptyClassesDir an empty directory that Tomcat requires to run * @param resourceMap the map of resources to serve */ public WebappComponentsConfiguration( final String hostname, final int port, final String contextPath, final File emptyWebappDir, final File emptyClassesDir, final Map resourceMap) { this.hostname = hostname; this.port = port; this.contextPath = contextPath; this.emptyWebappDir = emptyWebappDir; this.emptyClassesDir = emptyClassesDir; this.resourceMap = resourceMap; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy