1 16 package org.apache.commons.jelly.impl; 17 18 import org.apache.commons.discovery.ResourceClass; 19 import org.apache.commons.discovery.ResourceClassIterator; 20 import org.apache.commons.discovery.resource.ClassLoaders; 21 import org.apache.commons.discovery.resource.classes.DiscoverClasses; 22 import org.apache.commons.jelly.TagLibrary; 23 import org.apache.commons.jelly.util.ClassLoaderUtils; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 28 38 public class DefaultTagLibraryResolver implements TagLibraryResolver { 39 40 41 private static final Log log = LogFactory.getLog(DefaultTagLibraryResolver.class); 42 43 private DiscoverClasses discovery; 44 45 51 private ClassLoader classLoader; 52 53 57 private boolean useContextClassLoader = false; 58 59 60 public DefaultTagLibraryResolver() { 61 } 62 63 64 67 72 public TagLibrary resolveTagLibrary(String uri) { 73 DiscoverClasses discovery = getDiscoverClasses(); 74 String name = uri; 75 if ( uri.startsWith( "jelly:" ) ) { 76 name = "jelly." + uri.substring(6); 77 } 78 79 log.info( "Looking up service name: " + name ); 80 81 98 ResourceClassIterator iter = discovery.findResourceClasses(name); 99 while (iter.hasNext()) { 100 ResourceClass resource = iter.nextResourceClass(); 101 try { 102 Class typeClass = resource.loadClass(); 103 if ( typeClass != null ) { 104 return newInstance(uri, typeClass); 105 } 106 } 107 catch (Exception e) { 108 log.error( "Could not load service: " + resource ); 109 } 110 } 111 log.info( "Could not find any services for name: " + name ); 112 return null; 113 } 114 115 118 128 public ClassLoader getClassLoader() { 129 return ClassLoaderUtils.getClassLoader(classLoader, useContextClassLoader, getClass()); 130 } 131 132 139 public void setClassLoader(ClassLoader classLoader) { 140 this.classLoader = classLoader; 141 } 142 143 146 public boolean getUseContextClassLoader() { 147 return useContextClassLoader; 148 } 149 150 159 public void setUseContextClassLoader(boolean use) { 160 useContextClassLoader = use; 161 } 162 163 167 public DiscoverClasses getDiscoverClasses() { 168 if ( discovery == null ) { 169 ClassLoaders loaders = ClassLoaders.getAppLoaders(TagLibrary.class, getClass(), false); 170 discovery = new DiscoverClasses(loaders); 171 } 172 return discovery; 173 } 174 175 179 public void setDiscoverClasses(DiscoverClasses discovery) { 180 this.discovery = discovery; 181 } 182 183 186 190 protected TagLibrary loadClass(String uri, String className) { 191 try { 192 Class theClass = getClassLoader().loadClass(className); 193 if ( theClass != null ) { 194 return newInstance(uri, theClass); 195 } 196 } 197 catch (ClassNotFoundException e) { 198 log.error("Could not find the class: " + className + " when trying to resolve URI: " + uri, e); 199 } 200 return null; 201 } 202 203 204 208 protected TagLibrary newInstance(String uri, Class theClass) { 209 try { 210 Object object = theClass.newInstance(); 211 if (object instanceof TagLibrary) { 212 return (TagLibrary) object; 213 } 214 else { 215 log.error( 216 "The tag library object mapped to: " 217 + uri 218 + " is not a TagLibrary. Object = " 219 + object); 220 } 221 } 222 catch (Exception e) { 223 log.error( 224 "Could not instantiate instance of class: " + theClass.getName() + ". Reason: " + e, 225 e); 226 } 227 return null; 228 } 229 230 } | Popular Tags |