Search in sources :

Example 26 with Archive

use of org.jboss.shrinkwrap.api.Archive in project wildfly-swarm by wildfly-swarm.

the class RuntimeDeployer method deploy.

public void deploy(Archive<?> deployment, String asName) throws DeploymentException {
    if (deployment.getName().endsWith(".rar")) {
        // Track any .rar deployments
        this.rarDeploymentNames.add(deployment.getName());
    } else if (!this.rarDeploymentNames.isEmpty()) {
        // Add any previous .rar deployments as dependencies
        // of any non-.rar deployments.
        JARArchive mutable = deployment.as(JARArchive.class);
        this.rarDeploymentNames.forEach(e -> {
            mutable.addModule("deployment." + e);
        });
    }
    try (AutoCloseable deploymentTimer = Performance.time("deployment: " + deployment.getName())) {
        // see DependenciesContainer#addAllDependencies()
        if (deployment instanceof DependenciesContainer) {
            DependenciesContainer<?> depContainer = (DependenciesContainer) deployment;
            if (depContainer.hasMarker(DependenciesContainer.ALL_DEPENDENCIES_MARKER)) {
                if (!depContainer.hasMarker(ALL_DEPENDENCIES_ADDED_MARKER)) {
                    ApplicationEnvironment appEnv = ApplicationEnvironment.get();
                    if (ApplicationEnvironment.Mode.UBERJAR == appEnv.getMode()) {
                        ArtifactLookup artifactLookup = ArtifactLookup.get();
                        for (String gav : appEnv.getDependencies()) {
                            depContainer.addAsLibrary(artifactLookup.artifact(gav));
                        }
                    } else {
                        Set<String> paths = appEnv.resolveDependencies(Collections.emptyList());
                        for (String path : paths) {
                            final File pathFile = new File(path);
                            if (path.endsWith(".jar")) {
                                depContainer.addAsLibrary(pathFile);
                            } else if (pathFile.isDirectory()) {
                                depContainer.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class).importDirectory(pathFile).as(GenericArchive.class), "/WEB-INF/classes", Filters.includeAll());
                            }
                        }
                    }
                    depContainer.addMarker(ALL_DEPENDENCIES_ADDED_MARKER);
                }
            }
        }
        this.deploymentContext.activate(deployment, asName, !this.implicitDeploymentsComplete);
        // 2. give fractions a chance to handle the deployment
        for (DeploymentProcessor processor : this.deploymentProcessors) {
            processor.process();
        }
        this.deploymentContext.deactivate();
        if (DeployerMessages.MESSAGES.isDebugEnabled()) {
            DeployerMessages.MESSAGES.deploying(deployment.getName());
            Map<ArchivePath, Node> ctx = deployment.getContent();
            for (Map.Entry<ArchivePath, Node> each : ctx.entrySet()) {
                DeployerMessages.MESSAGES.deploymentContent(each.getKey().toString());
            }
        }
        if (BootstrapProperties.flagIsSet(SwarmProperties.EXPORT_DEPLOYMENT)) {
            String exportLocation = System.getProperty(SwarmProperties.EXPORT_DEPLOYMENT);
            if (exportLocation != null) {
                Path archivePath = null;
                if (exportLocation.toLowerCase().equals("true")) {
                    archivePath = Paths.get(deployment.getName());
                } else {
                    Path exportDir = Paths.get(exportLocation);
                    Files.createDirectories(exportDir);
                    archivePath = exportDir.resolve(deployment.getName());
                }
                final File out = archivePath.toFile();
                DeployerMessages.MESSAGES.exportingDeployment(out.getAbsolutePath());
                deployment.as(ZipExporter.class).exportTo(out, true);
            }
        }
        byte[] hash = this.contentRepository.addContent(deployment);
        final ModelNode deploymentAdd = new ModelNode();
        deploymentAdd.get(OP).set(ADD);
        deploymentAdd.get(OP_ADDR).set("deployment", deployment.getName());
        deploymentAdd.get(RUNTIME_NAME).set(deployment.getName());
        deploymentAdd.get(ENABLED).set(true);
        deploymentAdd.get(PERSISTENT).set(true);
        ModelNode content = deploymentAdd.get(CONTENT).add();
        content.get(HASH).set(hash);
        int deploymentTimeout = Integer.getInteger(SwarmProperties.DEPLOYMENT_TIMEOUT, 300);
        final ModelNode opHeaders = new ModelNode();
        opHeaders.get(BLOCKING_TIMEOUT).set(deploymentTimeout);
        deploymentAdd.get(OPERATION_HEADERS).set(opHeaders);
        BootstrapLogger.logger("org.wildfly.swarm.runtime.deployer").info("deploying " + deployment.getName());
        System.setProperty(SwarmInternalProperties.CURRENT_DEPLOYMENT, deployment.getName());
        try {
            ModelNode result = client.execute(deploymentAdd);
            ModelNode outcome = result.get("outcome");
            if (outcome.asString().equals("success")) {
                return;
            }
            ModelNode description = result.get("failure-description");
            throw new DeploymentException(deployment, SwarmMessages.MESSAGES.deploymentFailed(description.asString()));
        } catch (IOException e) {
            throw SwarmMessages.MESSAGES.deploymentFailed(e, deployment);
        }
    } catch (Exception e) {
        throw new DeploymentException(deployment, e);
    }
}
Also used : ApplicationEnvironment(org.wildfly.swarm.bootstrap.env.ApplicationEnvironment) ADD(org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD) DefaultDeploymentCreator(org.wildfly.swarm.container.runtime.deployments.DefaultDeploymentCreator) DependenciesContainer(org.wildfly.swarm.spi.api.DependenciesContainer) Node(org.jboss.shrinkwrap.api.Node) PreDestroy(javax.annotation.PreDestroy) BLOCKING_TIMEOUT(org.jboss.as.controller.descriptions.ModelDescriptionConstants.BLOCKING_TIMEOUT) Map(java.util.Map) BootstrapLogger(org.wildfly.swarm.bootstrap.logging.BootstrapLogger) ENABLED(org.jboss.as.controller.descriptions.ModelDescriptionConstants.ENABLED) Any(javax.enterprise.inject.Any) Path(java.nio.file.Path) DeploymentContext(org.wildfly.swarm.container.runtime.cdi.DeploymentContext) Instance(javax.enterprise.inject.Instance) ZipImporter(org.jboss.shrinkwrap.api.importer.ZipImporter) Collection(java.util.Collection) SwarmInternalProperties(org.wildfly.swarm.spi.api.internal.SwarmInternalProperties) Set(java.util.Set) JARArchive(org.wildfly.swarm.spi.api.JARArchive) GenericArchive(org.jboss.shrinkwrap.api.GenericArchive) DeploymentException(org.wildfly.swarm.container.DeploymentException) FileSystemLayout(org.wildfly.swarm.internal.FileSystemLayout) List(java.util.List) OP(org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP) Stream(java.util.stream.Stream) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) CONTENT(org.jboss.as.controller.descriptions.ModelDescriptionConstants.CONTENT) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ModelNode(org.jboss.dmr.ModelNode) SwarmProperties(org.wildfly.swarm.spi.api.SwarmProperties) RUNTIME_NAME(org.jboss.as.controller.descriptions.ModelDescriptionConstants.RUNTIME_NAME) DeploymentProcessor(org.wildfly.swarm.spi.api.DeploymentProcessor) TempFileProvider(org.jboss.vfs.TempFileProvider) Performance(org.wildfly.swarm.bootstrap.performance.Performance) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Deployer(org.wildfly.swarm.container.internal.Deployer) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) DeployerMessages(org.wildfly.swarm.internal.DeployerMessages) ArtifactLookup(org.wildfly.swarm.spi.api.ArtifactLookup) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) OPERATION_HEADERS(org.jboss.as.controller.descriptions.ModelDescriptionConstants.OPERATION_HEADERS) OP_ADDR(org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR) PERSISTENT(org.jboss.as.controller.descriptions.ModelDescriptionConstants.PERSISTENT) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Files(java.nio.file.Files) BootstrapProperties(org.wildfly.swarm.bootstrap.util.BootstrapProperties) ExplodedImporter(org.jboss.shrinkwrap.api.importer.ExplodedImporter) SwarmMessages(org.wildfly.swarm.internal.SwarmMessages) IOException(java.io.IOException) Filters(org.jboss.shrinkwrap.api.Filters) Archive(org.jboss.shrinkwrap.api.Archive) SwarmContentRepository(org.wildfly.swarm.container.runtime.wildfly.SwarmContentRepository) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) File(java.io.File) HASH(org.jboss.as.controller.descriptions.ModelDescriptionConstants.HASH) Paths(java.nio.file.Paths) Collections(java.util.Collections) ArtifactLookup(org.wildfly.swarm.spi.api.ArtifactLookup) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) Node(org.jboss.shrinkwrap.api.Node) ModelNode(org.jboss.dmr.ModelNode) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) DeploymentProcessor(org.wildfly.swarm.spi.api.DeploymentProcessor) JARArchive(org.wildfly.swarm.spi.api.JARArchive) Path(java.nio.file.Path) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) DependenciesContainer(org.wildfly.swarm.spi.api.DependenciesContainer) IOException(java.io.IOException) ApplicationEnvironment(org.wildfly.swarm.bootstrap.env.ApplicationEnvironment) DeploymentException(org.wildfly.swarm.container.DeploymentException) IOException(java.io.IOException) GenericArchive(org.jboss.shrinkwrap.api.GenericArchive) DeploymentException(org.wildfly.swarm.container.DeploymentException) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File) Map(java.util.Map)

