Skip to content

Commit

Permalink
WINDUP-241: Port Brad's Manifest Visitor to Windup 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jsight committed Sep 4, 2014
1 parent 41f1f5c commit 824f221
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 73 deletions.
@@ -1,21 +1,18 @@
package org.jboss.windup.graph.dao;

import java.util.Iterator;
import java.util.StringTokenizer;

import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.WindupVertexFrame;
import org.jboss.windup.graph.model.resource.ResourceModel;
import org.jboss.windup.graph.model.resource.FileModel;
import org.jboss.windup.graph.service.GraphService;

import com.thinkaurelius.titan.core.attribute.Cmp;
import com.thinkaurelius.titan.core.attribute.Text;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.PipeFunction;

/**
* Provides methods for searching, creating, and deleting ArchiveModel Vertices.
*
* @author jsightler <jesse.sightler@gmail.com>
*
*/
public class ArchiveService extends GraphService<ArchiveModel>
{
public ArchiveService()
Expand All @@ -28,43 +25,43 @@ public ArchiveService(GraphContext context)
super(context, ArchiveModel.class);
}

public Iterable<ArchiveModel> findAllRootArchives()
/**
* Finds the file at the provided path within the archive.
*
* Eg, getChildFile(ArchiveModel, "/META-INF/MANIFEST.MF") will return a {@link FileModel} if a file named
* /META-INF/MANIFEST.MF exists within the archive
*
* This function expects filePath to use "/" characters to index within the archive, regardless of the underlying
* operating system platform being used.
*
* @return Returns the located {@link FileModel} or null if no file with this path could be located
*/
public FileModel getChildFile(ArchiveModel archiveModel, String filePath)
{
// iterate through all vertices
Iterable<Vertex> pipeline = new GremlinPipeline<Vertex, Vertex>(getTypedQuery())
// check to see whether there is an edge coming in that links to the resource providing the java
// class model.
.filter(new PipeFunction<Vertex, Boolean>()
{
public Boolean compute(Vertex argument)
{
Iterator<Edge> edges = argument.getEdges(Direction.IN, "child").iterator();
if (!edges.hasNext())
{
return true;
}
// if there aren't two edges, return false.
return false;
}
});
return getGraphContext().getFramed().frameVertices(pipeline, ArchiveModel.class);
}
StringTokenizer stk = new StringTokenizer(filePath, "/");

public boolean isArchiveResource(ResourceModel resource)
{
return (new GremlinPipeline<Vertex, Vertex>(resource.asVertex())).out("archiveResourceFacet").iterator()
.hasNext();
FileModel currentFileModel = archiveModel.getUnzippedDirectory();

while (stk.hasMoreTokens() && currentFileModel != null)
{
String pathElement = stk.nextToken();

currentFileModel = findFileModel(currentFileModel, pathElement);
}
return currentFileModel;
}

public ArchiveModel getArchiveFromResource(ResourceModel resource)
private FileModel findFileModel(FileModel fm, String pathElement)
{
Iterator<Vertex> v = (new GremlinPipeline<Vertex, Vertex>(resource.asVertex())).out("archiveResourceFacet")
.iterator();
if (v.hasNext())
FileModel result = null;
for (FileModel child : fm.getFilesInDirectory())
{
return getGraphContext().getFramed().frame(v.next(), ArchiveModel.class);
if (child.getFileName().equals(pathElement))
{
result = child;
break;
}
}

return null;
return result;
}
}
Expand Up @@ -63,7 +63,7 @@ public interface FileModel extends ResourceModel

@Property(MD5_HASH)
public void setMD5Hash(String md5Hash);

@Property(SHA1_HASH)
public String getSHA1Hash();

