Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enforcer rule for valid Classpath Entries in EAR modules' manifest #5

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package org.apache.maven.plugins.enforcer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
* This rule verifies that the EJB's Classpath entry in the manifest refers to a libray in the shared library path.
*
* The rule assumes that all EJB are located at the root of the EAR and the lib folder contains all shared libraries.
*
* @author Martin Goldhahn <[email protected]>
* @author Stig Tore Johannesen <[email protected]>
*/
public class EarModuleClasspath implements EnforcerRule {

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {

try {
MavenProject project = (MavenProject) helper.evaluate("${project}");
String projectType = project.getArtifact().getType();
if (!"ear".equals(projectType)) {
throw new EnforcerRuleException("EarModuleClasspath rule can only be applied in EAR projects");
}
File outputDir = new File(project.getBuild().getDirectory());
String finalName = project.getBuild().getFinalName();
File earFile = new File(outputDir, finalName + '.' + projectType);

verifyManifests(earFile);
} catch (ExpressionEvaluationException e) {
throw new EnforcerRuleException("Unable to lookup expression " + e.getLocalizedMessage(), e);
} catch (IOException ex) {
throw new EnforcerRuleException(ex.getMessage(), ex);
}
}

public boolean isCacheable() {
return false;
}

public boolean isResultValid(EnforcerRule cachedRule) {
return false;
}

public String getCacheId() {
return "";
}

private void verifyManifests(File earFile) throws EnforcerRuleException, IOException {
Set<String> sharedLibs = new HashSet<String>();
Map<String, Set<String>> ejbClasspaths = new HashMap<String, Set<String>>();

ApplicationXmlHandler appXml = readApplicationXml(earFile);

extractSharedLibsAndEjbClasspath(earFile, appXml, sharedLibs, ejbClasspaths);

checkClassPaths(sharedLibs, ejbClasspaths, appXml);
}

private void extractSharedLibsAndEjbClasspath(File earFile, ApplicationXmlHandler appXml, Set<String> sharedLibs,
Map<String, Set<String>> ejbClasspaths) throws IOException {

String libFolderName = appXml.getLibDir() + '/';
ZipFile ear = null;
try {
ear = new ZipFile(earFile);
Enumeration<? extends ZipEntry> entries = ear.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
if (appXml.getEjbModules().contains(entryName)) {
Set<String> cl = extractClassPathFromManifest(ear, entry);
ejbClasspaths.put(entryName, cl);
} else if (appXml.getWebModules().contains(entryName)) {
// web modules are ignored
} else if (entryName.startsWith(libFolderName) && entryName.endsWith(".jar")) {
// this must be a shared library
sharedLibs.add(entryName);
}
}
} finally {
if (ear != null) {
ear.close();
}
}
}

private Set<String> extractClassPathFromManifest(ZipFile ear, ZipEntry entry) throws IOException {

ZipInputStream zipStream = null;
Set<String> classPath = new HashSet<String>();
try {
zipStream = new ZipInputStream(ear.getInputStream(entry));

ZipEntry moduleEntry;
while ((moduleEntry = zipStream.getNextEntry()) != null) {
if (moduleEntry.getName().equals(JarFile.MANIFEST_NAME)) {
ByteArrayOutputStream sb = new ByteArrayOutputStream();
byte[] buffer = new byte[0xFFFF];
int read;
while ((read = zipStream.read(buffer)) >= 0) {
sb.write(buffer, 0, read);
}
Manifest mf = new Manifest(new ByteArrayInputStream(sb.toByteArray()));
String cl = mf.getMainAttributes().getValue("Class-Path");
if (cl != null) {
for (String clEntry : cl.split(" ")) {
classPath.add(clEntry);
}
}
break;
}
}
return classPath;
} finally {
if (zipStream != null) {
zipStream.close();
}
}
}

/**
* Read the META-INF/application.xml from the EAR.
*/
private ApplicationXmlHandler readApplicationXml(File earFile) throws IOException {
ZipFile ear = null;
try {
ear = new ZipFile(earFile);
ApplicationXmlHandler appXml = new ApplicationXmlHandler();
ZipEntry applicationXmlEntry = ear.getEntry("META-INF/application.xml");
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(ear.getInputStream(applicationXmlEntry), appXml);
return appXml;
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} finally {
if (ear != null) {
ear.close();
}
}
}

