1 5 6 package org.joseki.server.module; 7 8 import org.apache.commons.logging.* ; 9 10 import org.joseki.vocabulary.JosekiModule ; 11 import org.joseki.util.PrintUtils ; 12 13 import com.hp.hpl.jena.rdf.model.* ; 14 import com.hp.hpl.jena.shared.* ; 15 16 23 24 public class Loader 25 { 26 private static Log logger = LogFactory.getLog(Loader.class.getName()); 27 28 protected static ClassLoader classLoader = chooseClassLoader() ; 29 30 public Loader() 31 { 32 } 33 34 public Loadable loadAndInstantiate(Resource bindingResource, Class expectedType) 35 { 36 logger.debug("Attempt to load: "+PrintUtils.fmt(bindingResource)) ; 37 38 Resource implementation = null ; 39 try 40 { 41 45 implementation = bindingResource 47 .getProperty(JosekiModule.implementation) 48 .getResource() ; 49 logger.trace("Implementation: "+PrintUtils.fmt(implementation)) ; 50 51 } catch (JenaException ex) 52 { 53 logger.warn("Binding/Implementation structure incorrect") ; 54 return null ; 55 } 56 catch (NullPointerException nullEx) 57 { 58 logger.warn("No definition for "+PrintUtils.fmt(bindingResource)) ; 59 return null ; 60 } 61 62 String className = "<<unset>>" ; 63 try { 64 className = implementation.getRequiredProperty(JosekiModule.className).getString(); 65 if ( className == null ) 66 { 67 logger.warn("Class name not found" ) ; 69 return null ; 70 } 71 logger.trace("Class name: "+className) ; 72 } catch (PropertyNotFoundException noPropEx) 73 { 74 logger.warn("No property 'className'") ; 75 return null ; 76 } 77 78 try { 79 logger.trace("Load module: " + className); 80 Class classObj = classLoader.loadClass(className); 81 if ( classObj == null ) 82 { 83 logger.warn("Null return from classloader"); 84 return null ; 85 } 86 logger.debug("Loaded: "+className) ; 87 88 if ( ! Loadable.class.isAssignableFrom(classObj) ) 89 { 90 logger.warn(className + " does not support interface Loadable" ) ; 91 return null; 92 } 93 94 Loadable module = (Loadable)classObj.newInstance(); 95 logger.trace("New Instance created") ; 96 97 Statement s = bindingResource.getProperty(JosekiModule.interface_) ; 98 if ( s == null || s.getResource() == null) 99 { 100 logger.warn("No 'joseki:interface' property or value not a resource for "+PrintUtils.fmt(bindingResource)) ; 101 return null ; 102 } 103 104 String uriInterface = s.getResource().getURI() ; 105 106 if ( uriInterface == null || ! module.getInterfaceURI().equals(uriInterface) ) 107 { 108 if ( uriInterface == null ) 109 { 110 logger.warn("No declared interface URI : expected "+module.getInterfaceURI()) ; 111 return null ; 112 } 113 logger.warn("Mismatch between expected and actual operation URIs: "+ 114 "Expected: "+uriInterface+" : Actual: "+module.getInterfaceURI() ) ; 115 return null ; 116 } 117 118 if ( expectedType != null && ! expectedType.isInstance(module) ) 119 { 120 logger.warn(" " + className + " is not of class "+expectedType.getName()) ; 121 return null; 122 } 123 124 module.init(bindingResource, implementation) ; 126 127 logger.debug("Module: " + uriInterface) ; 129 logger.debug(" Implementation: "+className); 130 return module; 131 } 132 catch (Exception ex) 133 { 134 logger.warn(" Problems loading class " + className + " : " + ex + ": " + ex.getMessage()); 135 ex.printStackTrace(System.out) ; 136 return null; 137 } 138 } 139 140 static private ClassLoader chooseClassLoader() 141 { 142 ClassLoader classLoader = null ; 143 144 if ( classLoader == null ) 145 { 146 classLoader = Thread.currentThread().getContextClassLoader(); 148 if ( classLoader != null ) 149 logger.trace("Using thread classloader") ; 150 } 151 152 159 if ( classLoader == null ) 160 { 161 classLoader = ClassLoader.getSystemClassLoader() ; 162 if ( classLoader != null ) 163 logger.trace("Using system classloader") ; 164 } 165 166 if ( classLoader == null ) 167 logger.warn("Failed to find a classloader") ; 168 return classLoader ; 169 } 170 171 172 } 173 174 175 201 | Popular Tags |