Example 27 with Archive

use of org.jboss.shrinkwrap.api.Archive in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManagerTest method testDeploymentConfigurableUsingBoth.

@Test
public void testDeploymentConfigurableUsingBoth() throws Exception {
    Properties props = new Properties();
    Map<String, String> env = new HashMap<>();
    ConfigViewFactory factory = new ConfigViewFactory(props, env);
    factory.withProperty("swarm.deployment.[myapp.war].context", "/my-specific-app");
    factory.withProperty("swarm.http.context", "/my-alias-app");
    ConfigView configView = factory.get(true);
    DeploymentContext context = new DeploymentContextImpl();
    ConfigurableManager manager = new ConfigurableManager(configView, context);
    Archive archive = ShrinkWrap.create(JavaArchive.class, "myapp.war");
    try {
        context.activate(archive, archive.getName(), false);
        Component component = new Component();
        manager.scan(component);
        assertThat(component.context.get()).isEqualTo("/my-specific-app");
    } finally {
        context.deactivate();
    }
    Archive archiveToo = ShrinkWrap.create(JavaArchive.class, "otherapp.war");
    try {
        context.activate(archiveToo, archiveToo.getName(), false);
        Component component = new Component();
        manager.scan(component);
        assertThat(component.context.get()).isEqualTo("/my-alias-app");
    } finally {
        context.deactivate();
    }
}
Also used : DeploymentContext(org.wildfly.swarm.container.runtime.cdi.DeploymentContext) Archive(org.jboss.shrinkwrap.api.Archive) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) HashMap(java.util.HashMap) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) ConfigViewFactory(org.wildfly.swarm.container.config.ConfigViewFactory) Properties(java.util.Properties) DeploymentContextImpl(org.wildfly.swarm.container.runtime.cdi.DeploymentContextImpl) Test(org.junit.Test)

