Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 89   Methods: 4
NCLOC: 55   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DirectoryClassLoader.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: DirectoryClassLoader.java 541508 2007-05-25 01:54:12Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.util;
 21   
 22    import java.io.File;
 23    import java.io.FileInputStream;
 24    import java.util.Collections;
 25    import java.util.HashMap;
 26    import java.util.Map;
 27   
 28    /**
 29    * DirectoryClassLoader reads classes relative to the
 30    * constructor-provided base directory. This class is typically used for loading
 31    * classes from a directory that isn't on the CLASSPATH.
 32    *
 33    * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $
 34    */
 35    public final class DirectoryClassLoader extends ClassLoader {
 36   
 37    private File directory = null;
 38    private Map cache = Collections.synchronizedMap(new HashMap());
 39    private Map dates = Collections.synchronizedMap(new HashMap());
 40   
 41   
 42  0 public DirectoryClassLoader(String directory) {
 43  0 this.directory = new File(directory);
 44    }
 45   
 46  0 private File getClassFile(String classname) {
 47  0 String filename = classname.replace('.', File.separatorChar) + ".class";
 48  0 return new File(directory, filename);
 49    }
 50   
 51  0 public boolean isModified(String classname) {
 52  0 Long value = (Long) dates.get(classname);
 53  0 if (value != null) {
 54  0 try {
 55  0 File file = getClassFile(classname);
 56  0 return file.lastModified() != value.longValue();
 57    } catch (Exception e) {
 58  0 return true;
 59    }
 60    }
 61  0 return false;
 62    }
 63   
 64  0 public Class loadClass(String classname, boolean resolve) throws ClassNotFoundException {
 65  0 try {
 66  0 Class c = (Class) cache.get(classname);
 67  0 if (c == null) {
 68  0 try {
 69  0 File file = getClassFile(classname);
 70  0 FileInputStream fis = new FileInputStream(file);
 71  0 byte[] data = new byte[fis.available()];
 72  0 fis.read(data);
 73  0 fis.close();
 74  0 c = defineClass(classname, data, 0, data.length);
 75  0 dates.put(classname, new Long(file.lastModified()));
 76    } catch (Throwable t) {
 77  0 c = findSystemClass(classname);
 78    }
 79  0 cache.put(classname, c);
 80    }
 81  0 if (resolve) {
 82  0 resolveClass(c);
 83    }
 84  0 return c;
 85    } catch (Exception e) {
 86  0 throw new ClassNotFoundException(e.toString());
 87    }
 88    }
 89    }