Skip to content

Commit

Permalink
[BACKPORT 3.12.z] Fix the HazelcastManifestTransformer configuration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kwart committed Jan 26, 2021
1 parent e2ee942 commit 1a9f7d4
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 31 deletions.
6 changes: 6 additions & 0 deletions hazelcast-all/pom.xml
Expand Up @@ -108,6 +108,12 @@
javax.naming.*;resolution:=optional
</Import-Package>
</overrideInstructions>
<manifestEntries>
<Bundle-Name>Hazelcast(All)</Bundle-Name>
</manifestEntries>
<removeEntries>
<Automatic-Module-Name/>
</removeEntries>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
Expand Down
Expand Up @@ -24,4 +24,10 @@ public class CheckAllDependenciesIT extends CheckDependenciesIT {
protected boolean isMatching(String urlString) {
return urlString.contains("hazelcast-all-") && urlString.contains("target");
}

@Override
protected String getBundleName() {
return "Hazelcast(All)";
}

}
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
Expand Down Expand Up @@ -62,17 +61,11 @@ public class HazelcastManifestTransformer extends ManifestResourceTransformer {
private static final String IMPORT_PACKAGE = "Import-Package";
private static final String EXPORT_PACKAGE = "Export-Package";

private static final Name AUTOMATIC_MODULE_NAME = new Name("Automatic-Module-Name");

// configuration
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Filled by Maven")
String mainClass;

@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Filled by Maven")
Map<String, Attributes> manifestEntries;

@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Filled by Maven")
Map<String, String> overrideInstructions;
private String mainClass;
private Map<String, Object> manifestEntries;
private Map<String, Object> removeEntries;
private Map<String, String> overrideInstructions;

private final Map<String, PackageDefinition> importedPackages = new HashMap<String, PackageDefinition>();
private final Map<String, PackageDefinition> exportedPackages = new HashMap<String, PackageDefinition>();
Expand All @@ -81,6 +74,26 @@ public class HazelcastManifestTransformer extends ManifestResourceTransformer {

private Manifest shadedManifest;

@Override
public void setMainClass(String mainClass) {
this.mainClass = mainClass;
super.setMainClass(mainClass);
}

@Override
public void setManifestEntries(Map<String, Object> manifestEntries) {
this.manifestEntries = manifestEntries;
super.setManifestEntries(manifestEntries);
}

public void setOverrideInstructions(Map<String, String> overrideInstructions) {
this.overrideInstructions = overrideInstructions;
}

public void setRemoveEntries(Map<String, Object> removeEntries) {
this.removeEntries = removeEntries;
}

@Override
public boolean canTransformResource(String resource) {
return JarFile.MANIFEST_NAME.equalsIgnoreCase(resource);
Expand Down Expand Up @@ -193,13 +206,16 @@ public void modifyOutputStream(JarOutputStream jarOutputStream) throws IOExcepti
}

if (manifestEntries != null) {
for (Map.Entry<String, Attributes> entry : manifestEntries.entrySet()) {
for (Map.Entry<String, Object> entry : manifestEntries.entrySet()) {
attributes.put(new Attributes.Name(entry.getKey()), entry.getValue());
}
}

// the Manifest in hazelcast-all uberjar won't have the Automatic-Module-Name
attributes.remove(AUTOMATIC_MODULE_NAME);
if (removeEntries != null) {
for (Map.Entry<String, Object> entry : removeEntries.entrySet()) {
attributes.remove(new Attributes.Name(entry.getKey()));
}
}

jarOutputStream.putNextEntry(new JarEntry(JarFile.MANIFEST_NAME));
shadedManifest.write(jarOutputStream);
Expand Down
Expand Up @@ -31,7 +31,6 @@
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

Expand Down Expand Up @@ -64,9 +63,9 @@ public void setUp() throws Exception {

transformer = new HazelcastManifestTransformer();

transformer.mainClass = "com.hazelcast.core.server.StartServer";
transformer.manifestEntries = new HashMap<String, Attributes>();
transformer.overrideInstructions = new HashMap<String, String>();
transformer.setMainClass("com.hazelcast.core.server.StartServer");
transformer.setManifestEntries(new HashMap<String, Object>());
transformer.setOverrideInstructions(new HashMap<String, String>());
}

@After
Expand Down
1 change: 1 addition & 0 deletions hazelcast-client/pom.xml
Expand Up @@ -102,6 +102,7 @@
</Import-Package>
<Fragment-Host>com.hazelcast</Fragment-Host>
<!--<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>-->
<Bundle-Name>Hazelcast(Client)</Bundle-Name>
</instructions>
</configuration>
</execution>
Expand Down
Expand Up @@ -24,4 +24,14 @@ public class ClientCheckDependenciesIT extends CheckDependenciesIT {
protected boolean isMatching(String urlString) {
return urlString.contains("hazelcast-client-") && urlString.contains("target");
}

@Override
protected String getBundleName() {
return "Hazelcast(Client)";
}

@Override
protected boolean hasMainClass() {
return false;
}
}
1 change: 1 addition & 0 deletions hazelcast/pom.xml
Expand Up @@ -142,6 +142,7 @@
org.jruby.embed.jsr223;resolution:=optional,
*
</Import-Package>
<Bundle-Name>Hazelcast(Core)</Bundle-Name>
</instructions>
</configuration>
</execution>
Expand Down
61 changes: 48 additions & 13 deletions hazelcast/src/test/java/com/hazelcast/osgi/CheckDependenciesIT.java
Expand Up @@ -16,24 +16,30 @@

package com.hazelcast.osgi;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.nio.IOUtil;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static org.junit.Assert.fail;
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.BuildInfoProvider;
import com.hazelcast.nio.IOUtil;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.util.StringUtil;

@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
Expand All @@ -55,10 +61,14 @@ public class CheckDependenciesIT extends HazelcastTestSupport {
"javax.security.auth",
"javax.transaction.xa",
"javax.xml",
"javax.naming",

// these 2 XML-related packages are part of the platform since Java SE 6
"org.xml.sax",
"org.w3c.dom",

// GSS-API (& Kerberos) related classes - part of JDK since 1.4
"org.ietf.jgss"
};

@Test
Expand All @@ -75,7 +85,28 @@ public void testNoMandatoryDependencyDeclared() throws IOException, InterruptedE
}
}

private Manifest getHazelcastManifest() throws IOException {
/**
* Verify the {@code HazelcastManifestTransformer} was properly used.
*/
@Test
public void verifyManifestEntries() throws IOException {
Manifest mf = getHazelcastManifest();
Attributes mainAttributes = mf.getMainAttributes();
assertEquals("Unexpected Bundle-Name attribute value", getBundleName(), mainAttributes.getValue("Bundle-Name"));
if (hasMainClass()) {
assertNotNull("The Main-Class attribute is expected", mainAttributes.getValue("Main-Class"));
}
}

protected boolean hasMainClass() {
return true;
}

protected String getBundleName() {
return "Hazelcast(Core)";
}

protected Manifest getHazelcastManifest() throws IOException {
URL hazelcastAllManifestUrl = findHazelcastManifestURL();
InputStream inputStream = null;
try {
Expand Down Expand Up @@ -133,6 +164,10 @@ protected String[] getWhitelistPrefixes() {
}

protected boolean isMatching(String urlString) {
return urlString.contains("hazelcast-3.") && urlString.contains("target");
return urlString.contains("hazelcast-" + getMajorVersion() + ".") && urlString.contains("target");
}

protected String getMajorVersion() {
return StringUtil.tokenizeVersionString(BuildInfoProvider.getBuildInfo().getVersion())[0];
}
}

0 comments on commit 1a9f7d4

Please sign in to comment.