1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
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 <code>javaImport</code>
* 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/*<String>*/ importedPackages ;
/**
* maps a simple name to a fully qualified name
*/
/* String -> java.lang.String */
/* should we cache the Class instead ? */
private Map/*<String,String>*/ 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/*<String>*/();
cache = new HashMap/*<String,String>*/() ;
}
/**
* 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<npacks; i++){
try{
String p = (String)importedPackages.get(i);
String candidate = p + "." + clazz ;
if( DEBUG ) System.out.println( " [J] trying class : " + candidate ) ;
res = Class.forName( candidate ) ;
} catch( Exception e){
if( DEBUG ) System.out.println( " [JE] " + e.getMessage() );
}
if( res != null ){
cache.put( clazz, res.getName() ) ;
return res ;
}
}
}
return null ;
}
/**
* @return true if the class is known
*/
public boolean exists( String clazz){
boolean res = exists_(clazz) ;
if( DEBUG ) System.out.println( " [J] exists( '" + clazz + "' ) = " + res ) ;
return res ;
}
public boolean exists_( String clazz ){
if( cache.containsKey( clazz ) ) return true ;
return ( lookup_( clazz ) != null );
}
/**
* Adds a package to the list of "imported" packages
*
* @param packageName package path name
*/
public void importPackage( String packageName ){
importedPackages.add( packageName ) ;
}
/**
* Adds a set of packages
*
* @param packages package path names
*/
public void importPackage( String[] packages ){
for( int i=0; i<packages.length; i++){
importPackage( packages[i] ) ;
}
}
/**
* @return the simple names of the classes currently known
* by this importer
*/
public String[] getKnownClasses(){
Set/*<String>*/ 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 ;
}
}
|