1 package org.apache.ws.jaxme.js.pattern; 2 3 import java.net.URL ; 4 5 import org.apache.ws.jaxme.js.JavaSource; 6 import org.apache.ws.jaxme.js.JavaSourceFactory; 7 8 9 24 public class InterfaceDescription { 25 private boolean isMandatory = true; 26 private String interfaceName; 27 private String type; 28 private JavaSource javaSource; 29 30 private ClassLoader [] getClassLoaders() { 31 return new ClassLoader []{ 32 Thread.currentThread().getContextClassLoader(), 33 getClass().getClassLoader(), 34 ClassLoader.getSystemClassLoader() 35 }; 36 } 37 38 40 public void setInterface(String pName) { 41 interfaceName = pName; 42 } 43 44 46 public String getInterface() { 47 return interfaceName; 48 } 49 50 56 public void setType(String pType) { 57 if (pType == null 58 || "Reflection".equalsIgnoreCase(pType) 59 || "Source".equalsIgnoreCase(pType)) { 60 type = pType; 61 } else { 62 throw new IllegalArgumentException ("Invalid type: " + pType + 63 ", expected 'Reflection', 'Source', or null."); 64 } 65 } 66 67 73 public String getType() { 74 return type; 75 } 76 77 84 public void setMandatory(boolean pMandatory) { isMandatory = pMandatory; } 85 86 93 public boolean isMandatory() { return isMandatory; } 94 95 98 public JavaSource getJavaSource() throws Exception { 99 if (javaSource == null) { 100 javaSource = initJavaSource(); 101 } 102 return javaSource; 103 } 104 105 107 private JavaSource initJavaSource() throws Exception { 108 Exception ex = null; 109 String mode = getType(); 110 if (mode == null || "Reflection".equals(mode)) { 111 try { 112 ClassLoader [] cls = getClassLoaders(); 113 for (int i = 0; i < cls.length; i++) { 114 if (cls[i] == null) { 115 continue; 116 } 117 118 Class c = cls[i].loadClass(getInterface()); 119 if (c != null) { 120 return new CompiledClassReflector(c).getJavaSource(new JavaSourceFactory()); 121 } 122 } 123 } catch (Exception e) { 124 if (ex == null) { 125 ex = e; 126 } 127 } 128 } 129 if (mode == null || "Source".equals(mode)) { 130 ClassLoader [] cls = getClassLoaders(); 131 for (int i = 0; i < cls.length; i++) { 132 if (cls[i] == null) { 133 continue; 134 } 135 URL url = cls[i].getResource(getInterface().replace('.', '/') + ".java"); 136 if (url != null) { 137 SourceReflector reflector = new SourceReflector(url); 138 return reflector.getJavaSource(new JavaSourceFactory()); 139 } 140 } 141 } 142 if (ex == null) { 143 throw new IllegalStateException ("Failed to locate Java class " 144 + getInterface()); 145 } else { 146 throw ex; 147 } 148 } 149 } | Popular Tags |