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

juzu.impl.bridge.DescriptorBuilder Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright 2013 eXo Platform SAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package juzu.impl.bridge;

import juzu.bridge.portlet.JuzuPortlet;
import juzu.bridge.servlet.JuzuServlet;
import juzu.impl.asset.AssetServlet;
import juzu.impl.common.RunMode;
import juzu.impl.common.Tools;
import juzu.impl.inject.spi.InjectorProvider;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

/**
 * The bridge descriptor for a web archive.
 *
 * @author Julien Viet
 */
public class DescriptorBuilder {

  /** . */
  public static DescriptorBuilder DEFAULT = new DescriptorBuilder(
      InjectorProvider.GUICE,
      Tools.ISO_8859_1,
      RunMode.PROD,
      null,
      new String[0],
      new int[0],
      new String[0],
      new String[0],
      new String[0],
      new String[0],
      new String[0],
      new Integer[0],
      new Boolean[0],
      new String[0],
      new String[0]
  );

  /** . */
  private final RunMode runMode;

  /** . */
  private final String sourcePath;

  /** . */
  private final InjectorProvider injector;

  /** . */
  private final String[] applicationNames;

  /** . */
  private final int[] applicationTypes;

  /** . */
  private final String[] urlPatterns;

  /** . */
  private final Charset requestEncoding;

  /** . */
  private final String[] servletsName;

  /** . */
  private final String[] servletsUrlPattern;

  /** . */
  private final String[] servletsClass;

  /** . */
  private final Integer[] servletsLoadOnStartup;

  /** . */
  private final Boolean[] servletsAsync;

  /** . */
  private final String[] listenersClass;

  /** . */
  private final String[] resourcesEnvRefName;

  /** . */
  private final String[] resourcesEnvRefType;

  protected DescriptorBuilder(DescriptorBuilder other) {
    this(
        other.injector,
        other.requestEncoding,
        other.runMode,
        other.sourcePath,
        other.applicationNames,
        other.applicationTypes,
        other.urlPatterns,
        other.listenersClass,
        other.servletsName,
        other.servletsUrlPattern,
        other.servletsClass,
        other.servletsLoadOnStartup,
        other.servletsAsync,
        other.resourcesEnvRefName,
        other.resourcesEnvRefType
    );
  }

  private DescriptorBuilder(
      InjectorProvider injector,
      Charset requestEncoding,
      RunMode runMode,
      String sourcePath,
      String[] applicationNames,
      int[] applicationTypes,
      String[] urlPatterns,
      String[] listenersClass,
      String[] servletsName,
      String[] servletsUrlPattern,
      String[] servletsClass,
      Integer[] servletsLoadOnStartup,
      Boolean[] servletsAsync,
      String[] resourcesEnvRefName,
      String[] resourcesEnvRefType) {
    this.applicationNames = applicationNames;
    this.runMode = runMode;
    this.injector = injector;
    this.applicationTypes = applicationTypes;
    this.sourcePath = sourcePath;
    this.urlPatterns = urlPatterns;
    this.requestEncoding = requestEncoding;
    this.listenersClass = listenersClass;
    this.servletsName = servletsName;
    this.servletsUrlPattern = servletsUrlPattern;
    this.servletsClass = servletsClass;
    this.servletsLoadOnStartup = servletsLoadOnStartup;
    this.servletsAsync = servletsAsync;
    this.resourcesEnvRefName = resourcesEnvRefName;
    this.resourcesEnvRefType = resourcesEnvRefType;
  }

