1 7 package org.ejtools.jndi.browser.model.ejb; 8 9 import java.awt.Component ; 10 import java.beans.BeanInfo ; 11 import java.beans.Customizer ; 12 import java.beans.Introspector ; 13 import java.beans.PropertyChangeListener ; 14 import java.beans.PropertyChangeSupport ; 15 import java.lang.reflect.InvocationHandler ; 16 import java.lang.reflect.Method ; 17 import java.lang.reflect.Proxy ; 18 19 import javax.ejb.EJBObject ; 20 21 import org.ejtools.adwt.GenericCustomizer; 22 import org.ejtools.jndi.browser.model.JNDIEntry; 23 24 35 public class EJBObjectProxy extends JNDIEntry implements InvocationHandler 36 { 37 38 protected transient Customizer c; 39 40 protected String desc; 41 42 protected BeanInfo info; 43 44 protected Object obj; 45 46 protected Object proxy; 47 48 protected PropertyChangeSupport ps; 49 50 protected Class remote; 51 52 53 60 public EJBObjectProxy(Object obj, ClassLoader cl) 61 throws Exception 62 { 63 this.obj = obj; 64 65 Class [] intf = obj.getClass().getInterfaces(); 66 for (int i = 0; i < intf.length; i++) 67 { 68 if (EJBObject .class.isAssignableFrom(intf[i])) 69 { 70 remote = intf[i]; 71 } 72 } 73 74 this.info = Introspector.getBeanInfo(this.remote); 75 this.proxy = Proxy.newProxyInstance(cl, new Class []{BeanInfo .class, remote, PropertyChanger.class}, this); 76 this.ps = new PropertyChangeSupport (proxy); 77 } 78 79 80 85 public Component getComponent() 86 { 87 if (c == null) 88 { 89 c = new GenericCustomizer(proxy); 90 } 91 return (Component ) c; 92 } 93 94 95 104 public Object invoke(Object proxy, Method method, Object [] args) 105 throws Throwable 106 { 107 if (method.getDeclaringClass().equals(BeanInfo .class)) 108 { 109 return method.invoke(info, args); 110 } 111 else if (method.getDeclaringClass().equals(Object .class)) 112 { 113 return method.invoke(this, args); 114 } 115 else if (method.getDeclaringClass().equals(PropertyChanger.class)) 116 { 117 if (method.getName().startsWith("add")) 118 { 119 ps.addPropertyChangeListener((PropertyChangeListener ) args[0]); 120 } 121 else 122 { 123 ps.removePropertyChangeListener((PropertyChangeListener ) args[0]); 124 } 125 return null; 126 } 127 else 128 { 129 try 130 { 131 return method.invoke(obj, args); 132 } 133 finally 134 { 135 if (!(method.getName().startsWith("get") || method.getName().startsWith("is"))) 136 { 137 ps.firePropertyChange(null, null, null); 138 } 139 } 140 } 141 } 142 143 144 149 public String toString() 150 { 151 if (desc == null) 152 { 153 try 154 { 155 Method m = EJBObject .class.getMethod("getPrimaryKey", new Class [0]); 156 desc = m.invoke(obj, new Object [0]).toString(); 157 } 158 catch (Exception e) 159 { 160 161 desc = remote.getName().substring(remote.getName().lastIndexOf(".") + 1); 162 164 } 165 166 } 167 return desc; 168 } 169 } 170 171 | Popular Tags |