Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WINDUP-766: Scan JSP's for potential migration issues
- Loading branch information
Showing
18 changed files
with
759 additions
and
94 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
...a-ee/addon/src/main/java/org/jboss/windup/rules/apps/javaee/model/JspSourceFileModel.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,19 @@ | ||
package org.jboss.windup.rules.apps.javaee.model; | ||
|
||
import org.jboss.windup.rules.apps.java.model.AbstractJavaSourceModel; | ||
|
||
import com.tinkerpop.frames.modules.typedgraph.TypeValue; | ||
|
||
/** | ||
* Represents a JSP file on disk. | ||
* | ||
* @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a> | ||
*/ | ||
@TypeValue(JspSourceFileModel.TYPE) | ||
public interface JspSourceFileModel extends AbstractJavaSourceModel | ||
{ | ||
String UNPARSEABLE_JSP_CLASSIFICATION = "Unparseable JSP File"; | ||
String UNPARSEABLE_JSP_DESCRIPTION = "This JSP file could not be parsed"; | ||
|
||
String TYPE = "JspSourceFile"; | ||
} |
174 changes: 174 additions & 0 deletions
174
...on/src/main/java/org/jboss/windup/rules/apps/javaee/rules/AnalyzeJSPFileRuleProvider.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,174 @@ | ||
package org.jboss.windup.rules.apps.javaee.rules; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jboss.windup.ast.java.data.ClassReference; | ||
import org.jboss.windup.ast.java.data.ResolutionStatus; | ||
import org.jboss.windup.ast.java.data.TypeReferenceLocation; | ||
import org.jboss.windup.config.AbstractRuleProvider; | ||
import org.jboss.windup.config.GraphRewrite; | ||
import org.jboss.windup.config.metadata.MetadataBuilder; | ||
import org.jboss.windup.config.operation.iteration.AbstractIterationOperation; | ||
import org.jboss.windup.config.phase.InitialAnalysisPhase; | ||
import org.jboss.windup.config.query.Query; | ||
import org.jboss.windup.graph.GraphContext; | ||
import org.jboss.windup.reporting.service.ClassificationService; | ||
import org.jboss.windup.rules.apps.java.model.JavaClassModel; | ||
import org.jboss.windup.rules.apps.java.scan.ast.JavaTypeReferenceModel; | ||
import org.jboss.windup.rules.apps.java.scan.ast.TypeInterestFactory; | ||
import org.jboss.windup.rules.apps.java.service.JavaClassService; | ||
import org.jboss.windup.rules.apps.java.service.TypeReferenceService; | ||
import org.jboss.windup.rules.apps.javaee.model.JspSourceFileModel; | ||
import org.ocpsoft.rewrite.config.Configuration; | ||
import org.ocpsoft.rewrite.config.ConfigurationBuilder; | ||
import org.ocpsoft.rewrite.context.EvaluationContext; | ||
|
||
/** | ||
* Extracts type references from JSP files. | ||
* | ||
* @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a> | ||
*/ | ||
public class AnalyzeJSPFileRuleProvider extends AbstractRuleProvider | ||
{ | ||
public AnalyzeJSPFileRuleProvider() | ||
{ | ||
super(MetadataBuilder.forProvider(AnalyzeJSPFileRuleProvider.class) | ||
.setPhase(InitialAnalysisPhase.class) | ||
.setHaltOnException(true)); | ||
} | ||
|
||
// @formatter:off | ||
@Override | ||
public Configuration getConfiguration(GraphContext context) | ||
{ | ||
return ConfigurationBuilder.begin() | ||
.addRule() | ||
.when(Query.fromType(JspSourceFileModel.class)) | ||
.perform(new ParseSourceOperation()); | ||
} | ||
// @formatter:on | ||
|
||
private class ParseSourceOperation extends AbstractIterationOperation<JspSourceFileModel> | ||
{ | ||
@Override | ||
public void perform(GraphRewrite event, EvaluationContext context, JspSourceFileModel sourceFile) | ||
{ | ||
|
||
TypeReferenceService typeReferenceService = new TypeReferenceService(event.getGraphContext()); | ||
|
||
try | ||
{ | ||
// Setup some basic details about the "Java Class" | ||
// source root, is decompiled, javaclass. package name | ||
sourceFile.setPackageName(""); | ||
sourceFile.setDecompiled(false); | ||
JavaClassService javaClassService = new JavaClassService(event.getGraphContext()); | ||
JavaClassModel classModel = javaClassService.create(); | ||
classModel.setPackageName(""); | ||
classModel.setSimpleName(sourceFile.getFileName()); | ||
classModel.setQualifiedName(sourceFile.getFileName()); | ||
classModel.setExtends(javaClassService.getOrCreatePhantom("javax.servlet.http.HttpServlet")); | ||
sourceFile.addJavaClass(classModel); | ||
|
||
Iterable<ClassReference> references = getClassReferences(typeReferenceService, sourceFile); | ||
for (ClassReference reference : references) | ||
{ | ||
JavaTypeReferenceModel typeReference = typeReferenceService.createTypeReference(sourceFile, | ||
reference.getLocation(), | ||
reference.getResolutionStatus(), | ||
reference.getLineNumber(), reference.getColumn(), reference.getLength(), | ||
reference.getQualifiedName(), | ||
reference.getLine()); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
ClassificationService classificationService = new ClassificationService(event.getGraphContext()); | ||
classificationService.attachClassification(context, sourceFile, JspSourceFileModel.UNPARSEABLE_JSP_CLASSIFICATION, | ||
JspSourceFileModel.UNPARSEABLE_JSP_DESCRIPTION); | ||
} | ||
} | ||
} | ||
|
||
private Iterable<ClassReference> getClassReferences(TypeReferenceService service, JspSourceFileModel sourceFile) throws IOException | ||
{ | ||
String source = FileUtils.readFileToString(sourceFile.asFile()); | ||
|
||
List<ClassReference> results = new ArrayList<>(); | ||
results.addAll(findImports(source)); | ||
results.addAll(findTaglib(source)); | ||
return results; | ||
} | ||
|
||
private List<ClassReference> findImports(String source) | ||
{ | ||
List<ClassReference> results = new ArrayList<>(); | ||
|
||
Pattern jspImport = Pattern.compile("<%@\\s*page\\s+[^>]*\\s*import\\s*=\\s*['\"]([^'\"]+)['\"].*?%>", | ||
Pattern.DOTALL | Pattern.CASE_INSENSITIVE); | ||
Matcher matcher = jspImport.matcher(source); | ||
while (matcher.find()) | ||
{ | ||
String matched = matcher.group(1); | ||
if (StringUtils.isNotBlank(matched)) | ||
{ | ||
String[] imports = StringUtils.split(matched, ","); | ||
if (imports != null) | ||
{ | ||
for (String imported : imports) | ||
{ | ||
imported = StringUtils.trim(imported); | ||
if (TypeInterestFactory.matchesAny(imported, TypeReferenceLocation.IMPORT)) | ||
{ | ||
ClassReference reference = createClassReference(TypeReferenceLocation.IMPORT, source, imported, matcher.start()); | ||
results.add(reference); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
private List<ClassReference> findTaglib(String source) | ||
{ | ||
List<ClassReference> results = new ArrayList<>(); | ||
|
||
Pattern taglibPattern = Pattern.compile("<%@\\s*taglib\\s+[^>]*\\s*uri\\s*=\\s*['\"]([^'\"]+)['\"].*?%>", | ||
Pattern.DOTALL | Pattern.CASE_INSENSITIVE); | ||
Matcher matcher = taglibPattern.matcher(source); | ||
while (matcher.find()) | ||
{ | ||
String matched = matcher.group(1); | ||
if (StringUtils.isNotBlank(matched)) | ||
{ | ||
if (TypeInterestFactory.matchesAny(matched, TypeReferenceLocation.TAGLIB_IMPORT)) | ||
{ | ||
ClassReference reference = createClassReference(TypeReferenceLocation.TAGLIB_IMPORT, source, matched, matcher.start()); | ||
results.add(reference); | ||
} | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
private ClassReference createClassReference(TypeReferenceLocation location, String source, String reference, int startPosition) | ||
{ | ||
String subString = StringUtils.substring(source, 0, startPosition + 1); | ||
String[] lines = subString.split("\r\n|\r|\n"); | ||
|
||
int lineNumber = lines.length; | ||
int column = lines[lines.length - 1].indexOf(source.substring(startPosition)); | ||
int length = reference.length(); | ||
|
||
return new ClassReference(reference, ResolutionStatus.UNKNOWN, location, lineNumber, column, length, | ||
reference); | ||
} | ||
|
||
} |
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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
...on/src/main/java/org/jboss/windup/rules/apps/javaee/rules/JspFileMappingRuleProvider.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,35 @@ | ||
package org.jboss.windup.rules.apps.javaee.rules; | ||
|
||
import org.jboss.windup.config.AbstractRuleProvider; | ||
import org.jboss.windup.config.metadata.MetadataBuilder; | ||
import org.jboss.windup.config.phase.ClassifyFileTypesPhase; | ||
import org.jboss.windup.graph.GraphContext; | ||
import org.jboss.windup.rules.apps.javaee.model.JspSourceFileModel; | ||
import org.jboss.windup.rules.files.FileMapping; | ||
import org.ocpsoft.rewrite.config.Configuration; | ||
import org.ocpsoft.rewrite.config.ConfigurationBuilder; | ||
|
||
/** | ||
* Insures that basic JSP file extensions are mapped to {@link JspSourceFileModel}. | ||
* | ||
* @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a> | ||
*/ | ||
public class JspFileMappingRuleProvider extends AbstractRuleProvider | ||
{ | ||
|
||
public JspFileMappingRuleProvider() | ||
{ | ||
super(MetadataBuilder.forProvider(JspFileMappingRuleProvider.class) | ||
.setPhase(ClassifyFileTypesPhase.class)); | ||
} | ||
|
||
@Override | ||
public Configuration getConfiguration(GraphContext context) | ||
{ | ||
return ConfigurationBuilder.begin() | ||
.addRule(FileMapping.from(".*\\.jsp$").to(JspSourceFileModel.class)) | ||
.addRule(FileMapping.from(".*\\.jspx$").to(JspSourceFileModel.class)) | ||
.addRule(FileMapping.from(".*\\.jsf$").to(JspSourceFileModel.class)) | ||
.addRule(FileMapping.from(".*\\.xhtml$").to(JspSourceFileModel.class)); | ||
} | ||
} |
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
Oops, something went wrong.