Skip to content

Commit

Permalink
First try was way to simple on the approach
Browse files Browse the repository at this point in the history
  • Loading branch information
noctarius committed Jul 4, 2014
1 parent 00fce69 commit f3eb950
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 17 deletions.
12 changes: 12 additions & 0 deletions hazelcast-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@
<transformer
implementation="com.hazelcast.buildutils.HazelcastManifestTransformer">
<mainClass>com.hazelcast.console.ConsoleApp</mainClass>

<overrideInstructions>
<Import-Package>
org.hibernate.*;resolution:=optional,
org.springframework.*;resolution:=optional,
javax.servlet.*;resolution:=optional,
javax.security.*;resolution:=optional,
org.slf4j.*;resolution:=optional,
org.apache.log4j.*;resolution:=optional,
javax.script.*;resolution:=optional
</Import-Package>
</overrideInstructions>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
Expand Down
5 changes: 5 additions & 0 deletions hazelcast-build-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<version>${maven.shade.plugin.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>biz.aQute</groupId>
<artifactId>bndlib</artifactId>
<version>1.50.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

package com.hazelcast.buildutils;

import aQute.lib.osgi.Instruction;
import edu.emory.mathcs.backport.java.util.Arrays;
import edu.emory.mathcs.backport.java.util.Collections;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -40,13 +44,22 @@
* integrating multiple dependencies into one output JAR
*/
public class HazelcastManifestTransformer
implements ResourceTransformer {
extends ManifestResourceTransformer {

private static final String VERSION_PREFIX = "version=";
private static final String RESOLUTION_PREFIX = "resolution:=";
private static final String USES_PREFIX = "uses:=";

private static final int VERSION_OFFSET = 8;
private static final int USES_OFFSET = 7;

private static final String IMPORT_PACKAGE = "Import-Package";
private static final String EXPORT_PACKAGE = "Export-Package";

private final Set<String> importedPackages = new HashSet<String>();
private final Set<String> exportedPackages = new HashSet<String>();
private final Map<String, PackageDefinition> importedPackages = new HashMap<String, PackageDefinition>();
private final Map<String, PackageDefinition> exportedPackages = new HashMap<String, PackageDefinition>();
private final List<InstructionDefinition> importOverrideInstructions = new ArrayList<InstructionDefinition>();
private final List<InstructionDefinition> exportOverrideInstructions = new ArrayList<InstructionDefinition>();

private Manifest shadedManifest;

Expand All @@ -59,9 +72,13 @@ public class HazelcastManifestTransformer
justification = "Filled by Maven")
private String mainClass;

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "UWF_UNWRITTEN_FIELD",
justification = "Filled by Maven")
private Map<String, String> overrideInstructions;

@Override
public boolean canTransformResource(String resource) {
return resource.equals(JarFile.MANIFEST_NAME);
return JarFile.MANIFEST_NAME.equalsIgnoreCase(resource);
}

@Override
Expand All @@ -77,26 +94,73 @@ public void processResource(String resource, InputStream inputStream, List<Reloc
attributes = manifest.getMainAttributes();
}

Set<String> imports = new LinkedHashSet<String>();
Set<String> exports = new LinkedHashSet<String>();

String importPackages = attributes.getValue(IMPORT_PACKAGE);
if (importPackages != null) {
imports.addAll(ElementParser.parseDelimitedString(importPackages, ',', true));
List<String> definitions = ElementParser.parseDelimitedString(importPackages, ',', true);
for (String definition : definitions) {
PackageDefinition packageDefinition = new PackageDefinition(definition);

String packageName = packageDefinition.packageName;
PackageDefinition oldPackageDefinition = importedPackages.get(packageName);
importedPackages.put(packageName, findStrongerDefinition(packageDefinition, oldPackageDefinition));
}
}

String exportPackages = attributes.getValue(EXPORT_PACKAGE);
if (exportPackages != null) {
exports.addAll(ElementParser.parseDelimitedString(exportPackages, ',', true));
}
List<String> definitions = ElementParser.parseDelimitedString(exportPackages, ',', true);
for (String definition : definitions) {
PackageDefinition packageDefinition = new PackageDefinition(definition);

importedPackages.removeAll(exports);
importedPackages.addAll(imports);
exportedPackages.addAll(exports);
String packageName = packageDefinition.packageName;
PackageDefinition oldPackageDefinition = exportedPackages.get(packageName);
exportedPackages.put(packageName, mergeExportUsesConstraint(packageDefinition, oldPackageDefinition));
}
}

IOUtil.close(inputStream);
}

private PackageDefinition findStrongerDefinition(PackageDefinition packageDefinition,
PackageDefinition oldPackageDefinition) {

// If no old definition or new definition is required import we take the new one
if (oldPackageDefinition == null
|| oldPackageDefinition.resolutionOptional && !packageDefinition.resolutionOptional) {
return packageDefinition;
}

// If old definition was required import but new isn't we take the old one
if (!oldPackageDefinition.resolutionOptional && packageDefinition.resolutionOptional) {
return oldPackageDefinition;
}

if (oldPackageDefinition.version == null && packageDefinition.version != null) {
return packageDefinition;
}

if (oldPackageDefinition.version != null && packageDefinition.version == null) {
return oldPackageDefinition;
}

return oldPackageDefinition;
}

private PackageDefinition mergeExportUsesConstraint(PackageDefinition packageDefinition,
PackageDefinition oldPackageDefinition) {

Set<String> uses = new LinkedHashSet<String>();
if (oldPackageDefinition != null) {
uses.addAll(oldPackageDefinition.uses);
}
uses.addAll(packageDefinition.uses);

String packageName = packageDefinition.packageName;
boolean resolutionOptional = packageDefinition.resolutionOptional;
String version = packageDefinition.version;
return new PackageDefinition(packageName, resolutionOptional, version, uses);
}

@Override
public boolean hasTransformedResource() {
return true;
Expand All @@ -110,9 +174,13 @@ public void modifyOutputStream(JarOutputStream jarOutputStream)
shadedManifest = new Manifest();
}

precompileOverrideInstructions();

Attributes attributes = shadedManifest.getMainAttributes();
attributes.putValue(IMPORT_PACKAGE, StringUtils.join(importedPackages.iterator(), ","));
attributes.putValue(EXPORT_PACKAGE, StringUtils.join(exportedPackages.iterator(), ","));
attributes.putValue(IMPORT_PACKAGE, StringUtils.join(shadeImports().iterator(), ","));
attributes.putValue(EXPORT_PACKAGE, StringUtils.join(shadeExports().iterator(), ","));

attributes.putValue("Created-By", "HazelcastManifestTransformer through Shade Plugin");

if (mainClass != null) {
attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
Expand All @@ -126,5 +194,184 @@ public void modifyOutputStream(JarOutputStream jarOutputStream)

jarOutputStream.putNextEntry(new JarEntry(JarFile.MANIFEST_NAME));
shadedManifest.write(jarOutputStream);
jarOutputStream.flush();
}

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_UNWRITTEN_FIELD",
justification = "Field is set by Maven")
private void precompileOverrideInstructions() {
String importPackageInstructions = overrideInstructions.get(IMPORT_PACKAGE);
if (importPackageInstructions != null) {
List<String> packageInstructions = ElementParser.parseDelimitedString(importPackageInstructions, ',', true);
for (String packageInstruction : packageInstructions) {
PackageDefinition packageDefinition = new PackageDefinition(packageInstruction);
Instruction instruction = Instruction.getPattern(packageDefinition.packageName);
System.out.println("Compiled import instruction '" + packageInstruction + "' -> " + instruction);
importOverrideInstructions.add(new InstructionDefinition(packageDefinition, instruction));
}
}
String exportPackageInstructions = overrideInstructions.get(EXPORT_PACKAGE);
if (exportPackageInstructions != null) {
List<String> packageInstructions = ElementParser.parseDelimitedString(exportPackageInstructions, ',', true);
for (String packageInstruction : packageInstructions) {
PackageDefinition packageDefinition = new PackageDefinition(packageInstruction);
Instruction instruction = Instruction.getPattern(packageDefinition.packageName);
System.out.println("Compiled export instruction '" + packageInstruction + "' -> " + instruction);
exportOverrideInstructions.add(new InstructionDefinition(packageDefinition, instruction));
}
}
}

private Set<String> shadeExports() {
Set<String> exports = new LinkedHashSet<String>();
for (Map.Entry<String, PackageDefinition> entry : exportedPackages.entrySet()) {
String definition = entry.getValue().buildDefinition(false);
exports.add(definition);
System.out.println("Adding shaded export -> " + definition);
}
return exports;
}

