import java.util.regex.Pattern ;
import java.util.regex.Matcher ;
import java.util.Vector;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import java.util.Set ;
import java.util.Iterator ;
import java.io.Serializable;
/**
* Utilities to manage java packages and how they are "imported" to R
* databases. This is the back door of the javaImport
* system in the R side
*
* @author Romain Francois <francoisromain@free.fr>
*/
public class RJavaImport implements Serializable {
/**
* Debug flag. Prints some messages if it is set to TRUE
*/
public static boolean DEBUG = false ;
/**
* list of imported packages
*/
/* TODO: vector is not good enough, we need to control the order
in which the packages appear */
private Vector/**/ importedPackages ;
/**
* maps a simple name to a fully qualified name
*/
/* String -> java.lang.String */
/* should we cache the Class instead ? */
private Map/**/ cache ;
/**
* associated class loader
*/
public ClassLoader loader ;
/**
* Constructor. Initializes the imported package vector and the cache
*/
public RJavaImport( ClassLoader loader ){
this.loader = loader ;
importedPackages = new Vector/**/();
cache = new HashMap/**/() ;
}
/**
* Look for the class in the set of packages
*
* @param clazz the simple class name
*
* @return an instance of Class representing the actual class
*/
public Class lookup( String clazz){
Class res = lookup_(clazz) ;
if( DEBUG ) System.out.println( " [J] lookup( '" + clazz + "' ) = " + (res == null ? " " : ("'" + res.getName() + "'" ) ) ) ;
return res ;
}
private Class lookup_( String clazz ){
Class res = null ;
if( cache.containsKey( clazz ) ){
try{
String fullname = (String)cache.get( clazz ) ;
Class cl = Class.forName( fullname ) ;
return cl ;
} catch( Exception e ){
/* does not happen */
}
}
/* first try to see if the class does not exist verbatim */
try{
res = Class.forName( clazz ) ;
} catch( Exception e){}
if( res != null ) {
cache.put( clazz, clazz ) ;
return res;
}
int npacks = importedPackages.size() ;
if( DEBUG ) System.out.println( " [J] " + npacks + " packages" ) ;
if( npacks > 0 ){
for( int i=0; i*/ set = cache.keySet() ;
int size = set.size() ;
String[] res = new String[size];
set.toArray( res );
if( DEBUG ) System.out.println( " [J] getKnownClasses().length = " + res.length ) ;
return res ;
}
public static Class lookup( String clazz , Set importers ){
Class res ;
Iterator iterator = importers.iterator() ;
while( iterator.hasNext()){
RJavaImport importer = (RJavaImport)iterator.next() ;
res = importer.lookup( clazz ) ;
if( res != null ) return res ;
}
return null ;
}
}