/**
* The libraries in the classpath entry of the manifest can include the library path or not.
*/
private void checkClassPaths(Set<String> sharedLibs, Map<String, Set<String>> ejbClasspaths, ApplicationXmlHandler appXml) throws EnforcerRuleException {
for (Map.Entry<String, Set<String>> entry : ejbClasspaths.entrySet()) {
for (String clEntry : entry.getValue()) {
if (!sharedLibs.contains(clEntry) && !sharedLibs.contains(appXml.getLibDir() + '/' + clEntry)) {
throw new EnforcerRuleException("Did not find shared library " + clEntry + " in manifest of " + entry.getKey());
}
}
}
}

private static class ApplicationXmlHandler extends DefaultHandler {
private Set<String> ejbModules = new HashSet<String>();
private Set<String> webModules = new HashSet<String>();
private String libDir;

private Stack<String> elementStack = new Stack<String>();

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
elementStack.push(qName);
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
elementStack.pop();
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length).trim();

String currentElement = elementStack.peek();
if ("ejb".equals(currentElement)) {
ejbModules.add(value);
} else if ("web-uri".equals(currentElement)) {
webModules.add(value);
} else if ("library-directory".equals(currentElement)) {
libDir = value;
}
}

public Set<String> getEjbModules() {
return ejbModules;
}

public Set<String> getWebModules() {
return webModules;
}

public String getLibDir() {
return libDir;
}
}
}
126 changes: 126 additions & 0 deletions src/site/apt/earModuleClasspath.apt.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
~~ Licensed to the Apache Software Foundation (ASF) under one
~~ or more contributor license agreements. See the NOTICE file
~~ distributed with this work for additional information
~~ regarding copyright ownership. The ASF licenses this file
~~ to you under the Apache License, Version 2.0 (the
~~ "License"); you may not use this file except in compliance
~~ with the License. You may obtain a copy of the License at
~~
~~ http://www.apache.org/licenses/LICENSE-2.0
~~
~~ Unless required by applicable law or agreed to in writing,
~~ software distributed under the License is distributed on an
~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~~ KIND, either express or implied. See the License for the
~~ specific language governing permissions and limitations
~~ under the License.

------
EAR module classpath
------
Martin Goldhahn
------
April 2015
------

EAR module classpath

This rule checks that the classpath manifest entry for Enterprise Java Bean (EJB) in an Enterprise ARchive (EAR) project refers to
existing libraries in the EAR's shared lib folder.

The Manifest of an EJB might have a manifest entry:
<<<Class-Path: commons-beanutils-1.8.3.jar>>>

This rule verifies that the EAR actually contains a commons-beanutils-1.8.3.jar in the EAR's lib folder.

As the EAR's lib folder can be configured, its actual value is read from META-INF/application.xml.
This file is also read to get the list of EJBs in the EAR.

Sample configuration:

+---+
<project>
<groupId>com.company</groupId>
<artifactId>product</artifactId>
<version>1.0</version>
<url>http://company/wiki/product</url>
<packaging>ear</packaging>
[...]
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>ejb1</artifactId>
<version>1.0</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>webapp1</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${enforcerApiVersion}</version> <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
<executions>
<execution>
<id>validate-ear</id>
<goals>
<goal>enforce</goal>
</goals>
<phase>package</phase>
<configuration>
<rules>
<earModuleClasspath/>
</rules>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<version>7</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<applicationName>product-app</applicationName>
<archive>
<manifestEntries>
<Dependencies>com.oracle.jdbc7_g</Dependencies>
</manifestEntries>
</archive>

<modules>
<webModule>
<groupId>com.company</groupId>
<artifactId>webapp1</artifactId>
<contextRoot>/webapp</contextRoot>
</webModule>

<ejbModule>
<groupId>com.company</groupId>
<artifactId>ejb1</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
+---+

* Trademarks

Apache, Apache Maven, Maven and the Apache feather logo are trademarks of The Apache Software Foundation.
2 changes: 2 additions & 0 deletions src/site/apt/index.apt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Extra Enforcer Rules

* {{{./requireEncoding.html}requireEncoding}} - verifies that source files have a required encoding.

* {{{./earModuleClasspath.html}earModuleClasspath}} - verifies that Class-Path manifest entries in EJBs inside a EAR are valid.

[]

<<NOTE: When using the Apache Maven Enforcer Plugin version 1.3 or later, be sure to use at least extra-enforcer-rules 1.0-beta-1>>
Expand Down
1 change: 1 addition & 0 deletions src/site/site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ under the License.
<item name="Enforce Bytecode Version" href="enforceBytecodeVersion.html"/>
<item name="Ban Circular Dependencies" href="banCircularDependencies.html"/>
<item name="Require Encoding" href="requireEncoding.html"/>
<item name="Ear Module Classpath" href="earModuleClasspath.html"/>
</menu>
</body>
</project>
Loading