Clover coverage report -
Coverage timestamp: Sat Jul 7 2007 16:41:13 CEST
file stats: LOC: 153   Methods: 5
NCLOC: 102   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JtrRmiClassLoader.java 87.5% 80.4% 100% 83.1%
coverage coverage
 1    /*
 2    * JTRunner is free software; you can redistribute it and/or modify it under the
 3    * terms of the GNU General Public License as published by the Free Software
 4    * Foundation; either version 2, or (at your option) any later version.
 5    */
 6   
 7    package jtr.remote.cl;
 8   
 9    import java.io.File;
 10    import java.io.FileNotFoundException;
 11    import java.io.FileOutputStream;
 12    import java.io.IOException;
 13    import java.net.MalformedURLException;
 14    import java.net.URL;
 15    import java.rmi.RemoteException;
 16    import java.security.SecureClassLoader;
 17    import jtr.enterprise.LocatorException;
 18    import jtr.remote.test.NodeInfo;
 19    import jtr.remote.utils.RmiUtil;
 20    import org.apache.log4j.Logger;
 21   
 22    /**
 23    * This is the JTR class-loader implementation, for providing a zero-configuration
 24    * class loading mechanism in the JTR runtime.
 25    *
 26    * @author frusso
 27    * @version 4.0
 28    * @since 4.0
 29    */
 30    public class JtrRmiClassLoader extends SecureClassLoader {
 31   
 32    /**
 33    * Creates a new instance of a JTR class loader.
 34    *
 35    * @param serverCL
 36    */
 37  6 public JtrRmiClassLoader(NodeInfo serverCL) {
 38  6 this.serverCL = serverCL;
 39  6 try {
 40  6 rcl = (JtrRmiClassLoaderServer) RmiUtil.lookupServer(serverCL);
 41    } catch(LocatorException e) {
 42  0 String msg = "Unable to connect to "+serverCL;
 43  0 logger.fatal(msg,e);
 44  0 System.err.println(msg+"\n"+e);
 45  0 System.out.println(msg+"\n"+e);
 46    }
 47    }
 48   
 49  1717 @Override
 50    public Class<?> findClass(String name) throws ClassNotFoundException {
 51  1717 byte[] bytecode = getByteCode(name);
 52  1717 if(bytecode==null)
 53  92 throw new ClassNotFoundException("Unable to find class "+name);
 54  1625 return defineClass(name,bytecode,0,bytecode.length);
 55    }
 56   
 57    /**
 58    * This method asks the server class loader for a specific Java class' bytecode.
 59    * Should something go wrong, <code>null</code> is returned, causing the
 60    * <code>findClass</code> method to throw a <code>ClassNotFoundException</code>.
 61    */
 62  1717 private byte[] getByteCode(String name) {
 63  1717 Byte[] bytecode = null;
 64  1717 String url = serverCL.toURL();
 65  1717 try {
 66  1717 logger.debug("Downloading class "+name+"...");
 67  1717 bytecode = rcl.getBytecode(name);
 68  1716 logger.debug("Bytecode received for class "+name);
 69    } catch (RemoteException ex) {
 70    //TODO should the RemoteException trigger an automatic retry for a configurable
 71    //number of times?
 72  0 logger.error("Unable to contact server class loader at "+url,ex);
 73    }
 74   
 75  1717 if(bytecode!=null) {
 76  1624 byte[] res = new byte[bytecode.length];
 77  1625 for (int i = 0; i<bytecode.length; i++) {
 78  6267788 res[i] = bytecode[i];
 79    }
 80  1625 return res;
 81    }
 82  92 return null;
 83    }
 84   
 85  39 @Override
 86    protected URL findResource(String name) {
 87  39 URL res = null;
 88  39 File file = null;
 89  39 try {
 90  39 logger.debug("Downloading resource "+name);
 91  39 Byte[] resource = rcl.getResource(name);
 92  39 logger.debug("Got resource "+name);
 93   
 94  39 if(resource!=null) {
 95    // create a new local file for hosting the resource
 96  9 file = prepareFile(name);
 97  9 FileOutputStream fos = new FileOutputStream(file);
 98   
 99    // just silly code
 100  9 byte[] tmp = new byte[resource.length];
 101  9 for (int i = 0; i<resource.length; i++) {
 102  171138 tmp[i] = resource[i];
 103    }
 104   
 105    // write the resource locally
 106  9 fos.write(tmp);
 107    // obtain the URL that must be returned
 108  9 res = file.toURL();
 109    // close the stream
 110  9 fos.close();
 111    }
 112    } catch (RemoteException ex) {
 113  0 logger.error("Unable to contact server class loader",ex);
 114    } catch (MalformedURLException ex) {
 115  0 logger.error("The provided URL is not legal",ex);
 116    } catch (FileNotFoundException ex) {
 117  0 logger.fatal("Error preparing file "+file.getAbsolutePath());
 118    } catch (IOException ex) {
 119  0 logger.fatal("Error accessing file "+file.getAbsolutePath());
 120    }
 121   
 122  39 return res;
 123    }
 124   
 125    /**
 126    * This method locally prepares a new file for writing the remotely load
 127    * resource.
 128    */
 129  9 private File prepareFile(String name) {
 130  9 File res = null;
 131  9 String[] names = name.split("/");
 132  9 if(names.length==1) {
 133    // there are no dirs
 134  0 res = new File(names[0]);
 135    } else {
 136    // there are dirs in the path
 137  9 int pathLength = names.length;
 138  9 File parent = null;
 139  9 for(int i=0;i<pathLength-1;i++) {
 140  59 parent = new File(parent,names[i]);
 141  59 if(!parent.exists()) {
 142  0 parent.mkdir();
 143    }
 144    }
 145  9 res = new File(name);
 146    }
 147  9 return res;
 148    }
 149   
 150    private static Logger logger = Logger.getLogger(JtrRmiClassLoader.class);
 151    private NodeInfo serverCL;
 152    private static JtrRmiClassLoaderServer rcl;
 153    }