Skip to content

Commit

Permalink
Merge pull request #360 from jsight/WINDUP-371
Browse files Browse the repository at this point in the history
WINDUP-371: Implement Roaster WildcardImportResolver SPI using Project's...
  • Loading branch information
lincolnthree committed Dec 1, 2014
2 parents e4fca96 + a4598be commit 7dea02f
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import java.util.Map;

import com.thinkaurelius.titan.core.TitanGraph;
import com.tinkerpop.blueprints.util.wrappers.event.EventGraph;
import com.tinkerpop.frames.FramedGraph;
import org.jboss.windup.graph.GraphContext;

/**
* Listen to events related to {@link GraphContext} initialization.
*/
public interface AfterGraphInitializationListener
{

void process(Map<String, Object> configuration, FramedGraph<EventGraph<TitanGraph>> graph);
/**
* Called after the {@link GraphContext} has been initialized.
*/
void afterGraphStarted(Map<String, Object> graphConfiguration, GraphContext graphContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.addons.Addon;
import org.jboss.forge.furnace.services.Imported;
import org.jboss.windup.graph.frames.TypeAwareFramedGraphQuery;
import org.jboss.windup.graph.listeners.AfterGraphInitializationListener;
Expand Down Expand Up @@ -156,7 +155,7 @@ public Graph configure(Graph baseGraph, FramedGraphConfiguration config)
{
for (AfterGraphInitializationListener listener : afterInitializationListeners)
{
listener.process(confProps, framed);
listener.afterGraphStarted(confProps, this);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions rexster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<optional>true</optional>
</dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.io.FileUtils;
import org.jboss.forge.furnace.addons.Addon;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.listeners.AfterGraphInitializationListener;

import com.thinkaurelius.titan.core.TitanGraph;
Expand All @@ -37,16 +35,19 @@ public class RexsterInitializer implements AfterGraphInitializationListener
public RexsterInitializer()
{
}

private Addon getAddon() {
Set<Addon> addons = SimpleContainer.getFurnace(RexsterInitializer.class.getClassLoader()).getAddonRegistry().getAddons();
for(Addon addon:addons) {
boolean isRexster =addon.getId().getName().contains("rexster");
if(isRexster) {
return addon;
}
}
return null;

private Addon getAddon()
{
Set<Addon> addons = SimpleContainer.getFurnace(RexsterInitializer.class.getClassLoader()).getAddonRegistry().getAddons();
for (Addon addon : addons)
{
boolean isRexster = addon.getId().getName().contains("rexster");
if (isRexster)
{
return addon;
}
}
return null;
}

public void start(FramedGraph<EventGraph<TitanGraph>> graph)
Expand All @@ -58,7 +59,7 @@ public void start(FramedGraph<EventGraph<TitanGraph>> graph)

RexsterProperties properties = new RexsterProperties("rexster.xml");
File rexsterAddonDir = new File(getClass().getResource("/public").getPath().split("!")[0].split(":")[1]);

new File(rexsterExtractDirectory).mkdirs();
extractZipFile(rexsterAddonDir, rexsterExtractDirectory);
configureScriptEngine(properties);
Expand All @@ -78,26 +79,29 @@ public void start(FramedGraph<EventGraph<TitanGraph>> graph)
log.warning("Error while creating rexster.xml");
}
}


private void extractZipFile(File jarFile,String destDir) throws IOException {
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
if (file.isDirectory()) { // if its a directory, create it
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0) { // write contents of 'is' to 'fos'
fos.write(is.read());
}
fos.close();
is.close();
}

private void extractZipFile(File jarFile, String destDir) throws IOException
{
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements())
{
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
if (file.isDirectory())
{ // if its a directory, create it
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0)
{ // write contents of 'is' to 'fos'
fos.write(is.read());
}
fos.close();
is.close();
}
}

private void configureScriptEngine(RexsterProperties properties)
Expand All @@ -123,8 +127,8 @@ private String createRexsterXmlFileString(Map<String, Object> conf)
" <server-port>8182</server-port>\n" +
" <server-host>0.0.0.0</server-host>\n" +
" <base-uri>http://localhost</base-uri>\n" +
" <web-root>" + rexsterExtractDirectory +"/public</web-root>\n" +
// " <http.web-root> </http.web-root> \n" +
" <web-root>" + rexsterExtractDirectory + "/public</web-root>\n" +
// " <http.web-root> </http.web-root> \n" +
" <character-set>UTF-8</character-set>\n" +
" <enable-jmx>false</enable-jmx>\n" +
" <enable-doghouse>true</enable-doghouse>\n" +
Expand Down Expand Up @@ -233,10 +237,10 @@ private String createRexsterXmlFileString(Map<String, Object> conf)
}

@Override
public void process(Map<String, Object> configuration, FramedGraph<EventGraph<TitanGraph>> graph)
public void afterGraphStarted(Map<String, Object> configuration, GraphContext graphContext)
{
rexsterExtractDirectory =getAddon().getRepository().getAddonDescriptor(getAddon().getId()).getParent() + "/rexster-extract";
rexsterExtractDirectory = getAddon().getRepository().getAddonDescriptor(getAddon().getId()).getParent() + "/rexster-extract";
configurationString = createRexsterXmlFileString(configuration);
start(graph);
start(graphContext.getFramed());
}
}
2 changes: 1 addition & 1 deletion rules-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-jdt</artifactId>
<version>2.6.0.Final</version>
<version>2.9.0.Final</version>
</dependency>
<dependency>
<groupId>org.apache.bcel</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
Expand Down Expand Up @@ -53,7 +54,6 @@
import org.jboss.windup.rules.apps.java.model.JavaClassModel;
import org.jboss.windup.rules.apps.java.service.JavaClassService;
import org.jboss.windup.rules.apps.java.service.TypeReferenceService;
import java.util.logging.Logger;
import org.jboss.windup.util.Logging;

