blob: 7796bbf091a002c33f6b13487f64db360f69180f [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2005 BEA Systems, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* mkaufman@bea.com - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.apt.core.internal;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarFactoryContainer extends FactoryContainer
{
private File _jarFile;
public JarFactoryContainer( File jarFile )
{
_jarFile = jarFile.getAbsoluteFile();
}
protected List<String> loadFactoryNames() {
return getServiceClassnamesFromJar( _jarFile );
}
public String getId() {
return _jarFile.getPath();
}
/**
* Given a jar file, get the names of any AnnotationProcessorFactory
* implementations it offers. The information is based on the Sun
* <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">
* Jar Service Provider spec</a>: the jar file contains a META-INF/services
* directory; that directory contains text files named according to the desired
* interfaces; and each file contains the names of the classes implementing
* the specified service. The files may also contain whitespace (which is to
* be ignored). The '#' character indicates the beginning of a line comment,
* also to be ignored. Implied but not stated in the spec is that this routine
* also ignores anything after the first nonwhitespace token on a line.
* @param jar the jar file.
* @return a list, possibly empty, of fully qualified classnames to be instantiated.
*/
private List<String> getServiceClassnamesFromJar(File jar)
{
List<String> classNames = new ArrayList<String>();
JarFile jarFile;
try {
jarFile = new JarFile(jar);
for (String providerName : AUTOLOAD_SERVICES) {
JarEntry provider = jarFile.getJarEntry(providerName);
if (provider == null) {
continue;
}
// Extract classnames from this text file.
InputStream is = jarFile.getInputStream(provider);
BufferedReader rd;
rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
for (String line = rd.readLine(); line != null; line = rd.readLine()) {
// hack off any comments
int iComment = line.indexOf('#');
if (iComment >= 0) {
line = line.substring(0, iComment);
}
// add the first non-whitespace token to the list
final String[] tokens = line.split("\\s", 2);
if (tokens[0].length() > 0) {
classNames.add(tokens[0]);
}
}
rd.close();
}
jarFile.close();
}
catch (IOException e) {
// TODO: log this exception
e.printStackTrace();
return classNames;
}
return classNames;
}
public URL getJarFileURL() throws MalformedURLException {
return _jarFile.toURL();
}
/** List of jar file entries that specify autoloadable service providers */
private static final String[] AUTOLOAD_SERVICES = {
"META-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory"
};
@Override
public FactoryType getType() {
return FactoryType.JAR;
}
}