Example 28 with Archive

use of org.jboss.shrinkwrap.api.Archive in project wildfly-swarm by wildfly-swarm.

the class HttpSecurityPreparer method process.

@SuppressWarnings("unchecked")
@Override
public void process() {
    if (deploymentConfigs == null || deploymentConfigs.isEmpty()) {
        return;
    }
    // find a matching archive declaration
    Optional<String> match = deploymentConfigs.keySet().stream().filter(c -> archive.getName().equals(c)).findFirst();
    if (!match.isPresent()) {
        // no matching archive
        return;
    }
    Map<String, Object> matchingConfig = (Map<String, Object>) deploymentConfigs.get(match.get());
    if (!matchingConfig.containsKey("web")) {
        // missing web configuration
        return;
    }
    Map<String, Object> deploymentConfig = (Map<String, Object>) matchingConfig.get("web");
    WARArchive war = archive.as(WARArchive.class);
    WebXmlAsset webXml = war.findWebXmlAsset();
    JBossWebAsset jbossWeb = war.findJbossWebAsset();
    // login-config
    Map<String, Object> loginConfig = (Map<String, Object>) deploymentConfig.get("login-config");
    if (loginConfig != null) {
        String authMethod = (String) loginConfig.getOrDefault("auth-method", "NONE");
        // Setup login-config
        webXml.setLoginConfig(authMethod, "ignored");
        // security domain
        if (loginConfig.containsKey("security-domain")) {
            jbossWeb.setSecurityDomain((String) loginConfig.get("security-domain"));
        }
        // form login
        if (loginConfig.containsKey("form-login-config")) {
            Map<String, Object> formLoginConfig = (Map<String, Object>) loginConfig.get("form-login-config");
            webXml.setFormLoginConfig("Security Realm", (String) formLoginConfig.get("form-login-page"), (String) formLoginConfig.get("form-error-page"));
        }
    }
    // security constraints
    List<Map<String, Object>> securityConstraints = (List<Map<String, Object>>) deploymentConfig.getOrDefault("security-constraints", Collections.EMPTY_LIST);
    for (Map<String, Object> sc : securityConstraints) {
        SecurityConstraint securityConstraint = webXml.protect((String) sc.getOrDefault("url-pattern", "/*"));
        ((List<String>) sc.getOrDefault("methods", Collections.emptyList())).forEach(securityConstraint::withMethod);
        ((List<String>) sc.getOrDefault("roles", Collections.emptyList())).forEach(securityConstraint::withRole);
    }
}
Also used : DeploymentProcessor(org.wildfly.swarm.spi.api.DeploymentProcessor) DeploymentScoped(org.wildfly.swarm.spi.runtime.annotations.DeploymentScoped) Logger(org.jboss.logging.Logger) JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) Archive(org.jboss.shrinkwrap.api.Archive) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) Inject(javax.inject.Inject) List(java.util.List) Configurable(org.wildfly.swarm.spi.api.annotations.Configurable) Map(java.util.Map) WARArchive(org.wildfly.swarm.undertow.WARArchive) AttributeDocumentation(org.wildfly.swarm.config.runtime.AttributeDocumentation) Optional(java.util.Optional) Collections(java.util.Collections) JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) WARArchive(org.wildfly.swarm.undertow.WARArchive) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) List(java.util.List) Map(java.util.Map)

