Skip to content

Commit

Permalink
Merge pull request #241 from jsight/WINDUP-243
Browse files Browse the repository at this point in the history
WINDUP-243: Port Brad's Archive Hash visitor to windup 2.x
  • Loading branch information
lincolnthree committed Sep 8, 2014
2 parents db242a8 + 5cd5008 commit b53950f
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
@@ -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());
}
}
Expand Up @@ -76,7 +76,15 @@ public void addTypeToElement(Class<? extends VertexFrame> kind, Element element)
String typeFieldName = typeHoldingTypeField.getAnnotation(TypeField.class).value();
String typeValue = typeValueAnnotation.value();

// Store the type value in a delimited list.
for (TitanProperty existingTypes : v.getProperties(typeFieldName))
{
if (existingTypes.getValue().toString().equals(typeValue))
{
// this is already in the list, so just exit now
return;
}
}

v.addProperty(typeFieldName, typeValue);
addSuperclassType(kind, element);
}
Expand Down
@@ -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);
}
}
}
Expand Up @@ -13,6 +13,9 @@
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.windup.engine.WindupProcessor;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.dao.ArchiveService;
import org.jboss.windup.graph.model.ArchiveModel;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -56,5 +59,20 @@ public static ForgeArchive getDeployment()
public void testRunWindupTiny() throws Exception
{
super.runTest(processor, graphContext, "../../test-files/Windup1x-javaee-example-tiny.war", false);
validateArchiveHashes();
}

private void validateArchiveHashes() throws Exception
{
ArchiveService archiveService = new ArchiveService(graphContext);
int numberFound = 0;
for (ArchiveModel model : archiveService.findAll())
{
numberFound++;

Assert.assertEquals("c60bb0c51623a915cb4a9a90ba9ba70e", model.getMD5Hash());
Assert.assertEquals("1a1888023eff8629a9e55f023c8ecf63f69fad03", model.getSHA1Hash());
}
Assert.assertEquals(1, numberFound);
}
}

0 comments on commit b53950f

Please sign in to comment.