private BundleDescriptor toBundleDescriptor(URL artifactUrl, String groupId) { String artifactPath = artifactUrl.getPath(); String groupIdPath = getGroupIdPath(groupId); int artifactIdIndex = artifactPath.indexOf(groupIdPath) + groupIdPath.length(); Matcher urlMatcher = MAVEN_ARTIFACT_PATTERN.matcher(artifactPath.substring(artifactIdIndex)); urlMatcher.find(); return new BundleDescriptor.Builder() .setGroupId(groupId) .setArtifactId(urlMatcher.group(1)) .setVersion(urlMatcher.group(2)) .setClassifier(urlMatcher.group(3)) .setType(urlMatcher.group(4)) .build(); }
.setArtifactId(artifactId) .setVersion(version) .setClassifier(classifier) .setType(type) .build();
/** * Loads a bundle descriptor from the provided properties * * * @param artifactFolder {@link File} where the current artifact to work with. Non null * @param attributes attributes defined in the loader. * @param artifactType the type of the artifact of the descriptor to be loaded. * @return a locator of the coordinates of the current artifact * @throws ArtifactDescriptorCreateException if any bundle descriptor required property is missing on the given attributes. */ @Override public BundleDescriptor load(File artifactFolder, Map<String, Object> attributes, ArtifactType artifactType) throws InvalidDescriptorLoaderException { String version = (String) attributes.get(VERSION); String groupId = (String) attributes.get(GROUP_ID); String artifactId = (String) attributes.get(ARTIFACT_ID); String classifier = (String) attributes.get(CLASSIFIER); String type = (String) attributes.get(TYPE); try { return new BundleDescriptor.Builder().setVersion(version).setGroupId(groupId).setArtifactId(artifactId) .setClassifier(classifier).setType(type).build(); } catch (IllegalArgumentException e) { throw new InvalidDescriptorLoaderException("Bundle descriptor attributes are not complete", e); } } }
private ClassLoaderModel createClassLoaderModelWithDomain() { BundleDescriptor domainDescriptor = new BundleDescriptor.Builder().setArtifactId(DOMAIN_NAME).setGroupId("test") .setVersion("1.0.0").setClassifier(MULE_DOMAIN_CLASSIFIER).build(); Set<BundleDependency> domainDependency = Collections.singleton(new BundleDependency.Builder().setDescriptor(domainDescriptor).build()); return new ClassLoaderModel.ClassLoaderModelBuilder().dependingOn(domainDependency).build(); }
.setArtifactId(artifactId) .setVersion(version) .setClassifier(classifier) .setType(type) .build();
.setVersion("1.0.0") .setType("jar") .setClassifier("mule-application") .build());
/** * Looks for the POM file within the current {@code pluginFolder} structure (under * {@link ArtifactPluginDescriptor#MULE_ARTIFACT_FOLDER} folder) to retrieve the plugin artifact locator. * * @param artifactFile {@link File} with the content of the artifact to work with. Non null * @param attributes collection of attributes describing the loader. Non null. * @param artifactType the type of the artifact of the descriptor to be loaded. * @return a locator of the coordinates of the current plugin * @throws ArtifactDescriptorCreateException if the plugin is missing the {@link ArtifactPluginDescriptor#MULE_PLUGIN_POM} or * there's an issue while reading that file */ @Override public BundleDescriptor load(File artifactFile, Map<String, Object> attributes, ArtifactType artifactType) throws InvalidDescriptorLoaderException { if (isHeavyPackage(artifactFile)) { File classLoaderModelDescriptor = getClassLoaderModelDescriptor(artifactFile); org.mule.tools.api.classloader.model.ClassLoaderModel packagerClassLoaderModel = deserialize(classLoaderModelDescriptor); return new BundleDescriptor.Builder() .setArtifactId(packagerClassLoaderModel.getArtifactCoordinates().getArtifactId()) .setGroupId(packagerClassLoaderModel.getArtifactCoordinates().getGroupId()) .setVersion(packagerClassLoaderModel.getArtifactCoordinates().getVersion()) .setType(packagerClassLoaderModel.getArtifactCoordinates().getType()) .setClassifier(packagerClassLoaderModel.getArtifactCoordinates().getClassifier()) .build(); } else { return getBundleDescriptor(artifactFile, artifactType); } }
.setArtifactId(pomProperties.getProperty("artifactId")) .setVersion(pomProperties.getProperty("version")) .setClassifier(artifactType.equals(APP) ? MULE_APPLICATION_CLASSIFIER : MULE_DOMAIN_CLASSIFIER) .build(); } else { .setType(JAR) .setClassifier(artifactType.equals(PLUGIN) ? MULE_PLUGIN_CLASSIFIER : model.getPackaging()) .build();
@Test public void remembersRamlApiClassLoader() throws Exception { final ClassLoader parentClassLoader = mock(ClassLoader.class); ArtifactDescriptor appDescriptor = mock(ArtifactDescriptor.class); RegionClassLoader regionClassLoader = new RegionClassLoader(ARTIFACT_ID, appDescriptor, parentClassLoader, lookupPolicy); createClassLoaders(parentClassLoader); ClassLoaderModel classLoaderModel = new ClassLoaderModel.ClassLoaderModelBuilder() .dependingOn(newHashSet(new BundleDependency.Builder() .setBundleUri(API_LOCATION.toURI()) .setDescriptor(new BundleDescriptor.Builder() .setGroupId(GROUP_ID) .setArtifactId(SPECIFIC_ARTIFACT_ID) .setVersion(ARTIFACT_VERSION) .setClassifier("raml") .setType("zip") .build()) .build())) .build(); when(appDescriptor.getClassLoaderModel()).thenReturn(classLoaderModel); String apiResource = format(SPECIFIC_RESOURCE_FORMAT, ARTIFACT_VERSION, "raml", "zip", API_RESOURCE_NAME); assertThat(regionClassLoader.findResource(apiResource), is(API_LOADED_RESOURCE)); assertThat(regionClassLoader.findResource(apiResource), is(API_LOADED_RESOURCE)); assertThat(regionClassLoader.findResource(apiResource), is(API_LOADED_RESOURCE)); assertThat(regionClassLoader.findResource(apiResource), is(API_LOADED_RESOURCE)); assertThat(regionClassLoader.findResource(apiResource), is(API_LOADED_RESOURCE)); verify(appDescriptor, times(1)).getClassLoaderModel(); }
@Before public void setUp() throws Exception { FooServiceProvider.INVOKED = false; BarServiceProvider.INVOKED = false; final File servicesFolder = getServicesFolder(); assertThat(servicesFolder.mkdir(), is(true)); when(containerClassLoader.getClassLoader()).thenReturn(getClass().getClassLoader()); when(containerClassLoader.getClassLoaderLookupPolicy()).thenReturn(mock(ClassLoaderLookupPolicy.class)); BundleDescriptorLoader bundleDescriptorLoaderMock = mock(BundleDescriptorLoader.class); when(bundleDescriptorLoaderMock.supportsArtifactType(ArtifactType.SERVICE)).thenReturn(true); when(bundleDescriptorLoaderMock.load(Mockito.any(File.class), Mockito.any(Map.class), eq(ArtifactType.SERVICE))) .thenReturn(new BundleDescriptor.Builder() .setGroupId("mockGroupId") .setArtifactId("mockArtifactId") .setVersion("1.0.0") .setClassifier("mule-service") .setType("jar") .build()); when(descriptorLoaderRepository.get(anyString(), anyObject(), argThat(equalTo(BundleDescriptorLoader.class)))) .thenReturn(bundleDescriptorLoaderMock); when(descriptorLoaderRepository.get(anyString(), anyObject(), argThat(equalTo(ClassLoaderModelLoader.class)))) .thenReturn(mock(ClassLoaderModelLoader.class)); doNothing().when(artifactDescriptorValidator).validate(anyObject()); }
private void getResourceFromApiArtifact(String apiKind, String resource, URL expectedResult) throws Exception { final ClassLoader parentClassLoader = mock(ClassLoader.class); ArtifactDescriptor appDescriptor = mock(ArtifactDescriptor.class); RegionClassLoader regionClassLoader = new RegionClassLoader(ARTIFACT_ID, appDescriptor, parentClassLoader, lookupPolicy); createClassLoaders(parentClassLoader); ClassLoaderModel classLoaderModel = new ClassLoaderModel.ClassLoaderModelBuilder() .dependingOn(newHashSet(new BundleDependency.Builder() .setBundleUri(API_LOCATION.toURI()) .setDescriptor(new BundleDescriptor.Builder() .setGroupId(GROUP_ID) .setArtifactId(SPECIFIC_ARTIFACT_ID) .setVersion(ARTIFACT_VERSION) .setClassifier(apiKind) .setType("zip") .build()) .build())) .build(); when(appDescriptor.getClassLoaderModel()).thenReturn(classLoaderModel); URL result = regionClassLoader.findResource(resource); assertThat(result, is(expectedResult)); }
private void getResourceFromExportingArtifact(String resource, URL expectedResult, RegionClassLoader regionClassLoader) { ArtifactClassLoader pluginClassloader = mock(ArtifactClassLoader.class); ArtifactDescriptor pluginDescriptor = mock(ArtifactDescriptor.class); when(lookupPolicy.getPackageLookupStrategy(PACKAGE_NAME)).thenReturn(CHILD_FIRST); when(pluginClassloader.getArtifactDescriptor()).thenReturn(pluginDescriptor); when(pluginDescriptor.getBundleDescriptor()).thenReturn(new BundleDescriptor.Builder() .setGroupId(GROUP_ID) .setArtifactId(SPECIFIC_ARTIFACT_ID) .setVersion(ARTIFACT_VERSION) .setClassifier("mule-plugin") .setType("jar") .build()); when(pluginClassloader.findResource(RESOURCE_NAME)).thenReturn(PLUGIN_LOADED_RESOURCE); when(pluginClassloader.findResource(NON_EXPORTED_RESOURCE_NAME)).thenReturn(PLUGIN_LOADED_RESOURCE); regionClassLoader.addClassLoader(pluginClassloader, new DefaultArtifactClassLoaderFilter(singleton(PACKAGE_NAME), singleton("dummy.txt"))); URL result = regionClassLoader.findResource(resource); assertThat(result, is(expectedResult)); }
@Before public void setUp() throws Exception { BundleDescriptorLoader bundleDescriptorLoaderMock = mock(BundleDescriptorLoader.class); when(bundleDescriptorLoaderMock.supportsArtifactType(ArtifactType.SERVICE)).thenReturn(true); when(bundleDescriptorLoaderMock.load(Mockito.any(File.class), Mockito.any(Map.class), eq(ArtifactType.SERVICE))) .thenReturn(new BundleDescriptor.Builder() .setGroupId("mockGroupId") .setArtifactId("mockArtifactId") .setVersion("1.0.0") .setClassifier("mule-service") .setType("jar") .build()); when(descriptorLoaderRepository.get(anyString(), anyObject(), argThat(equalTo(BundleDescriptorLoader.class)))) .thenReturn(bundleDescriptorLoaderMock); when(descriptorLoaderRepository.get(anyString(), anyObject(), argThat(equalTo(ClassLoaderModelLoader.class)))) .thenReturn(mock(ClassLoaderModelLoader.class)); doNothing().when(artifactDescriptorValidator).validate(anyObject()); serviceDescriptorFactory = new ServiceDescriptorFactory(descriptorLoaderRepository, builder()); }
private BundleDependency toBundleDependency(Artifact artifact) { BundleDependency.Builder builder = new BundleDependency.Builder(); if (artifact.getArtifactCoordinates().getScope() != null) { builder.setScope(BundleScope.valueOf(artifact.getArtifactCoordinates().getScope().toUpperCase())); } return builder .setBundleUri(new File(artifactFolder, artifact.getUri().toString()).toURI()) .setDescriptor(new BundleDescriptor.Builder() .setArtifactId(artifact.getArtifactCoordinates().getArtifactId()) .setGroupId(artifact.getArtifactCoordinates().getGroupId()) .setVersion(artifact.getArtifactCoordinates().getVersion()) .setClassifier(artifact.getArtifactCoordinates().getClassifier()) .setType(artifact.getArtifactCoordinates().getType()) .build()) .build(); }
private ClassLoaderModel buildClassLoaderModel(File rootApplication) throws InvalidDescriptorLoaderException { DeployableMavenClassLoaderModelLoader deployableMavenClassLoaderModelLoader = new DeployableMavenClassLoaderModelLoader(mockMavenClient); Map<String, Object> attributes = ImmutableMap.of(org.mule.runtime.module.artifact.api.descriptor.BundleDescriptor.class.getName(), new org.mule.runtime.module.artifact.api.descriptor.BundleDescriptor.Builder() .setGroupId("groupId") .setArtifactId("artifactId") .setVersion("1.0.0") .setType("jar") .setClassifier("mule-application") .build()); return deployableMavenClassLoaderModelLoader.load(rootApplication, attributes, APP); }
private Function<Artifact, BundleDependency> createBundleDependencyFromPackagerDependency(File artifactFile) { return d -> { File bundle = new File(artifactFile, d.getUri().toString()); return new BundleDependency.Builder() .setDescriptor( new BundleDescriptor.Builder().setArtifactId(d.getArtifactCoordinates().getArtifactId()) .setGroupId(d.getArtifactCoordinates().getGroupId()) .setClassifier(d.getArtifactCoordinates().getClassifier()) .setType(d.getArtifactCoordinates().getType()).setVersion(d.getArtifactCoordinates().getVersion()) .build()) .setBundleUri(bundle.toURI()) .build(); }; }
private static BundleDescriptor createTestBundleDescriptor(String artifactId, String version) { return new BundleDescriptor.Builder().setGroupId("test").setArtifactId(artifactId).setVersion(version) .setType(EXTENSION_BUNDLE_TYPE).setClassifier(MULE_PLUGIN_CLASSIFIER).build(); }
private ClassLoaderModel createClassLoaderModelWithDomain() { BundleDescriptor domainDescriptor = new BundleDescriptor.Builder().setArtifactId(DOMAIN_NAME).setGroupId("test") .setVersion("1.0.0").setClassifier(MULE_DOMAIN_CLASSIFIER).build(); Set<BundleDependency> domainDependency = Collections.singleton(new BundleDependency.Builder().setDescriptor(domainDescriptor).build()); return new ClassLoaderModel.ClassLoaderModelBuilder().dependingOn(domainDependency).build(); }
public static BundleDescriptor getApplicationBundleDescriptor(String appName, Optional<String> classifier) { return new BundleDescriptor.Builder().setGroupId("test").setArtifactId(appName) .setVersion("1.0.0").setClassifier(classifier.orElse("mule-application")).build(); }
public static BundleDescriptor getDomainBundleDescriptor(String appName) { return new BundleDescriptor.Builder().setGroupId("test").setArtifactId(appName) .setVersion("1.0.0").setClassifier("mule-domain").build(); } }