Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-10681] Added support for OSGi modules when mapping warnings
to modules. If a module is an OSGi bundle then the content of pom or
build.xml will not be used.
  • Loading branch information
uhafner committed Aug 25, 2011
1 parent 5c6d44c commit 9a77f97
Showing 1 changed file with 67 additions and 12 deletions.
79 changes: 67 additions & 12 deletions src/main/java/hudson/plugins/analysis/util/ModuleDetector.java
Expand Up @@ -10,8 +10,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.apache.commons.digester.Digester;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.xml.sax.SAXException;

Expand All @@ -20,6 +23,7 @@
* file or the ANT build.xml file.
*
* @author Ulli Hafner
* @author Christoph Läubrich (support for OSGi-Bundles)
*/
public class ModuleDetector {
private static final String BACK_SLASH = "\\";
Expand All @@ -28,7 +32,11 @@ public class ModuleDetector {

static final String MAVEN_POM = "pom.xml";
static final String ANT_PROJECT = "build.xml";
private static final String PATTERN = ALL_DIRECTORIES + MAVEN_POM + ", " + ALL_DIRECTORIES + ANT_PROJECT;
static final String OSGI_BUNDLE = "META-INF/MANIFEST.MF";

private static final String PATTERN = ALL_DIRECTORIES + MAVEN_POM
+ ", " + ALL_DIRECTORIES + ANT_PROJECT
+ ", " + ALL_DIRECTORIES + OSGI_BUNDLE;

/** The factory to create input streams with. */
private FileInputStreamFactory factory = new DefaultFileInputStreamFactory();
Expand Down Expand Up @@ -85,10 +93,15 @@ private Map<String, String> createFilesToModuleMapping(final File workspace) {
if (fileName.endsWith(MAVEN_POM)) {
addMapping(mapping, fileName, MAVEN_POM, parsePom(fileName));
}
else {
else if (fileName.endsWith(ANT_PROJECT)) {
addMapping(mapping, fileName, ANT_PROJECT, parseBuildXml(fileName));
}
}
for (String fileName : projects) {
if (fileName.endsWith(OSGI_BUNDLE)) {
addMapping(mapping, fileName, OSGI_BUNDLE, parseManifest(fileName));
}
}

return mapping;
}
Expand Down Expand Up @@ -143,14 +156,15 @@ private String[] find(final File path) {
/**
* Returns the project name stored in the build.xml.
*
* @param path
* root folder
* @param buildXml
* Ant build.xml file name
* @return the project name or an empty string if the name could not be
* resolved
*/
private String parseBuildXml(final String path) {
private String parseBuildXml(final String buildXml) {
InputStream file = null;
try {
InputStream pom = factory.create(path);
file = factory.create(buildXml);
Digester digester = new Digester();
digester.setValidating(false);
digester.setClassLoader(ModuleDetector.class.getClassLoader());
Expand All @@ -160,7 +174,7 @@ private String parseBuildXml(final String path) {
digester.addCallMethod(xPath, "append", 1);
digester.addCallParam(xPath, 0, "name");

StringBuffer result = (StringBuffer)digester.parse(pom);
StringBuffer result = (StringBuffer)digester.parse(file);
return result.toString();
}
catch (IOException exception) {
Expand All @@ -169,28 +183,32 @@ private String parseBuildXml(final String path) {
catch (SAXException exception) {
// ignore
}
finally {
IOUtils.closeQuietly(file);
}
return StringUtils.EMPTY;
}

/**
* Returns the project name stored in the POM.
*
* @param fileName
* Maven module root folder
* @param pom
* Maven POM file name
* @return the project name or an empty string if the name could not be
* resolved
*/
private String parsePom(final String fileName) {
private String parsePom(final String pom) {
InputStream file = null;
try {
InputStream pom = factory.create(fileName);
file = factory.create(pom);
Digester digester = new Digester();
digester.setValidating(false);
digester.setClassLoader(ModuleDetector.class.getClassLoader());

digester.push(new StringBuffer());
digester.addCallMethod("project/name", "append", 0);

StringBuffer result = (StringBuffer)digester.parse(pom);
StringBuffer result = (StringBuffer)digester.parse(file);
return result.toString();
}
catch (IOException exception) {
Expand All @@ -199,6 +217,43 @@ private String parsePom(final String fileName) {
catch (SAXException exception) {
// ignore
}
finally {
IOUtils.closeQuietly(file);
}
return StringUtils.EMPTY;
}

/**
* Scans a Manifest file for OSGi Bundle Information
*
* @param manifestFile
* file name of MANIFEST.MF
* @return the project name or an empty string if the name could not be
* resolved
*/
private String parseManifest(final String manifestFile) {
InputStream file = null;
try {
file = factory.create(manifestFile);
Manifest manifest = new Manifest(file);
Attributes attributes = manifest.getMainAttributes();
String symbolicName = attributes.getValue("Bundle-SymbolicName");
if (StringUtils.isNotBlank(symbolicName)) {
String vendor = attributes.getValue("Bundle-Vendor");
if (StringUtils.isNotBlank(vendor)) {
return symbolicName + " (" + vendor + ")";
}
else {
return symbolicName;
}
}
}
catch (IOException exception) {
// ignore
}
finally {
IOUtils.closeQuietly(file);
}
return StringUtils.EMPTY;
}

Expand Down

0 comments on commit 9a77f97

Please sign in to comment.