Expand Down Expand Up @@ -135,7 +135,16 @@ public String getPrettyPathWithinProject()
else
{
FileModel projectModelFileModel = projectModel.getRootFileModel();
Path projectPath = Paths.get(projectModelFileModel.getFilePath());
Path projectPath;
if (projectModelFileModel instanceof ArchiveModel)
{
ArchiveModel archiveModelForProject = (ArchiveModel) projectModelFileModel;
projectPath = Paths.get(archiveModelForProject.getUnzippedDirectory().getFilePath());
}
else
{
projectPath = Paths.get(projectModelFileModel.getFilePath());
}

List<String> paths = generatePathList(projectPath);
return generatePathString(paths);
Expand Down Expand Up @@ -191,7 +200,6 @@ private void appendPath(List<String> paths, Path stopPath, FileModel fileModel)

if (fileModel.getParentFile() != null)
{

FileModel parent = fileModel.getParentFile();
appendPath(paths, stopPath, parent);
}
Expand Down
@@ -1,29 +1,24 @@
package org.jboss.windup.rules.apps.java.model;

import java.util.Set;

import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.resource.FileModel;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.frames.Adjacency;
import com.tinkerpop.frames.modules.javahandler.JavaHandler;
import com.tinkerpop.frames.modules.typedgraph.TypeValue;

@TypeValue("JarManifestMeta")
/**
* Contains information from the META-INF/MANIFEST.MF file within an archive.
*/
@TypeValue(JarManifestModel.TYPE)
public interface JarManifestModel extends FileModel
{
@Adjacency(label = "archive", direction = Direction.IN)
public void setJarArchive(final JarArchiveModel archive);

@Adjacency(label = "archive", direction = Direction.IN)
public JarArchiveModel getJarArchive();

@JavaHandler
public String getProperty(String property);
public static final String TYPE = "JarManifestModel";
public static final String ARCHIVE = "archive";

@JavaHandler
public void setProperty(String propertyName, String obj);
@Adjacency(label = ARCHIVE, direction = Direction.IN)
public void setArchive(final ArchiveModel archive);

@JavaHandler
public Set<String> keySet();
@Adjacency(label = ARCHIVE, direction = Direction.IN)
public ArchiveModel getArchive();
}
Expand Up @@ -131,7 +131,7 @@ private void unzipToTempDirectory(final GraphContext context, final Path tempFol
newFileModel.setParentArchive(archiveModel);

// add all unzipped files, and make sure their parent archive is set
recurseAndAddFiles(context, tempFolder, fileService, archiveModel, newFileModel, false);
recurseAndAddFiles(context, tempFolder, fileService, archiveModel, newFileModel);
}

/**
Expand All @@ -143,7 +143,7 @@ private void unzipToTempDirectory(final GraphContext context, final Path tempFol
*/
private void recurseAndAddFiles(GraphContext context, Path tempFolder, FileModelService fileService,
ArchiveModel archiveModel,
FileModel parentFileModel, boolean setParentFile)
FileModel parentFileModel)
{
File fileReference = parentFileModel.asFile();

Expand All @@ -154,15 +154,7 @@ private void recurseAndAddFiles(GraphContext context, Path tempFolder, FileModel
{
for (File subFile : subFiles)
{
FileModel subFileModel;
if (setParentFile)
{
subFileModel = fileService.createByFilePath(parentFileModel, subFile.getAbsolutePath());
}
else
{
subFileModel = fileService.createByFilePath(subFile.getAbsolutePath());
}
FileModel subFileModel = fileService.createByFilePath(parentFileModel, subFile.getAbsolutePath());
subFileModel.setParentArchive(archiveModel);

if (subFile.isFile() && ZipUtil.endsWithZipExtension(subFileModel.getFilePath()))
Expand All @@ -178,7 +170,7 @@ private void recurseAndAddFiles(GraphContext context, Path tempFolder, FileModel

if (subFile.isDirectory())
{
recurseAndAddFiles(context, tempFolder, fileService, archiveModel, subFileModel, true);
recurseAndAddFiles(context, tempFolder, fileService, archiveModel, subFileModel);
}
}
}
Expand Down
@@ -0,0 +1,105 @@
package org.jboss.windup.rules.apps.java.scan.provider;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.RulePhase;
import org.jboss.windup.config.WindupRuleProvider;
import org.jboss.windup.config.operation.ruleelement.AbstractIterationOperation;
import org.jboss.windup.config.query.Query;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.dao.ArchiveService;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.resource.FileModel;
import org.jboss.windup.rules.apps.java.model.JarManifestModel;
import org.jboss.windup.rules.apps.java.service.JarManifestService;
import org.ocpsoft.rewrite.config.ConditionBuilder;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.context.EvaluationContext;

/**
* Discovers MANIFEST.MF files within archives.
*
* @author jsightler <jesse.sightler@gmail.com>
*
*/
public class DiscoverArchiveManifestFilesRuleProvider extends WindupRuleProvider
{
private static final Logger LOG = Logger.getLogger(DiscoverArchiveManifestFilesRuleProvider.class.getSimpleName());

@Inject
private ArchiveService archiveService;

@Inject
private JarManifestService jarManifestService;

@Override
public RulePhase getPhase()
{
return RulePhase.DISCOVERY;
}

@Override
public List<Class<? extends WindupRuleProvider>> getExecuteAfter()
{
return asClassList(UnzipArchivesToOutputRuleProvider.class);
}

@Override
public Configuration getConfiguration(GraphContext context)
{
ConditionBuilder archivesFound = Query.find(ArchiveModel.class);

return ConfigurationBuilder.begin()
.addRule()
.when(archivesFound)
.perform(new ExtractManifestInformationFromArchive());
}

private class ExtractManifestInformationFromArchive extends AbstractIterationOperation<ArchiveModel>
{
@Override
public void perform(GraphRewrite event, EvaluationContext context, ArchiveModel payload)
{
FileModel manifestFile = archiveService.getChildFile(payload, "META-INF/MANIFEST.MF");
if (manifestFile == null)
{
// no manifest found, skip this one
return;
}

JarManifestModel jarManifest = jarManifestService.addTypeToModel(manifestFile);
jarManifest.setArchive(payload);

try (InputStream is = manifestFile.asInputStream())
{
Manifest manifest = new Manifest(is);
if (manifest == null || manifest.getMainAttributes().size() == 0)
{
return;
}

for (Object key : manifest.getMainAttributes().keySet())
{
String property = StringUtils.trim(key.toString());
String propertyValue = StringUtils.trim(manifest.getMainAttributes().get(key).toString());
jarManifest.asVertex().setProperty(property, propertyValue);
}
}
catch (IOException e)
{
LOG.log(Level.WARNING, "Exception reading manifest from file: " + manifestFile.getFilePath(), e);
}
}
}

}
@@ -0,0 +1,28 @@
package org.jboss.windup.rules.apps.java.service;

import javax.inject.Inject;

import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.service.GraphService;
import org.jboss.windup.rules.apps.java.model.JarManifestModel;

/**
* Manages the creation, querying, and deletion of {@link JarManifestModel}s.
*
* @author jsightler <jesse.sightler@gmail.com>
*
*/
public class JarManifestService extends GraphService<JarManifestModel>
{

public JarManifestService()
{
super(JarManifestModel.class);
}

@Inject
public JarManifestService(GraphContext context)
{
super(context, JarManifestModel.class);
}
}

0 comments on commit 824f221

Please sign in to comment.