Skip to content

Commit

Permalink
Defend agains 1+ deployments
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Oct 17, 2023
1 parent 6e7368e commit 82508da
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* <li>useBeanXmlTemplate: (Optional) defaults to true: will create the default templates/beans.xml when beans.xml is missing</li>
* <li>includeWarContextPath: (Optional) defaults to false: will include the war name as a root context.
* For example, if a example.war is deployed, the root context is going to be /example.</li>
* <li>multipleDeployments: (Optional) defaults to true: workaround for tests that unintentionally
* executes 1+ times @org.jboss.arquillian.container.test.api.Deployment</li>
* </ul>
*/
public class HelidonContainerConfiguration implements ContainerConfiguration {
Expand All @@ -52,6 +54,7 @@ public class HelidonContainerConfiguration implements ContainerConfiguration {
private boolean useParentClassloader = true;
private boolean inWebContainer = false;
private boolean useBeanXmlTemplate = true;
private boolean multipleDeployments = true;
/*
* Restful requires it, but core profile don't (because rest used to be deployed in a
* web container together with other apps and in core profile there is only one app)
Expand Down Expand Up @@ -140,6 +143,14 @@ public void setIncludeWarContextPath(boolean includeWarContextPath) {
this.includeWarContextPath = includeWarContextPath;
}

public boolean isMultipleDeployments() {
return multipleDeployments;
}

public void setMultipleDeployments(boolean multipleDeployments) {
this.multipleDeployments = multipleDeployments;
}

@Override
public void validate() throws ConfigurationException {
if ((port <= 0) || (port > Short.MAX_VALUE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,79 +189,81 @@ public ProtocolDescription getDefaultProtocol() {

@Override
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
// Because helidon doesn't have a dynamic war deployment model, we need to actually start the server here.
RunContext context = new RunContext();
contexts.put(archive.getId(), context);
if (containerConfig.isMultipleDeployments() || (!containerConfig.isMultipleDeployments() && contexts.isEmpty())) {
// Because helidon doesn't have a dynamic war deployment model, we need to actually start the server here.
RunContext context = new RunContext();
contexts.put(archive.getId(), context);

// Is it a JavaArchive?
boolean isJavaArchive = archive instanceof JavaArchive;
// Is it a JavaArchive?
boolean isJavaArchive = archive instanceof JavaArchive;

try {
// Create the temporary deployment directory.
if (containerConfig.getUseRelativePath()) {
context.deployDir = Paths.get("target/helidon-arquillian-test");
} else {
context.deployDir = Files.createTempDirectory("helidon-arquillian-test");
}
LOGGER.log(Level.INFO, "Running Arquillian tests in directory: " + context.deployDir.toAbsolutePath());

copyArchiveToDeployDir(archive, context.deployDir);
try {
// Create the temporary deployment directory.
if (containerConfig.getUseRelativePath()) {
context.deployDir = Paths.get("target/helidon-arquillian-test");
} else {
context.deployDir = Files.createTempDirectory("helidon-arquillian-test");
}
LOGGER.log(Level.INFO, "Running Arquillian tests in directory: " + context.deployDir.toAbsolutePath());

for (Archive<?> additionalArchive : additionalArchives) {
copyArchiveToDeployDir(additionalArchive, context.deployDir);
}
copyArchiveToDeployDir(archive, context.deployDir);

List<Path> classPath = new ArrayList<>();
for (Archive<?> additionalArchive : additionalArchives) {
copyArchiveToDeployDir(additionalArchive, context.deployDir);
}

Path rootDir = context.deployDir.resolve("");
if (isJavaArchive) {
ensureBeansXml(rootDir, null);
classPath.add(rootDir);
} else {
// Prepare the launcher files
Path webInfDir = context.deployDir.resolve("WEB-INF");
Path classesDir = webInfDir.resolve("classes");
Path libDir = webInfDir.resolve("lib");
ensureBeansXml(classesDir, webInfDir);
addServerClasspath(classPath, classesDir, libDir, rootDir);
if (containerConfig.isInWebContainer()) {
if (containerConfig.isIncludeWarContextPath()) {
context.rootContext = archive.getName().split("\\.")[0];
}
if (!loadApplicationFromWebXml(context, webInfDir)) {
// Search Application in classes
loadApplicationFromClasses(context, archive);
List<Path> classPath = new ArrayList<>();

Path rootDir = context.deployDir.resolve("");
if (isJavaArchive) {
ensureBeansXml(rootDir, null);
classPath.add(rootDir);
} else {
// Prepare the launcher files
Path webInfDir = context.deployDir.resolve("WEB-INF");
Path classesDir = webInfDir.resolve("classes");
Path libDir = webInfDir.resolve("lib");
ensureBeansXml(classesDir, webInfDir);
addServerClasspath(classPath, classesDir, libDir, rootDir);
if (containerConfig.isInWebContainer()) {
if (containerConfig.isIncludeWarContextPath()) {
context.rootContext = archive.getName().split("\\.")[0];
}
if (!loadApplicationFromWebXml(context, webInfDir)) {
// Search Application in classes
loadApplicationFromClasses(context, archive);
}
}
}
}

startServer(context, classPath.toArray(new Path[0]));
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.log(Level.INFO, "Failed to start container", e);
throw new DeploymentException("Failed to copy the archive assets into the deployment directory", e);
} catch (InvocationTargetException e) {
startServer(context, classPath.toArray(new Path[0]));
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.log(Level.INFO, "Failed to start container", e);
throw new DeploymentException("Failed to copy the archive assets into the deployment directory", e);
} catch (InvocationTargetException e) {

try {
context.runnerClass
.getDeclaredMethod("abortedCleanup")
.invoke(context.runner);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
ex.printStackTrace();
try {
context.runnerClass
.getDeclaredMethod("abortedCleanup")
.invoke(context.runner);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
ex.printStackTrace();
}

throw lookForSupressedDeploymentException(e.getTargetException())
.map(d ->
new org.jboss.arquillian.container.spi.client.container.DeploymentException("Deployment failure!", d))
.orElseThrow(() -> new DefinitionException(e));
} catch (ReflectiveOperationException e) {
LOGGER.log(Level.INFO, "Failed to start container", e);
throw new DefinitionException(e); // validation exceptions
}

throw lookForSupressedDeploymentException(e.getTargetException())
.map(d ->
new org.jboss.arquillian.container.spi.client.container.DeploymentException("Deployment failure!", d))
.orElseThrow(() -> new DefinitionException(e));
} catch (ReflectiveOperationException e) {
LOGGER.log(Level.INFO, "Failed to start container", e);
throw new DefinitionException(e); // validation exceptions
// Server has started, so we're done.
// ProtocolMetaData pm = new ProtocolMetaData();
// pm.addContext(new HTTPContext("Helidon", "localhost", containerConfig.getPort()));
// return pm;
}

// Server has started, so we're done.
// ProtocolMetaData pm = new ProtocolMetaData();
// pm.addContext(new HTTPContext("Helidon", "localhost", containerConfig.getPort()));
// return pm;
return new ProtocolMetaData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<property name="useRelativePath">false</property>
<property name="inWebContainer">true</property>
<property name="includeWarContextPath">true</property>
<property name="multipleDeployments">false</property>
</configuration>
</container>
</arquillian>

0 comments on commit 82508da

Please sign in to comment.