/**
Expand Down Expand Up @@ -84,8 +84,8 @@ public VariableResolvingASTVisitor(GraphContext context)
private final List<String> wildcardImports = new ArrayList<>();

/**
* Indicates that we have already attempted to query the graph for this particular shortname. The shortname will
* exist here even if no results were found.
* Indicates that we have already attempted to query the graph for this particular shortname. The shortname will exist here even if no results
* were found.
*/
private final Set<String> classNameLookedUp = new HashSet<>();
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.jboss.windup.rules.apps.java.scan.ast;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jboss.forge.roaster.model.JavaType;
import org.jboss.forge.roaster.model.source.Import;
import org.jboss.forge.roaster.model.source.Importer;
import org.jboss.forge.roaster.spi.WildcardImportResolver;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.rules.apps.java.model.JavaClassModel;
import org.jboss.windup.rules.apps.java.service.JavaClassService;

/**
* Provides a wildcard resolver for imports that attempts to search the graph for related types
*
*/
public class WindupRoasterWildcardImportResolver implements WildcardImportResolver
{
private static ThreadLocal<GraphContext> graphContextTL = new ThreadLocal<>();

/**
* Contains a map of class short names (eg, MyClass) to qualified names (eg, com.example.MyClass)
*/
private final Map<String, String> classNameToFQCN = new HashMap<>();

/**
* Indicates that we have already attempted to query the graph for this particular shortname. The shortname will exist here even if no results
* were found.
*/
private final Set<String> classNameLookedUp = new HashSet<>();

@Override
public String resolve(JavaType<?> source, String type)
{
GraphContext graphContext = getGraphContext();
if (graphContext == null)
{
return type;
}

// If the type contains a "." assume that it is fully qualified.
// FIXME - This is a carryover from the original Windup code, and I don't think
// that this assumption is valid.
// Check if we have already looked this one up
if (classNameLookedUp.contains(type))
{
// if yes, then just use the looked up name from the map
String qualifiedName = classNameToFQCN.get(type);
if (qualifiedName != null)
{
return qualifiedName;
}
else
{
// otherwise, just return the provided name (unchanged)
return type;
}
}
else
{
// if this name has not been resolved before, go ahead and resolve it from the graph (if possible)
classNameLookedUp.add(type);

// search every wildcard import for this name
Importer<?> importer = (Importer<?>) source;
for (Import importDeclaration : importer.getImports())
{
if (importDeclaration.isWildcard())
{
String wildcardImport = importDeclaration.getQualifiedName();
String candidateQualifiedName = wildcardImport + "." + type;

JavaClassService javaClassService = new JavaClassService(graphContext);
Iterable<JavaClassModel> models = javaClassService.findAllByProperty(JavaClassModel.PROPERTY_QUALIFIED_NAME,
candidateQualifiedName);
if (models.iterator().hasNext())
{
// we found it... put it in the map and return the result
classNameToFQCN.put(type, candidateQualifiedName);
return candidateQualifiedName;
}
}
}
// nothing was found, so just return the original value
return type;
}
}

private GraphContext getGraphContext()
{
return graphContextTL.get();
}

public static void setGraphContext(GraphContext graphContext)
{
graphContextTL.set(graphContext);
}
}
Loading

0 comments on commit 7dea02f

Please sign in to comment.