Skip to content

Commit

Permalink
Fixes #4190 - Add ability to disable Tyrus (#4238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Nov 22, 2024
1 parent 6975429 commit de1fcda
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 9 deletions.
2 changes: 1 addition & 1 deletion extension/scinitializer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
<id>default</id>
<url>file:///tmp/piranha/extension/scinitializer</url>
</site>
</distributionManagement>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
*/
public class ServletContainerInitializerExtension implements WebApplicationExtension {

/**
* Stores the ignore initializers property.
*/
public static final String IGNORE_INITIALIZERS_PROPERTY
= ServletContainerInitializerExtension.class.getName() + ".ignoreInitializers";

/**
* Stores the logger.
*/
Expand Down Expand Up @@ -96,6 +102,15 @@ public void configure(WebApplication webApplication) {
"cloud.piranha.extension.scinitializer.ServletContainerInitializerExtensionp.enabled",
"true"))) {

if (System.getProperty(IGNORE_INITIALIZERS_PROPERTY) != null) {
String[] initializers = System.getProperty(IGNORE_INITIALIZERS_PROPERTY).split(",");
if (initializers.length > 0) {
for(int i=0; i<initializers.length; i++) {
ignoreInitializers.add(initializers[i]);
}
}
}

ServiceLoader<ServletContainerInitializer> serviceLoader = ServiceLoader.load(
ServletContainerInitializer.class, webApplication.getClassLoader());

Expand Down
46 changes: 42 additions & 4 deletions extension/tyrus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,56 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-container-servlet</artifactId>
<scope>compile</scope>
</dependency>
<!-- provided -->
<dependency>
<groupId>cloud.piranha.extension</groupId>
<artifactId>piranha-extension-scinitializer</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- runtime -->
<!-- test -->
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-container-servlet</artifactId>
<scope>runtime</scope>
<groupId>cloud.piranha.core</groupId>
<artifactId>piranha-core-impl</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<site>
<id>default</id>
<url>file:///tmp/piranha/extension/tyrus</url>
</site>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import cloud.piranha.core.api.WebApplication;
import cloud.piranha.core.api.WebApplicationExtension;
import java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.TRACE;
import org.glassfish.tyrus.servlet.TyrusServletContainerInitializer;

/**
* The extension that delivers Tyrus to Piranha.
Expand All @@ -39,18 +41,41 @@
*/
public class TyrusExtension implements WebApplicationExtension {

/**
* Stores the property used for enabling/disabling Tyrus.
*/
public static final String TYRUS_ENABLED_PROPERTY
= "cloud.piranha.extension.tyrus.TyrusExtension.enabled";

/**
* Stores the property used for ignore initializers.
*/
private static final String IGNORE_INITIALIZER_PROPERTY
= "cloud.piranha.extension.scinitializer.ServletContainerInitializerExtension.ignoreInitializers";

/**
* Stores the logger.
*/
private static final Logger LOGGER = System.getLogger(TyrusExtension.class.getName());

/**
* Configure the extension.
*
*
* @param webApplication the web application.
*/
@Override
public void configure(WebApplication webApplication) {
LOGGER.log(TRACE, "Configuring Eclipse Tyrus extension");
LOGGER.log(DEBUG, "Configuring Tyrus extension");

if (!Boolean.parseBoolean(System.getProperty(TYRUS_ENABLED_PROPERTY, "true"))) {
LOGGER.log(TRACE, "Disabling Tyrus extension");

String ignoredInitializers = System.getProperty(IGNORE_INITIALIZER_PROPERTY, "");
if (ignoredInitializers.equals("")) {
ignoredInitializers = TyrusServletContainerInitializer.class.getName();
}

System.setProperty(IGNORE_INITIALIZER_PROPERTY, ignoredInitializers);
}
}
}
20 changes: 19 additions & 1 deletion extension/tyrus/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,30 @@
*/

/**
* This module integrates Eclipse Tyrus into Piranha.
* This module integrates Tyrus into Piranha.
*
* <p>
* The following properties can be used to influence the workings of this module.
* </p>
* <table>
* <tr>
* <th>Property</th>
* <th>Notes</th>
* </tr>
* <tr>
* <td>cloud.piranha.extension.tyrus.TyrusExtension.enabled</td>
* <td>true to enable this extension (default), false to disable</td>
* </tr>
* <caption>Configurable properties</caption>
* </table>
*
* @author Manfred Riem ([email protected])
*/
module cloud.piranha.extension.tyrus {

exports cloud.piranha.extension.tyrus;
opens cloud.piranha.extension.tyrus;
requires cloud.piranha.core.api;
requires org.glassfish.tyrus.container.servlet;
requires static cloud.piranha.extension.scinitializer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.extension.tyrus;

import cloud.piranha.core.impl.DefaultWebApplication;
import cloud.piranha.extension.scinitializer.ServletContainerInitializerExtension;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* The JUnit tests for the TyrusExtension class.
*
* @author Manfred Riem ([email protected])
*/
public class TyrusExtensionTest {

/**
* Test configure method.
*/
@Test
public void testConfigure() {
DefaultWebApplication webApplication = new DefaultWebApplication();
TyrusExtension extension = new TyrusExtension();
extension.configure(webApplication);
assertNull(System.getProperty(TyrusExtension.TYRUS_ENABLED_PROPERTY));
}

/**
* Test configure method and disabling Tyrus.
*/
@Test
public void testConfigureAndDisableTyrus() {
DefaultWebApplication webApplication = new DefaultWebApplication();
TyrusExtension extension = new TyrusExtension();
System.setProperty(TyrusExtension.TYRUS_ENABLED_PROPERTY, "false");
extension.configure(webApplication);
assertNotNull(System.getProperty(
ServletContainerInitializerExtension.IGNORE_INITIALIZERS_PROPERTY));
System.clearProperty(TyrusExtension.TYRUS_ENABLED_PROPERTY);
}
}

0 comments on commit de1fcda

Please sign in to comment.