1 23 24 package org.enhydra.xml.xmlc; 25 26 31 public class XMLCStdFactory implements XMLCFactory { 32 35 private ClassLoader fDefaultClassLoader; 36 37 40 private XMLCLogger fLogger; 41 42 48 public XMLCStdFactory(ClassLoader classLoader, 49 XMLCLogger logger) { 50 fDefaultClassLoader = classLoader; 51 if (logger != null) { 52 fLogger = logger; 53 } else { 54 fLogger = new StreamXMLCLogger(); } 56 } 57 58 68 protected Error handleError(String className, 69 Throwable except) { 70 73 if (except instanceof Error ) { 75 return (Error )except; 76 } 77 78 return new XMLCError("Couldn't load XMLC class", except); 80 } 81 82 85 protected XMLObject doCreate(Class xmlcBasedClass) 86 throws ClassNotFoundException , IllegalAccessException , InstantiationException { 87 if (xmlcBasedClass.isInterface()) { 88 return doCreate(xmlcBasedClass.getName() + "Impl", 90 fDefaultClassLoader); 91 } else { 92 if (!XMLObject.class.isAssignableFrom(xmlcBasedClass)) { 93 throw new XMLCError("class \"" + xmlcBasedClass.getName() 94 + "\" does not implement \"" 95 + XMLObject.class.getName() + "\""); 96 } 97 return (XMLObject)(xmlcBasedClass.newInstance()); 98 } 99 } 100 101 104 protected XMLObject doCreate(String xmlcClassName, 105 ClassLoader classLoader) 106 throws ClassNotFoundException , IllegalAccessException , InstantiationException { 107 if (classLoader != null) { 108 return doCreate(classLoader.loadClass(xmlcClassName)); 109 } else { 110 return doCreate(Class.forName(xmlcClassName)); 111 } 112 } 113 114 119 public XMLObject create(String xmlcClassName) { 120 try { 121 return doCreate(xmlcClassName, fDefaultClassLoader); 122 } catch (Throwable except) { 123 throw handleError(xmlcClassName, except); 124 } 125 } 126 127 132 public XMLObject create(Class xmlcBasedClass) { 133 try { 134 if (xmlcBasedClass.isInterface()) { 135 return doCreate(xmlcBasedClass.getName() + "Impl", 137 xmlcBasedClass.getClassLoader()); 138 } else { 139 return doCreate(xmlcBasedClass); 140 } 141 } catch (Throwable except) { 142 throw handleError(xmlcBasedClass.getName(), except); 143 } 144 } 145 146 147 protected ClassLoader getDefaultClassLoader() { 148 return fDefaultClassLoader; 149 } 150 153 public XMLCLogger getLogger() { 154 return fLogger; 155 } 156 157 } 158 | Popular Tags |