}
These methods are called by delegating to the Java object instance class. Note that the
code to create the apiClassLoader custom class loader is provided later in this
section.
/* Java code */
public class api
{
/* Load classes from the z folder */
static ClassLoader customLoader = new apiClassLoader("C:\z");
static String API_IMPL = "apiImpl";
apiInterface cp = null;
public api()
{
cp = load();
}
public void m()
{
cp.m();
}
public void m2()
{
cp.m2();
}
private static apiInterface load()
{
try
{
Class aClass = customLoader.loadClass(API_IMPL);
return (apiInterface) aClass.newInstance();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
The following DATA step program calls these methods by delegating through the api
Java object instance class. The Java object instantiates the api class, which creates a
custom class loader to load classes from the z folder. The api class calls the custom
loader and returns an instance of the
apiImpl interface implementation class to the
Java object. When methods are called through the Java object, the api class delegates
them to the implementation class.
/* DATA step code */
data _null_;
dcl javaobj j('api');
j.callvoidmethod('m');
j.callvoidmethod('m2');
run;
Using the Java Object 549
The following lines are written to the SAS log:
method m is z folder
method m2 in z folder
In the previous Java code, you could also use .jar files to augment the classpath in the
ClassLoader constructor.
static ClassLoader customLoader = new apiClassLoader("C:\z;C:\tempsome.jar");
In this case, the Java code for the custom class loader is as follows. This code for this
class loader can be added to or modified as needed.
import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
public class apiClassLoader extends ClassLoader
{
//class repository where findClass performs its search
private List classRepository;
public apiClassLoader(String loadPath)
{
super(apiClassLoader.class.getClassLoader());
initLoader(loadPath);
}
public apiClassLoader(ClassLoader parent,String loadPath)
{
super(parent);
initLoader(loadPath);
}
/**
* This method will look for the class in the class repository. If
* the method cannot find the class, the method will delegate to its parent
* class loader.
*
* @param className A String specifying the class to be loaded
* @return A Class object loaded by the apiClassLoader
* @throws ClassNotFoundException if the method is unable to load the class
*/
public Class loadClass(String name) throws ClassNotFoundException
{
// Check if the class is already loaded
Class loadedClass = findLoadedClass(name);
// Search for class in local repository before delegating
if (loadedClass == null)
{
loadedClass = myFindClass(name);
}
// If class not found, delegate to parent
if (loadedClass == null)
{
550 Chapter 22 Using DATA Step Component Objects
loadedClass = this.getClass().getClassLoader().loadClass(name);
}
return loadedClass;
}
private Class myFindClass(String className) throws ClassNotFoundException
{
byte[] classBytes = loadFromCustomRepository(className);
if(classBytes != null)
{
return defineClass(className,classBytes,0,classBytes.length);
}
return null;
}
/**
* This method loads binary class file data from the classRepository.
*/
private byte[] loadFromCustomRepository(String classFileName)
throws ClassNotFoundException
{
Iterator dirs = classRepository.iterator();
byte[] classBytes = null;
while (dirs.hasNext())
{
String dir = (String) dirs.next();
if (dir.endsWith(".jar"))
{
// Look for class in jar
String jclassFileName = classFileName;
jclassFileName = jclassFileName.replace('.', '/');
jclassFileName += ".class";
try
{
JarFile j = new JarFile(dir);
for (Enumeration e = j.entries(); e.hasMoreElements() ;)
{
Object n = e.nextElement();
if (jclassFileName.equals(n.toString()))
{
ZipEntry zipEntry = j.getEntry(jclassFileName);
if (zipEntry == null)
{
return null;
}
else
{
// read file
InputStream is = j.getInputStream(zipEntry);
classBytes = new byte[is.available()];
is.read(classBytes);
Using the Java Object 551
break;
}
}
}
}
catch (Exception e)
{
System.out.println("jar file exception");
return null;
}
}
else
{
// Look for class in directory
String fclassFileName = classFileName;
fclassFileName = fclassFileName.replace('.', File.separatorChar);
fclassFileName += ".class";
try
{
File file = new File(dir,fclassFileName);
if(file.exists()) {
//read file
InputStream is = new FileInputStream(file);
classBytes = new byte[is.available()];
is.read(classBytes);
break;
}
}
catch(IOException ex)
{
System.out.println("IOException raised while reading class
file data");
ex.printStackTrace();
return null;
}
}
}
return classBytes;
}
private void initLoader(String loadPath)
{
/*
* loadPath is passed in as a string of directories/jar files
* separated by the File.pathSeparator
*/
classRepository = new ArrayList();
if((loadPath != null) && !(loadPath.equals("")))
{
StringTokenizer tokenizer =
new StringTokenizer(loadPath,File.pathSeparator);
while(tokenizer.hasMoreTokens())
{
classRepository.add(tokenizer.nextToken());
552 Chapter 22 Using DATA Step Component Objects
}
}
}
}
Using the Java Object 553
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.27.171