1 17 18 package org.apache.geronimo.naming.reference; 19 20 import org.apache.geronimo.gbean.AbstractName; 21 import org.apache.geronimo.gbean.AbstractNameQuery; 22 import org.apache.geronimo.kernel.GBeanNotFoundException; 23 import org.apache.geronimo.kernel.Kernel; 24 import org.apache.geronimo.kernel.repository.Artifact; 25 26 import javax.naming.NameNotFoundException ; 27 28 31 public class ResourceReference extends ConfigurationAwareReference { 32 private final Class iface; 33 34 40 public ResourceReference(Artifact configId, AbstractNameQuery abstractNameQuery, Class iface) { 41 super(configId, abstractNameQuery); 42 this.iface = iface; 43 } 44 45 public String getClassName() { 46 return iface.getName(); 47 } 48 49 public Object getContent() throws NameNotFoundException { 50 Kernel kernel = getKernel(); 51 52 AbstractName target; 53 try { 54 target = resolveTargetName(); 55 } catch (GBeanNotFoundException e) { 56 throw (NameNotFoundException ) new NameNotFoundException ("Could not resolve name query: " + abstractNameQueries).initCause(e); 57 } 58 59 Object proxy; 60 try { 61 proxy = kernel.invoke(target, "$getResource"); 62 } catch (Exception e) { 63 throw (IllegalStateException ) new IllegalStateException ("Could not get proxy").initCause(e); 64 } 65 if (proxy == null) { 66 throw new IllegalStateException ("Proxy not returned. Target " + target + " not started"); 67 } 68 if (!iface.isAssignableFrom(proxy.getClass())) { 69 Class proxyClass = proxy.getClass(); 70 Class [] interfaces = proxyClass.getInterfaces(); 71 StringBuffer message = new StringBuffer (); 72 boolean namesMatch = false; 73 for (int i = 0; i < interfaces.length; i++) { 74 Class anInterface = interfaces[i]; 75 if (iface.getName().equals(anInterface.getName())) { 76 namesMatch = true; 77 message.append("Proxy implements correct interface: ").append(iface.getName()).append(", but classloaders differ\n"); 78 message.append("lookup interface classloader: ").append(iface.getClassLoader().toString()).append("\n"); 79 message.append("target interface classloader: ").append(anInterface.getClassLoader().toString()).append("\n"); 80 message.append("target proxy classloader: ").append(proxy.getClass().getClassLoader()); 81 break; 82 } 83 } 84 if (!namesMatch) { 85 message.append("Proxy does not implement an interface named: ").append(iface.getName()); 86 } 87 throw new ClassCastException (message.toString()); 88 } 89 return proxy; 90 91 } 92 } 93 | Popular Tags |