  public DescriptorBuilder runMode(RunMode runMode) {
    if (runMode == null) {
      throw new NullPointerException("No null run mode");
    }
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, listenersClass, servletsName, servletsUrlPattern, servletsClass, servletsLoadOnStartup, servletsAsync, resourcesEnvRefName, resourcesEnvRefType);
  }

  public DescriptorBuilder injector(InjectorProvider injector) {
    if (injector == null) {
      throw new NullPointerException("No null injector");
    }
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, listenersClass, servletsName, servletsUrlPattern, servletsClass, servletsLoadOnStartup, servletsAsync, resourcesEnvRefName, resourcesEnvRefType);
  }

  public DescriptorBuilder portletApp(String applicationName, String portletName) {
    return app(applicationName, 1, portletName);
  }

  public DescriptorBuilder servletApp(String applicationName) {
    return servletApp(applicationName, "/");
  }

  public DescriptorBuilder servletApp(String applicationName, String urlPattern) {
    if (urlPattern == null) {
      throw new NullPointerException("No null url pattern");
    }
    return app(applicationName, 0, urlPattern);
  }

  private DescriptorBuilder app(String applicationName, int applicationType, String urlPattern) {
    int[] tmp = Arrays.copyOf(applicationTypes, applicationTypes.length + 1);
    tmp[applicationTypes.length] = applicationType;
    return new DescriptorBuilder(
        injector,
        requestEncoding,
        runMode,
        sourcePath,
        Tools.appendTo(applicationNames, applicationName),
        tmp,
        Tools.appendTo(urlPatterns, urlPattern),
        listenersClass,
        servletsName,
        servletsUrlPattern,
        servletsClass,
        servletsLoadOnStartup,
        servletsAsync,
        resourcesEnvRefName,
        resourcesEnvRefType
    );
  }

  public DescriptorBuilder sourcePath(String sourcePath) {
    if (sourcePath == null) {
      throw new NullPointerException("No null source path");
    }
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, listenersClass, servletsName, servletsUrlPattern, servletsClass, servletsLoadOnStartup, servletsAsync, resourcesEnvRefName, resourcesEnvRefType);
  }

  public DescriptorBuilder requestEncoding(Charset requestEncoding) {
    if (requestEncoding == null) {
      throw new NullPointerException("No null request encoding");
    }
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, listenersClass, servletsName, servletsUrlPattern, servletsClass, servletsLoadOnStartup, servletsAsync, resourcesEnvRefName, resourcesEnvRefType);
  }

  public DescriptorBuilder listener(String listenerClass) {
    if (requestEncoding == null) {
      throw new NullPointerException("No null listener class");
    }
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, Tools.appendTo(listenersClass, listenerClass), servletsName, servletsUrlPattern, servletsClass, servletsLoadOnStartup, servletsAsync, resourcesEnvRefName, resourcesEnvRefType);
  }

  public DescriptorBuilder servlet(String servletName, String servletUrlPattern, String servletClass, Integer servletLoadOnStartup, Boolean servletAsync) {
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, listenersClass, Tools.appendTo(servletsName, servletName), Tools.appendTo(servletsUrlPattern, servletUrlPattern), Tools.appendTo(servletsClass, servletClass), Tools.appendTo(servletsLoadOnStartup, servletLoadOnStartup), Tools.appendTo(servletsAsync, servletAsync), resourcesEnvRefName, resourcesEnvRefType);
  }

  public DescriptorBuilder embedPortletContainer() {
    return servlet("EmbedServlet", "/embed/*", "org.gatein.pc.embed.EmbedServlet", 0, null);
  }

  public DescriptorBuilder resourceEnvRef(String name, String type) {
    return new DescriptorBuilder(injector, requestEncoding, runMode, sourcePath, applicationNames, applicationTypes, urlPatterns, listenersClass, servletsName, servletsUrlPattern, servletsClass,servletsLoadOnStartup, servletsAsync, Tools.appendTo(resourcesEnvRefName, name), Tools.appendTo(resourcesEnvRefType, type));
  }

  public Iterable getApplications() {
    return Tools.iterable(applicationNames);
  }

  public String getURLPattern() {
    return urlPatterns.length > 0 ? urlPatterns[0] : null;
  }

  public RunMode getRunMode() {
    return runMode;
  }

  public InjectorProvider getInjector() {
    return injector;
  }

  public Charset getRequestEncoding() {
    return requestEncoding;
  }

  private void appendContextParam(StringBuilder buffer, String paramName, String paramValue) {
    buffer.append("\n");
    buffer.append("").append(paramName).append("");
    buffer.append("").append(paramValue).append("");
    buffer.append("\n");
  }

  private void appendServlet(StringBuilder buffer, Map initParams, String servletName, String servletClass, Integer loadOnStartup, Boolean async) {
    buffer.append("");
    buffer.append("").append(servletName).append("");
    buffer.append("").append(servletClass).append("");
    if (initParams.size() > 0) {
      for (Map.Entry initParam : initParams.entrySet()) {
        buffer.append("");
        buffer.append("").append(initParam.getKey()).append("");
        buffer.append("").append(initParam.getValue()).append("");
        buffer.append("");
      }
    }
    if (loadOnStartup != null) {
      buffer.append("").append(loadOnStartup).append("");
    }
    if (async != null) {
      buffer.append("").append(async).append("");
    }
    buffer.append("");
  }

  private void appendMapping(StringBuilder buffer, String servletName, String urlPattern) {
    buffer.append("");
    buffer.append("").append(servletName).append("");
    buffer.append("").append(urlPattern).append("");
    buffer.append("");
  }

  private void appendListener(StringBuilder buffer, String listenerClass) {
    buffer.append("");
    buffer.append("").append(listenerClass).append("");
    buffer.append("");
  }

  protected void appendWebXmlHeader(StringBuilder buffer) {
    buffer.append("");
    buffer.append("\n");
  }

  protected void appendWebXmlFooter(StringBuilder buffer) {
    buffer.append("");
  }

  public String toWebXml() {

    StringBuilder buffer = new StringBuilder();
    appendWebXmlHeader(buffer);
    appendContextParam(buffer, "juzu.run_mode", runMode.getValue());
    appendContextParam(buffer, "juzu.inject", injector.getValue());
    appendContextParam(buffer, "juzu.request_encoding", requestEncoding.name());
    if (sourcePath != null) {
      appendContextParam(buffer, "juzu.src_path", sourcePath);
    }
    for (String listenerClass : listenersClass) {
      appendListener(buffer, listenerClass);
    }

    //
    for (int i = 0;i < applicationNames.length;i++) {
      if (applicationTypes[i] == 0) {
        appendServlet(
            buffer,
            Collections.singletonMap(BridgeConfig.APP_NAME, applicationNames[i]),
            "JuzuServlet-" + i,
            JuzuServlet.class.getName(),
            null,
            true);
        appendMapping(buffer, "JuzuServlet-" + i, urlPatterns[i]);
      }
    }

    //
    for (int i = 0;i < servletsName.length;i++) {
      appendServlet(buffer, Collections.emptyMap(), servletsName[i], servletsClass[i], servletsLoadOnStartup[i], servletsAsync[i]);
      appendMapping(buffer, servletsName[i], servletsUrlPattern[i]);
    }

    //
    appendServlet(buffer, Collections.emptyMap(), "AssetServlet", AssetServlet.class.getName(), 0, null);
    appendMapping(buffer, "AssetServlet", "/assets/*");

    //
    if (resourcesEnvRefName.length > 0) {
      for (int i = 0;i < resourcesEnvRefName.length;i++) {
        buffer.append("");
        buffer.append("").append(resourcesEnvRefName[i]).append("");
        buffer.append("").append(resourcesEnvRefType[i]).append("");
        buffer.append("");
      }
    }

    //
    appendWebXmlFooter(buffer);

    //
    return buffer.toString();
  }

  public String toPortletXml() {
    StringBuilder buffer = new StringBuilder();
    buffer.append("\n" +
        "");
    for (int i = 0;i < applicationNames.length;i++) {
      if (applicationTypes[i] == 1) {
        buffer.append("");
        buffer.append("").append(urlPatterns[i]).append("");
        buffer.append("").append(urlPatterns[i]).append("");
        buffer.append("").append(JuzuPortlet.class.getName()).append("");
        buffer.append("");
        buffer.append("").append(BridgeConfig.APP_NAME).append("");
        buffer.append("").append(applicationNames[i]).append("");
        buffer.append("");
        buffer.append("");
        buffer.append("text/html");
        buffer.append("");
        buffer.append("");
        buffer.append("").append(urlPatterns[i]).append(" Application");
        buffer.append("");
        buffer.append("");
      }
    }
    buffer.append("");
    return buffer.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy