Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from jsight/WINDUP-243
WINDUP-243: Port Brad's Archive Hash visitor to windup 2.x
- Loading branch information
Showing
4 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
config/api/src/main/java/org/jboss/windup/config/IteratingRuleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jboss.windup.config; | ||
|
||
import org.jboss.windup.config.operation.ruleelement.AbstractIterationOperation; | ||
import org.jboss.windup.graph.GraphContext; | ||
import org.jboss.windup.graph.model.WindupVertexFrame; | ||
import org.ocpsoft.rewrite.config.ConditionBuilder; | ||
import org.ocpsoft.rewrite.config.Configuration; | ||
import org.ocpsoft.rewrite.config.ConfigurationBuilder; | ||
import org.ocpsoft.rewrite.context.EvaluationContext; | ||
|
||
/** | ||
* This provides a simplified way to extend {@link WindupRuleProvider} for cases where the rule simply needs to provide | ||
* some query, and wants to execute a function over each resulting row. | ||
* | ||
* @author jsightler <jesse.sightler@gmail.com> | ||
*/ | ||
public abstract class IteratingRuleProvider<PAYLOADTYPE extends WindupVertexFrame> extends WindupRuleProvider | ||
{ | ||
/** | ||
* Gets the condition for the {@link Configuration}'s "when" clause. | ||
*/ | ||
public abstract ConditionBuilder getCondition(); | ||
|
||
/** | ||
* Perform this function for each {@link WindupVertexFrame} returned by the "when" clause. | ||
*/ | ||
public abstract void perform(GraphRewrite event, EvaluationContext context, PAYLOADTYPE payload); | ||
|
||
private class IterationOperation extends AbstractIterationOperation<PAYLOADTYPE> | ||
{ | ||
@Override | ||
public void perform(GraphRewrite event, EvaluationContext context, PAYLOADTYPE payload) | ||
{ | ||
IteratingRuleProvider.this.perform(event, context, payload); | ||
} | ||
} | ||
|
||
@Override | ||
public final Configuration getConfiguration(GraphContext context) | ||
{ | ||
return ConfigurationBuilder.begin() | ||
.addRule() | ||
.when(getCondition()) | ||
.perform(new IterationOperation()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...rc/main/java/org/jboss/windup/rules/apps/java/scan/provider/HashArchivesRuleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.jboss.windup.rules.apps.java.scan.provider; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.jboss.windup.config.GraphRewrite; | ||
import org.jboss.windup.config.IteratingRuleProvider; | ||
import org.jboss.windup.config.RulePhase; | ||
import org.jboss.windup.config.WindupRuleProvider; | ||
import org.jboss.windup.config.query.Query; | ||
import org.jboss.windup.graph.model.ArchiveModel; | ||
import org.jboss.windup.util.exception.WindupException; | ||
import org.ocpsoft.rewrite.config.ConditionBuilder; | ||
import org.ocpsoft.rewrite.context.EvaluationContext; | ||
|
||
/** | ||
* This RuleProvider gets the MD5 and SHA1 hash for each {@link ArchiveModel} in the graph. | ||
* | ||
* @author jsightler <jesse.sightler@gmail.com> | ||
* | ||
*/ | ||
public class HashArchivesRuleProvider extends IteratingRuleProvider<ArchiveModel> | ||
{ | ||
|
||
@Override | ||
protected ConditionBuilder getCondition() | ||
{ | ||
return Query.find(ArchiveModel.class); | ||
} | ||
|
||
@Override | ||
public RulePhase getPhase() | ||
{ | ||
return RulePhase.DISCOVERY; | ||
} | ||
|
||
@Override | ||
public List<Class<? extends WindupRuleProvider>> getExecuteAfter() | ||
{ | ||
return asClassList(UnzipArchivesToOutputRuleProvider.class); | ||
} | ||
|
||
@Override | ||
protected void perform(GraphRewrite event, EvaluationContext context, ArchiveModel payload) | ||
{ | ||
try (InputStream is = payload.asInputStream()) | ||
{ | ||
String md5 = DigestUtils.md5Hex(is); | ||
payload.setMD5Hash(md5); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new WindupException("Failed to read archive file at: " + payload.getFilePath() + " due to: " | ||
+ e.getMessage(), e); | ||
} | ||
try (InputStream is = payload.asInputStream()) | ||
{ | ||
String sha1 = DigestUtils.sha1Hex(is); | ||
payload.setSHA1Hash(sha1); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new WindupException("Failed to read archive file at: " + payload.getFilePath() + " due to: " | ||
+ e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters