Skip to content

Commit

Permalink
[WFCORE-6524] Do not duplicate managed deployment in content reposito…
Browse files Browse the repository at this point in the history
…ry in tmp/vfs/temp directory
  • Loading branch information
gaol committed Sep 27, 2023
1 parent 91c7baa commit 0952ae5
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Closeable mountDeploymentContent(final VirtualFile contents, VirtualFile
assert contents != null : "null contents";
switch (type) {
case ZIP:
return VFS.mountZip(contents, mountPoint, tempFileProvider);
return VFS.mountZip(contents.getPhysicalFile(), mountPoint, tempFileProvider);
case EXPANDED:
return VFS.mountZipExpanded(contents, mountPoint, tempFileProvider);
case REAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private synchronized ResourceRoot createResourceRoot(final VirtualFile file, fin
if(overlay != null) {
overlay.remountAsZip(false);
} else if(file.isFile()) {
closable = VFS.mountZip(file, file, TempFileProviderService.provider());
closable = VFS.mountZip(file.getPhysicalFile(), file, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(file, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.wildfly.core.test.standalone.deployment;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import jakarta.inject.Inject;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.Operation;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.repository.ContentFilter;
import org.jboss.as.repository.ContentRepositoryElement;
import org.jboss.as.repository.PathUtil;
import org.jboss.as.server.ServerEnvironment;
import org.jboss.as.test.deployment.trivial.ServiceActivatorDeploymentUtil;
import org.jboss.as.test.integration.management.ManagementOperations;
import org.jboss.as.test.integration.management.util.ServerReload;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.core.testrunner.ManagementClient;
import org.wildfly.core.testrunner.WildFlyRunner;

/**
* Tests that there is no jar duplication on deployment to save disk space.
*
* @author <a href="mailto:aoingl@gmail.com">Lin Gao</a>
*/
@RunWith(WildFlyRunner.class)
public class DeploymentDuplicationTestCase {

private static final String DEPLOYMENT_NAME = DeploymentDuplicationTestCase.class.getSimpleName() + ".jar";

private final Properties props = new Properties();

@Inject
private ManagementClient managementClient;

@Before
public void setup() {
props.clear();
props.put("prop-key-A", "prop-value-A");
props.put("prop-key-B", "prop-value-B");
props.put("prop-key-C", "prop-value-C");
}

@After
public void tearDown() {
props.clear();
}

@Test
public void test() throws Exception {
ModelControllerClient mcc = managementClient.getControllerClient();
JavaArchive deployment = ServiceActivatorDeploymentUtil.createServiceActivatorDeploymentArchive(DEPLOYMENT_NAME, props);
Operation deploymentOp = createDeploymentOp(deployment);
try {
ManagementOperations.executeOperation(mcc, deploymentOp);
ServiceActivatorDeploymentUtil.validateProperties(mcc, props);

Path tempPath = Paths.get(ManagementOperations.executeOperation(mcc, readTempPathOp()).asString());
List<ContentRepositoryElement> elements = PathUtil.listFiles(tempPath, null, new ContentFilter() {
@Override
public boolean acceptFile(Path rootPath, Path file) throws IOException {
return file.endsWith("content");
}
@Override
public boolean acceptFile(Path rootPath, Path file, InputStream in) throws IOException {
return file.endsWith("content");
}
@Override
public boolean acceptDirectory(Path rootPath, Path path) throws IOException {
return false;
}
});
// Check temp directory to make sure no duplication
Assert.assertTrue("There should be no content file in the tmp directory", elements.isEmpty());
} finally {
ManagementOperations.executeOperation(mcc, Util.createRemoveOperation(PathAddress.pathAddress("deployment", deployment.getName())));
ServerReload.executeReloadAndWaitForCompletion(mcc);
}
}

private Operation readTempPathOp() {
final ModelNode readAttributeOperation = Util.getReadAttributeOperation(PathAddress.pathAddress("path", ServerEnvironment.SERVER_TEMP_DIR), "path");
return Operation.Factory.create(readAttributeOperation, Collections.emptyList(), true);
}

private Operation createDeploymentOp(JavaArchive deployment) {
final List<InputStream> streams = new ArrayList<>();
streams.add(deployment.as(ZipExporter.class).exportAsInputStream());
final ModelNode addOperation = Util.createAddOperation(PathAddress.pathAddress("deployment", deployment.getName()));
addOperation.get("enabled").set(true);
addOperation.get("content").add().get("input-stream-index").set(0);
return Operation.Factory.create(addOperation, streams, true);
}

}

0 comments on commit 0952ae5

Please sign in to comment.