private Set<String> shadeImports() {
for (String export : exportedPackages.keySet()) {
PackageDefinition definition = new PackageDefinition(export);
importedPackages.remove(definition.packageName);
}
Set<String> imports = new LinkedHashSet<String>();
for (Map.Entry<String, PackageDefinition> entry : importedPackages.entrySet()) {
PackageDefinition original = entry.getValue();
PackageDefinition overridden = overridePackageDefinitionResolution(original);
String definition = overridden.buildDefinition(true);
imports.add(definition);
System.out.println("Adding shaded import -> " + definition);
}
return imports;
}

private PackageDefinition overridePackageDefinitionResolution(PackageDefinition packageDefinition) {
for (InstructionDefinition instructionDefinition : importOverrideInstructions) {
Instruction instruction = instructionDefinition.instruction;
boolean instructed = !instruction.isNegated() == instruction.matches(packageDefinition.packageName);
if (instructed) {
System.out.println("Instruction '" + instruction + "' -> package '" + packageDefinition.packageName + "'");

PackageDefinition override = instructionDefinition.packageDefinition;
String packageName = packageDefinition.packageName;
String version = packageDefinition.version;
Set<String> uses = packageDefinition.uses;
return new PackageDefinition(packageName, override.resolutionOptional, version, uses);
}
}

return packageDefinition;
}

private static final class PackageDefinition {
private final String packageName;
private final boolean resolutionOptional;
private final String version;
private final Set<String> uses;

private PackageDefinition(String definition) {
String[] tokens = definition.split(";");
this.packageName = tokens[0];
this.resolutionOptional = findResolutionConstraint(tokens);
this.version = findVersionConstraint(tokens);
this.uses = findUsesConstraint(tokens);
}

private PackageDefinition(String packageName, boolean resolutionOptional, String version, Set<String> uses) {
this.packageName = packageName;
this.resolutionOptional = resolutionOptional;
this.version = version;
this.uses = new LinkedHashSet<String>(uses);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

PackageDefinition that = (PackageDefinition) o;

if (packageName != null ? !packageName.equals(that.packageName) : that.packageName != null) {
return false;
}

return true;
}

@Override
public int hashCode() {
return packageName != null ? packageName.hashCode() : 0;
}

@Override
public String toString() {
return "PackageDefinition{" + "packageName='" + packageName + '\'' + ", resolutionOptional=" + resolutionOptional
+ ", version='" + version + '\'' + ", uses=" + uses + '}';
}

public String buildDefinition(boolean addResolutionConstraint) {
StringBuilder sb = new StringBuilder(packageName);
if (addResolutionConstraint && resolutionOptional) {
sb.append(";").append(RESOLUTION_PREFIX).append("optional");
}
if (version != null) {
sb.append(";").append(VERSION_PREFIX).append(version);
}
if (uses != null && !uses.isEmpty()) {
sb.append(";").append(USES_PREFIX).append('"').append(StringUtils.join(uses.iterator(), ",")).append('"');
}
return sb.toString();
}

private String findVersionConstraint(String[] tokens) {
for (String token : tokens) {
if (token.startsWith(VERSION_PREFIX)) {
return token.substring(VERSION_OFFSET);
}
}
return null;
}

private boolean findResolutionConstraint(String[] tokens) {
for (String token : tokens) {
if (token.startsWith(RESOLUTION_PREFIX)) {
return token.equalsIgnoreCase("resolution:=optional");
}
}
return false;
}

private Set<String> findUsesConstraint(String[] tokens) {
for (String token : tokens) {
if (token.startsWith(USES_PREFIX)) {
String packages = token.substring(USES_OFFSET, token.length() - 1);
String[] sepPackages = packages.split(",");
return new LinkedHashSet<String>(Arrays.asList(sepPackages));
}
}
return Collections.emptySet();
}
}

private static final class InstructionDefinition {
private final PackageDefinition packageDefinition;
private final Instruction instruction;

private InstructionDefinition(PackageDefinition packageDefinition, Instruction instruction) {
this.packageDefinition = packageDefinition;
this.instruction = instruction;
}

@Override
public String toString() {
return "InstructionDefinition{" + "packageDefinition=" + packageDefinition + ", instruction=" + instruction + '}';
}
}
}

0 comments on commit f3eb950

Please sign in to comment.