BundleDescriptor requiredDescriptor = new BundleDescriptor.Builder() .setGroupId(groupId) .setArtifactId(artifactId) .setVersion(version) .setClassifier(classifier) .setType(type) .build(); URLClassLoader classLoader = descriptorMapping.get(requiredDescriptor); if (classLoader != null) {
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 { Model model; return new BundleDescriptor.Builder() .setArtifactId(model.getArtifactId()) .setGroupId(model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId()) .setVersion(model.getVersion() != null ? model.getVersion() : model.getParent().getVersion()) .setType(EXTENSION_BUNDLE_TYPE) .setClassifier(MULE_PLUGIN_CLASSIFIER) .build();
applicationPolicyInstance3); policyTemplateDescriptor1.setBundleDescriptor(new BundleDescriptor.Builder().setArtifactId(POLICY_NAME1).setGroupId("test") .setVersion("1.0").build()); policyTemplateDescriptor2.setBundleDescriptor(new BundleDescriptor.Builder().setArtifactId(POLICY_NAME2).setGroupId("test") .setVersion("2.0").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 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 BundleDescriptor convertBundleDescriptor(org.mule.maven.client.api.model.BundleDescriptor descriptor) { BundleDescriptor.Builder builder = new BundleDescriptor.Builder().setGroupId(descriptor.getGroupId()) .setArtifactId(descriptor.getArtifactId()) // Use baseVersion as it will refer to the unresolved meta version (case of SNAPSHOTS instead of timestamp versions) .setVersion(descriptor.getBaseVersion()) .setType(descriptor.getType()); descriptor.getClassifier().ifPresent(builder::setClassifier); return builder.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(); }
String normalizedResource = normalize(resource, true); BundleDescriptor requestDescriptor = new BundleDescriptor.Builder() .setGroupId(groupId) .setArtifactId(artifactId) .setVersion(version) .setClassifier(classifier) .setType(type) .build();
new org.mule.runtime.module.artifact.api.descriptor.BundleDescriptor.Builder() .setGroupId("groupId") .setArtifactId("artifactId") .setVersion("1.0.0") .setType("jar") .setClassifier("mule-application") .build());
String pluginGroupId = getAttribute(plugin, GROUP_ID); String pluginArtifactId = getAttribute(plugin, ARTIFACT_ID); BundleDescriptor mulePluginDescriptor = new BundleDescriptor.Builder() .setGroupId(pluginGroupId) .setArtifactId(pluginArtifactId) .setVersion("-") .build(); Set<BundleDescriptor> mulePluginAdditionalLibraries = new HashSet<>(); Xpp3Dom dependenciesDom = plugin.getChild(PLUGIN_DEPENDENCIES_FIELD); String dependencyArtifactId = getAttribute(dependency, ARTIFACT_ID); String dependencyVersion = getAttribute(dependency, VERSION); mulePluginAdditionalLibraries.add(new BundleDescriptor.Builder() .setGroupId(dependencyGroupId) .setArtifactId(dependencyArtifactId) .setVersion(dependencyVersion) .build());
applicationPolicyInstance3); policyTemplateDescriptor1.setBundleDescriptor(new BundleDescriptor.Builder().setArtifactId(POLICY_NAME1).setGroupId("test") .setVersion("1.0").build()); policyTemplateDescriptor2.setBundleDescriptor(new BundleDescriptor.Builder().setArtifactId(POLICY_NAME2).setGroupId("test") .setVersion("2.0").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); } }
@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(); }
private BundleDescriptor getBundleDescriptor(File artifactFile, ArtifactType artifactType) { BundleDescriptor.Builder builder = new BundleDescriptor.Builder(); pomProperties = getPomPropertiesFromJar(artifactFile); return builder.setGroupId(pomProperties.getProperty("groupId")) .setArtifactId(pomProperties.getProperty("artifactId")) .setVersion(pomProperties.getProperty("version")) .setClassifier(artifactType.equals(APP) ? MULE_APPLICATION_CLASSIFIER : MULE_DOMAIN_CLASSIFIER) .build(); } else { Model model; return new BundleDescriptor.Builder() .setArtifactId(model.getArtifactId()) .setGroupId(model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId()) .setVersion(model.getVersion() != null ? model.getVersion() : model.getParent().getVersion()) .setType(JAR) .setClassifier(artifactType.equals(PLUGIN) ? MULE_PLUGIN_CLASSIFIER : model.getPackaging()) .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); } } }
@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()); }
@Test public void providedPluginsHaveOldestVersionOfSameHttpPluginInPolicy() { BundleDescriptor httpDescriptor1_0 = new BundleDescriptor.Builder() .setGroupId(MULE_HTTP_CONNECTOR_GROUP_ID) .setArtifactId(MULE_HTTP_CONNECTOR_ARTIFACT_ID) .setVersion("1.0.0") .build(); BundleDescriptor httpDescriptor1_1 = new BundleDescriptor.Builder() .setGroupId(MULE_HTTP_CONNECTOR_GROUP_ID) .setArtifactId(MULE_HTTP_CONNECTOR_ARTIFACT_ID) .setVersion("1.1.0") .build(); ArtifactPluginDescriptor httpPluginDescriptor1_0 = newArtifactPluginDescriptor("HTTP"); httpPluginDescriptor1_0.setBundleDescriptor(httpDescriptor1_0); ArtifactPluginDescriptor httpPluginDescriptor1_1 = newArtifactPluginDescriptor("HTTP"); httpPluginDescriptor1_1.setBundleDescriptor(httpDescriptor1_1); expectedException.expect(IllegalStateException.class); expectedException .expectMessage("Incompatible version of plugin 'HTTP' (org.mule.modules:mule-http-connector) found. Artifact requires version '1.1.0' but context provides version '1.0.0'"); dependenciesResolver.resolve(ImmutableSet.of(httpPluginDescriptor1_0), ImmutableList.of(httpPluginDescriptor1_1), false); }
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)); }