1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright (c) Corporation for National Research Initiatives
package org.python.core;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.WeakHashMap;
import org.python.util.Generic;
public class PyBeanEventProperty extends PyObject {
private static Map<String, Class<?>> adapterClasses = Generic.map();
private static Map<Object, Map<String, WeakReference<Object>>> adapters =
new WeakHashMap<Object, Map<String, WeakReference<Object>>>();
public Method addMethod;
public String eventName;
public Class<?> eventClass;
public String __name__;
private Field adapterField;
private Class<?> adapterClass;
public PyBeanEventProperty(String eventName,
Class<?> eventClass,
Method addMethod,
Method eventMethod) {
__name__ = eventMethod.getName().intern();
this.addMethod = addMethod;
this.eventName = eventName;
this.eventClass = eventClass;
}
@Override
public PyObject _doget(PyObject self) {
if (self == null) {
return this;
}
initAdapter();
Object jself = Py.tojava(self, addMethod.getDeclaringClass());
Object field;
try {
field = adapterField.get(getAdapter(jself));
} catch (Exception exc) {
throw Py.JavaError(exc);
}
PyCompoundCallable func;
if (field == null) {
func = new PyCompoundCallable();
setFunction(jself, func);
return func;
}
if (field instanceof PyCompoundCallable) {
return (PyCompoundCallable)field;
}
func = new PyCompoundCallable();
setFunction(jself, func);
func.append((PyObject)field);
return func;
}
@Override
public boolean _doset(PyObject self, PyObject value) {
Object jself = Py.tojava(self, addMethod.getDeclaringClass());
if (!(value instanceof PyCompoundCallable)) {
PyCompoundCallable func = new PyCompoundCallable();
setFunction(jself, func);
func.append(value);
} else {
setFunction(jself, value);
}
return true;
}
@Override
public String toString() {
return "<beanEventProperty " + __name__ + " for event " + eventClass.toString() + " "
+ Py.idstr(this) + ">";
}
private Object getAdapter(Object o, String evc) {
Map<String, WeakReference<Object>> ads = adapters.get(o);
if (ads == null) {
return null;
}
WeakReference<Object> adw = ads.get(evc);
if (adw == null) {
return null;
}
return adw.get();
}
private void putAdapter(Object o, String evc, Object ad) {
Map<String, WeakReference<Object>> ads = adapters.get(o);
if (ads == null) {
ads = Generic.map();
adapters.put(o, ads);
}
ads.put(evc, new WeakReference<Object>(ad));
}
private synchronized Object getAdapter(Object self) {
String eventClassName = eventClass.getName();
Object adapter = getAdapter(self, eventClassName);
if (adapter != null) {
return adapter;
}
try {
adapter = adapterClass.newInstance();
addMethod.invoke(self, adapter);
} catch (Exception e) {
throw Py.JavaError(e);
}
putAdapter(self, eventClassName, adapter);
return adapter;
}
private void initAdapter() {
if (adapterClass == null) {
adapterClass = getAdapterClass(eventClass);
try {
adapterField = adapterClass.getField(__name__);
} catch (NoSuchFieldException exc) {
throw Py.AttributeError("Internal bean event error: " + __name__);
}
}
}
private void setFunction(Object self, PyObject callable) {
initAdapter();
try {
adapterField.set(getAdapter(self), callable);
} catch (Exception exc) {
throw Py.JavaError(exc);
}
}
private synchronized static Class<?> getAdapterClass(Class<?> c) {
String name = "org.python.proxies." + c.getName() + "$Adapter";
Class<?> pc = Py.findClass(name);
if (pc == null) {
pc = adapterClasses.get(name);
if (pc == null) {
pc = MakeProxies.makeAdapter(c);
adapterClasses.put(name, pc);
}
}
return pc;
}
}
|