Example 29 with Archive

use of org.jboss.shrinkwrap.api.Archive in project wildfly-swarm by wildfly-swarm.

the class JolokiaWarDeploymentProducer method jolokiaWar.

@Produces
public Archive jolokiaWar() throws Exception {
    if (this.context == null) {
        this.context = this.fraction.context();
    }
    Archive deployment = this.lookup.artifact(DEPLOYMENT_GAV, DEPLOYMENT_NAME);
    deployment.as(WARArchive.class).setContextRoot(this.context);
    Consumer<Archive> preparer = new ConfigurationValueAccessPreparer(this.jolokiaAccessXML);
    if (this.fraction.jolokiaWarPreparer() != null) {
        preparer = preparer.andThen(this.fraction.jolokiaWarPreparer());
    }
    preparer.accept(deployment);
    return deployment;
}
Also used : WARArchive(org.wildfly.swarm.undertow.WARArchive) Archive(org.jboss.shrinkwrap.api.Archive) ConfigurationValueAccessPreparer(org.wildfly.swarm.jolokia.access.ConfigurationValueAccessPreparer) WARArchive(org.wildfly.swarm.undertow.WARArchive) Produces(javax.enterprise.inject.Produces)

Example 30 with Archive

use of org.jboss.shrinkwrap.api.Archive in project wildfly-swarm by wildfly-swarm.

the class JolokiaWarDeploymentProducerTest method testPreferConfigValueFile_vs_FractionSetting.

public void testPreferConfigValueFile_vs_FractionSetting() throws Exception {
    URL resource = getClass().getClassLoader().getResource("my-jolokia-access2.xml");
    URL fractionResource = getClass().getClassLoader().getResource("my-jolokia-access3.xml");
    String path = resource.getPath();
    File file = new File(path);
    JolokiaWarDeploymentProducer producer = new JolokiaWarDeploymentProducer();
    producer.fraction = new JolokiaFraction().prepareJolokiaWar(JolokiaFraction.jolokiaAccessXml(fractionResource));
    producer.lookup = new MockArtifactLookup();
    producer.jolokiaAccessXML = file.getAbsolutePath();
    Archive war = producer.jolokiaWar();
    Node xml = war.get("WEB-INF/classes/jolokia-access.xml");
    assertThat(xml).isNotNull();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(xml.getAsset().openStream()))) {
        List<String> lines = reader.lines().collect(Collectors.toList());
        assertThat(lines).isNotEmpty();
        assertThat(lines.get(0)).contains("This is my-jolokia-access2.xml");
    }
}
Also used : Archive(org.jboss.shrinkwrap.api.Archive) InputStreamReader(java.io.InputStreamReader) Node(org.jboss.shrinkwrap.api.Node) BufferedReader(java.io.BufferedReader) File(java.io.File) JolokiaFraction(org.wildfly.swarm.jolokia.JolokiaFraction) URL(java.net.URL)

Aggregations

Archive (org.jboss.shrinkwrap.api.Archive)37 Node (org.jboss.shrinkwrap.api.Node)17 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)12 Test (org.junit.Test)12 File (java.io.File)11 URL (java.net.URL)10 ArrayList (java.util.ArrayList)9 ArchivePath (org.jboss.shrinkwrap.api.ArchivePath)8 JolokiaFraction (org.wildfly.swarm.jolokia.JolokiaFraction)8 BufferedReader (java.io.BufferedReader)7 IOException (java.io.IOException)7 InputStreamReader (java.io.InputStreamReader)7 Map (java.util.Map)7 HashMap (java.util.HashMap)6 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)6 List (java.util.List)5 ZipImporter (org.jboss.shrinkwrap.api.importer.ZipImporter)5 DeploymentContext (org.wildfly.swarm.container.runtime.cdi.DeploymentContext)5 JARArchive (org.wildfly.swarm.spi.api.JARArchive)5 Path (java.nio.file.Path)4