jvanzyl 2002/06/05 12:55:38
Added: src/java/org/apache/maven/dependency
BytecodeProjectResolver.java
Log:
Adding back in the bytecode project resolver as someone might like
to play with it (steve :-)). I use the source resolver now but the
bytecode analysis will probably come in handy for something.
Revision Changes Path
1.1
jakarta-turbine-maven/src/java/org/apache/maven/dependency/BytecodeProjectResolver.java
Index: BytecodeProjectResolver.java
===================================================================
package org.apache.maven.dependency;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.StringTokenizer;
import java.util.Properties;
import java.util.HashMap;
import org.apache.maven.MavenUtils;
import org.apache.maven.util.JarUtil;
import org.apache.maven.importscrubber.ClassParserWrapper;
import org.apache.maven.importscrubber.TreeMapListener;
/**
* <code>BytecodeProjectResolver</code> is an executor that will process
* a directory looking for JAR and class files and turn the set
* class references it finds into a set of project references
* which can be used to create an accurate set of dependencies
* in a project descriptor.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @version $Id: BytecodeProjectResolver.java,v 1.1 2002/06/05 19:55:38 jvanzyl Exp $
*/
public class BytecodeProjectResolver
{
/**
* Base directory in which JAR files and class files will
* be searched for or just a JAR file.
*/
private File base;
/**
* Listener that registers class references as they are found by the
* {@link ClassParserWrapper}
*/
private TreeMapListener treeMapListener;
/**
* Package references that are collected by mapping the class references
* to package references.
*/
private List packageReferences;
/**
* Packages to exclude from the final list of packages.
*/
private List packageExcludes;
/**
* Package -> Project map.
*/
private File mapFile;
/**
* Project references.
*/
private List projectReferences;
/**
* Default constructor.
*/
public BytecodeProjectResolver()
{
packageReferences = new LinkedList();
packageExcludes = new LinkedList();
treeMapListener = new TreeMapListener();
projectReferences = new LinkedList();
}
/**
* Set the packages to exclude from the final list found
* by the resolution process.
*
* @param packageExcludesList comma separate list of the packages to exclude
* from the final list of packages.
*/
public void setPackageExcludes(String packageExcludesList)
{
StringTokenizer st = new StringTokenizer(packageExcludesList, ",");
while (st.hasMoreTokens())
{
String packageExclude = st.nextToken();
packageExcludes.add(packageExclude);
}
}
/**
* Set the directory in which JARs and class files are
* searched for.
*
* @param base Directory in which JARs and class files are searched for.
*/
public void setBase(File base)
{
this.base = base;
}
/**
* Set the location of the package -> project map.
*
* @param mapFile Location of the package -> project map.
*/
public void setMapFile(File mapFile)
{
this.mapFile = mapFile;
}
/**
* Get the package referenced determined by the resolution
* process.
*
* @return List The list of packages references found.
*/
public List getPackageReferences()
{
return packageReferences;
}
/**
* Get the projects referenced determined by the resolution
* process.
*
* @return List The list of project references found.
*/
public List getProjectReferences()
{
return projectReferences;
}
/**
* Resolve the package dependencies from the set of JARs
* and classes used as the input.
*/
public void resolveDependencies()
{
// Collect all the JAR and class file that we can find
// in the base or just process the JAR.
if (base.isDirectory())
{
String[] files =
MavenUtils.getFiles(base.getAbsolutePath(), "*.jar,*.class");
for (int i = 0; i < files.length; i++)
{
String file = files[i];
if (file.endsWith("jar"))
{
processJAR(file);
}
else
{
processClassReference(file);
}
}
}
else
{
processJAR(base.getAbsolutePath());
}
Properties map = new Properties();
try
{
map.load(new FileInputStream(mapFile));
}
catch (Exception e)
{
e.printStackTrace();
}
HashMap projects = new HashMap();
List packageReferences = getPackageReferences();
for (Iterator i = packageReferences.iterator(); i.hasNext();)
{
String classReference = (String) i.next();
for (Iterator j = map.keySet().iterator(); j.hasNext();)
{
String packageKey = (String) j.next();
if (classReference.indexOf(packageKey) == 0)
{
Object o = map.get(packageKey);
projects.put(o, o);
}
}
}
projectReferences.addAll(projects.values());
}
/**
* Process an individual JAR file looking package
* dependencies.
* @param file the jar file to process
*/
public void processJAR(String file)
{
JarFile jarFile = null;
try
{
jarFile = new JarFile(new File(file));
List classEntries = JarUtil.getClassEntries(jarFile);
treeMapListener.getTreeMap().clear();
for (Iterator i = classEntries.iterator(); i.hasNext();)
{
JarEntry jarEntry = (JarEntry) i.next();
InputStream classInputStream = jarFile.getInputStream(jarEntry);
ClassParserWrapper.parse(classInputStream, treeMapListener);
}
jarFile.close();
for (Iterator i = treeMapListener.getTreeMap().keySet().iterator();
i.hasNext();)
{
String classReference = (String) i.next();
processClassReference(classReference);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Process an individual class file.
*
* @param classReference An individual class reference as a string.
*/
private void processClassReference(String classReference)
{
// Don't care about inner classes.
if (classReference.indexOf("$") < 0 &&
classReference.startsWith("[") == false)
{
boolean includePackage = true;
for (Iterator j = packageExcludes.iterator(); j.hasNext();)
{
String packageExclude = (String) j.next();
if (classReference.startsWith(packageExclude))
{
includePackage = false;
break;
}
}
if (includePackage)
{
packageReferences.add(
treeMapListener.getTreeMap().get(classReference));
}
}
}
/**
* Used for testing only.
* @param args command line parameters
*/
public static void main(String[] args)
throws Exception
{
ProjectResolver bdr = new ProjectResolver();
bdr.setBase(new File(args[0]));
bdr.setPackageExcludes(args[1]);
bdr.setMapFile(new File(args[2]));
bdr.resolveDependencies();
List projectReferences = bdr.getProjectReferences();
for (Iterator i = projectReferences.iterator(); i.hasNext();)
{
System.out.println(i.next());
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>