/* * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ package org.mule.runtime.deployment.model.internal.application; import static org.apache.commons.lang.StringUtils.isEmpty; import static org.mule.runtime.api.util.Preconditions.checkArgument; import static org.mule.runtime.api.util.Preconditions.checkState; import org.mule.runtime.deployment.model.api.application.Application; import org.mule.runtime.deployment.model.api.application.ApplicationDescriptor; import org.mule.runtime.deployment.model.api.domain.Domain; import org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor; import org.mule.runtime.deployment.model.api.plugin.ArtifactPluginRepository; import org.mule.runtime.deployment.model.internal.AbstractArtifactClassLoaderBuilder; import org.mule.runtime.module.artifact.classloader.ArtifactClassLoader; import org.mule.runtime.module.artifact.classloader.ArtifactClassLoaderFactory; import org.mule.runtime.module.artifact.classloader.DeployableArtifactClassLoaderFactory; import org.mule.runtime.module.artifact.classloader.MuleDeployableArtifactClassLoader; import org.mule.runtime.module.artifact.classloader.RegionClassLoader; import org.mule.runtime.module.artifact.descriptor.ArtifactDescriptor; import java.io.IOException; /** * {@link ArtifactClassLoader} builder for class loaders required by {@link Application} artifacts * * @since 4.0 */ public class ApplicationClassLoaderBuilder extends AbstractArtifactClassLoaderBuilder<ApplicationClassLoaderBuilder> { private final DeployableArtifactClassLoaderFactory artifactClassLoaderFactory; private Domain domain; /** * Creates a new builder for creating {@link Application} artifacts. * <p> * The {@code domainRepository} is used to locate the domain that this application belongs to and the * {@code artifactClassLoaderBuilder} is used for building the common parts of artifacts. * * @param artifactClassLoaderFactory factory for the classloader specific to the artifact resource and classes * @param artifactPluginClassLoaderFactory creates artifact plugin class loaders. */ public ApplicationClassLoaderBuilder(DeployableArtifactClassLoaderFactory<ApplicationDescriptor> artifactClassLoaderFactory, ArtifactClassLoaderFactory<ArtifactPluginDescriptor> artifactPluginClassLoaderFactory) { super(artifactPluginClassLoaderFactory); this.artifactClassLoaderFactory = artifactClassLoaderFactory; } /** * Creates a new {@code ArtifactClassLoader} using the provided configuration. It will create the proper class loader hierarchy * and filters so application classes, resources, plugins and it's domain resources are resolve correctly. * * @return a {@code MuleDeployableArtifactClassLoader} created from the provided configuration. * @throws IOException exception cause when it was not possible to access the file provided as dependencies */ public MuleDeployableArtifactClassLoader build() throws IOException { checkState(domain != null, "Domain cannot be null"); return (MuleDeployableArtifactClassLoader) super.build(); } @Override protected ArtifactClassLoader createArtifactClassLoader(String artifactId, RegionClassLoader regionClassLoader) { return artifactClassLoaderFactory.create(artifactId, regionClassLoader, artifactDescriptor, artifactPluginClassLoaders); } @Override protected String getArtifactId(ArtifactDescriptor artifactDescriptor) { return getApplicationId(domain.getArtifactId(), artifactDescriptor.getName()); } /** * {@inheritDoc} */ @Override protected ArtifactClassLoader getParentClassLoader() { return this.domain.getArtifactClassLoader(); } /** * @param domain the domain artifact to which the application that is going to use this classloader belongs. * @return the builder */ public ApplicationClassLoaderBuilder setDomain(Domain domain) { this.domain = domain; return this; } /** * @param domainId name of the domain where the application is deployed. Non empty. * @param applicationName name of the application. Non empty. * @return the unique identifier for the application in the container. */ public static String getApplicationId(String domainId, String applicationName) { checkArgument(!isEmpty(domainId), "domainId cannot be empty"); checkArgument(!isEmpty(applicationName), "applicationName cannot be empty"); return domainId + "/app/" + applicationName; } }