001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: import java.lang.reflect.Field;
005: import java.lang.reflect.Method;
006:
007: public class PyBeanEventProperty extends PyReflectedField {
008: public Method addMethod;
009: public String eventName;
010: public Class eventClass;
011: public String __name__;
012:
013: public PyBeanEventProperty(String eventName, Class eventClass,
014: Method addMethod, Method eventMethod) {
015: __name__ = eventMethod.getName().intern();
016: this .addMethod = addMethod;
017: this .eventName = eventName;
018: this .eventClass = eventClass;
019: }
020:
021: public PyObject _doget(PyObject self) {
022: if (self == null)
023: return this ;
024:
025: initAdapter();
026:
027: Object jself = Py.tojava(self, addMethod.getDeclaringClass());
028:
029: Object field;
030: try {
031: field = adapterField.get(getAdapter(jself));
032: } catch (Exception exc) {
033: throw Py.JavaError(exc);
034: }
035:
036: PyCompoundCallable func;
037: if (field == null) {
038: func = new PyCompoundCallable();
039: setFunction(jself, func);
040: return func;
041: }
042: if (field instanceof PyCompoundCallable)
043: return (PyCompoundCallable) field;
044:
045: func = new PyCompoundCallable();
046: setFunction(jself, func);
047: func.append((PyObject) field);
048: return func;
049: }
050:
051: private synchronized static Class getAdapterClass(Class c) {
052: // System.err.println("getting adapter for: "+c+", "+c.getName());
053: InternalTables tbl = PyJavaClass.getInternalTables();
054: Object o = tbl.getAdapterClass(c);
055: if (o != null)
056: return (Class) o;
057: Class pc = Py.findClass("org.python.proxies." + c.getName()
058: + "$Adapter");
059: if (pc == null) {
060: //System.err.println("adapter not found for: "+
061: // "org.python.proxies."+
062: // c.getName()+"$Adapter");
063: pc = MakeProxies.makeAdapter(c);
064: }
065: tbl.putAdapterClass(c, pc);
066: return pc;
067: }
068:
069: private synchronized Object getAdapter(Object self) {
070: InternalTables tbl = PyJavaClass.getInternalTables();
071: String eventClassName = eventClass.getName();
072:
073: Object adapter = tbl.getAdapter(self, eventClassName);
074: if (adapter != null)
075: return adapter;
076:
077: try {
078: adapter = adapterClass.newInstance();
079: addMethod.invoke(self, new Object[] { adapter });
080: } catch (Exception e) {
081: throw Py.JavaError(e);
082: }
083: tbl.putAdapter(self, eventClassName, adapter);
084: return adapter;
085: }
086:
087: private Field adapterField;
088: private Class adapterClass;
089:
090: private void initAdapter() {
091: if (adapterClass == null) {
092: adapterClass = getAdapterClass(eventClass);
093: }
094: if (adapterField == null) {
095: try {
096: adapterField = adapterClass.getField(__name__);
097: } catch (NoSuchFieldException exc) {
098: throw Py.AttributeError("Internal bean event error: "
099: + __name__);
100: }
101: }
102: }
103:
104: private void setFunction(Object self, PyObject callable) {
105: initAdapter();
106: try {
107: adapterField.set(getAdapter(self), callable);
108: } catch (Exception exc) {
109: throw Py.JavaError(exc);
110: }
111: }
112:
113: public boolean _doset(PyObject self, PyObject value) {
114: Object jself = Py.tojava(self, addMethod.getDeclaringClass());
115: if (!(value instanceof PyCompoundCallable)) {
116: PyCompoundCallable func = new PyCompoundCallable();
117: setFunction(jself, func);
118: func.append(value);
119: } else {
120: setFunction(jself, value);
121: }
122: return true;
123: }
124:
125: public String toString() {
126: return "<beanEventProperty " + __name__ + " for event "
127: + eventClass.toString() + " " + Py.idstr(this ) + ">";
128: }
129: }
|