1 package org.apache.turbine.services.assemblerbroker.util.java; 2 3 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.commons.lang.StringUtils; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.apache.turbine.Turbine; 30 import org.apache.turbine.TurbineConstants; 31 import org.apache.turbine.modules.Assembler; 32 import org.apache.turbine.modules.GenericLoader; 33 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory; 34 import org.apache.turbine.util.ObjectUtils; 35 36 44 public abstract class JavaBaseFactory 45 implements AssemblerFactory 46 { 47 48 private static List packages = 49 Turbine.getConfiguration().getList(TurbineConstants.MODULE_PACKAGES); 50 51 52 protected Log log = LogFactory.getLog(this.getClass()); 53 54 58 private Map classCache = Collections.synchronizedMap(new HashMap ()); 59 60 static 61 { 62 ObjectUtils.addOnce(packages, GenericLoader.getBasePackage()); 63 } 64 65 72 public Assembler getAssembler(String packageName, String name) 73 { 74 Assembler assembler = null; 75 76 log.debug("Class Fragment is " + name); 77 78 if (StringUtils.isNotEmpty(name)) 79 { 80 for (Iterator it = packages.iterator(); it.hasNext();) 81 { 82 StringBuffer className = new StringBuffer (); 83 84 className.append(it.next()); 85 className.append('.'); 86 className.append(packageName); 87 className.append('.'); 88 className.append(name); 89 90 log.debug("Trying " + className); 91 92 try 93 { 94 Class servClass = (Class ) classCache.get(className); 95 if(servClass == null) 96 { 97 servClass = Class.forName(className.toString()); 98 classCache.put(className, servClass); 99 } 100 assembler = (Assembler) servClass.newInstance(); 101 break; } 103 catch (ClassNotFoundException cnfe) 104 { 105 log.debug(className + ": Not found"); 107 } 108 catch (NoClassDefFoundError ncdfe) 109 { 110 log.debug(className + ": No Class Definition found"); 112 } 113 catch (ClassCastException cce) 114 { 115 log.error("Could not load "+className, cce); 119 break; } 121 catch (InstantiationException ine) 122 { 123 log.error("Could not load "+className, ine); 127 break; } 129 catch (IllegalAccessException ilae) 130 { 131 log.error("Could not load "+className, ilae); 135 break; } 137 } 139 } 140 log.debug("Returning: " + assembler); 141 142 return assembler; 143 } 144 } 145 | Popular Tags |