Skip to content

Commit

Permalink
Fixes #3798 - Add PiranhaBuilder API (#3802)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jul 7, 2024
1 parent c8eaaaa commit b47ebbf
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 17 deletions.
44 changes: 44 additions & 0 deletions core/api/src/main/java/cloud/piranha/core/api/PiranhaBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.core.api;

/**
* The PiranhaBuilder API.
*
* @author Manfred Riem ([email protected])
* @param <T> the concrete Piranha type.
*/
public interface PiranhaBuilder<T> {

/**
* Build the Piranha instance.
*
* @return the Piranha instance.
*/
public T build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.dist.coreprofile;

import cloud.piranha.core.api.PiranhaBuilder;
import cloud.piranha.core.api.PiranhaConfiguration;
import cloud.piranha.core.api.WebApplicationExtension;
import java.io.File;
Expand All @@ -39,7 +40,7 @@
*
* @author Manfred Riem ([email protected])
*/
public class CoreProfilePiranhaBuilder {
public class CoreProfilePiranhaBuilder implements PiranhaBuilder<CoreProfilePiranha> {

/**
* Stores the logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.dist.microprofile;

import cloud.piranha.core.api.PiranhaBuilder;
import cloud.piranha.core.api.PiranhaConfiguration;
import cloud.piranha.core.api.WebApplicationExtension;
import java.io.File;
Expand All @@ -39,7 +40,7 @@
*
* @author Manfred Riem ([email protected])
*/
public class MicroProfilePiranhaBuilder {
public class MicroProfilePiranhaBuilder implements PiranhaBuilder<MicroProfilePiranha> {

/**
* Stores the logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.dist.platform;

import cloud.piranha.core.api.PiranhaBuilder;
import java.io.File;
import java.lang.System.Logger.Level;

Expand All @@ -41,7 +42,7 @@
* @author Manfred Riem ([email protected])
* @see cloud.piranha.dist.platform.PlatformPiranha
*/
public class PlatformPiranhaBuilder {
public class PlatformPiranhaBuilder implements PiranhaBuilder<PlatformPiranha> {

/**
* Stores the logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.dist.server;

import cloud.piranha.core.api.PiranhaBuilder;
import java.lang.System.Logger.Level;

import static javax.naming.Context.INITIAL_CONTEXT_FACTORY;
Expand All @@ -41,7 +42,7 @@
* @author Manfred Riem ([email protected])
* @see cloud.piranha.dist.server.ServerPiranha
*/
public class ServerPiranhaBuilder {
public class ServerPiranhaBuilder implements PiranhaBuilder<ServerPiranha> {

/**
* Stores the logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.dist.servlet;

import cloud.piranha.core.api.PiranhaBuilder;
import cloud.piranha.core.api.PiranhaConfiguration;
import cloud.piranha.core.api.WebApplicationExtension;
import java.io.File;
Expand All @@ -40,7 +41,7 @@
*
* @author Manfred Riem ([email protected])
*/
public class ServletPiranhaBuilder {
public class ServletPiranhaBuilder implements PiranhaBuilder<ServletPiranha> {

/**
* Stores the logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.dist.webprofile;

import cloud.piranha.core.api.PiranhaBuilder;
import cloud.piranha.core.api.PiranhaConfiguration;
import cloud.piranha.core.api.WebApplicationExtension;
import java.io.File;
Expand All @@ -39,7 +40,7 @@
*
* @author Manfred Riem ([email protected])
*/
public class WebProfilePiranhaBuilder {
public class WebProfilePiranhaBuilder implements PiranhaBuilder<WebProfilePiranha> {

/**
* Stores the logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package cloud.piranha.embedded;

import cloud.piranha.core.api.HttpSessionManager;
import cloud.piranha.core.api.PiranhaBuilder;
import cloud.piranha.core.api.WebApplication;
import cloud.piranha.core.api.WebApplicationExtension;
import cloud.piranha.core.impl.DefaultWebApplicationExtensionContext;
Expand All @@ -52,7 +53,7 @@
* @author Manfred Riem ([email protected])
* @see cloud.piranha.embedded.EmbeddedPiranha
*/
public class EmbeddedPiranhaBuilder {
public class EmbeddedPiranhaBuilder implements PiranhaBuilder<EmbeddedPiranha> {

/**
* Stores the async supported flags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package cloud.piranha.micro.builder;

import cloud.piranha.core.api.PiranhaBuilder;
import org.jboss.shrinkwrap.api.Archive;
import cloud.piranha.micro.loader.MicroConfiguration;
import cloud.piranha.micro.loader.MicroOuterDeployer;
Expand All @@ -36,7 +37,7 @@
*
* @author Arjan Tijms
*/
public class MicroEmbeddedPiranhaBuilder {
public class MicroEmbeddedPiranhaBuilder implements PiranhaBuilder<MicroEmbeddedPiranha> {

/**
* Object containing all configuration settings for Piranha Micro
Expand Down Expand Up @@ -71,13 +72,8 @@ public MicroEmbeddedPiranhaBuilder archive(Archive<?> archive) {
return this;
}

/**
* Builds an embedded Piranha Micro instance and deploys the archive set by
* this builder to it.
*
* @return the newly created Piranha Micro instance
*/
public MicroEmbeddedPiranha buildAndStart() {
@Override
public MicroEmbeddedPiranha build() {
MicroEmbeddedPiranha microEmbeddedPiranha = new MicroEmbeddedPiranha();
MicroWebApplication microWebApplication = microEmbeddedPiranha.getWebApplication();
if (configuration == null) {
Expand All @@ -86,25 +82,36 @@ public MicroEmbeddedPiranha buildAndStart() {
configuration.setHttpStart(false);
}

if (configuration.getContextPath()!= null) {
if (configuration.getContextPath() != null) {
// If an explicit root is set, use it. Otherwise use the default.
microWebApplication.setContextPath(configuration.getContextPath());
}

microWebApplication.setDeployedApplication(
new MicroOuterDeployer(configuration.postConstruct()).deploy(archive).getDeployedApplication());

return microEmbeddedPiranha;
}

/**
* Builds an embedded Piranha Micro instance and deploys the archive set by
* this builder to it.
*
* @return the newly created Piranha Micro instance
*/
public MicroEmbeddedPiranha buildAndStart() {
MicroEmbeddedPiranha microEmbeddedPiranha = build();
if (!configuration.isHttpStart()) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
MicroWebApplication microWebApplication = microEmbeddedPiranha.getWebApplication();
Thread.currentThread().setContextClassLoader(microWebApplication.getClassLoader());
microWebApplication.initialize();
microWebApplication.start();
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}

return microEmbeddedPiranha;
}
}

0 comments on commit b47ebbf

Please sign in to comment.