* Konstruktor
* @param obj
*/
public DynamicBlisterProxy(Object obj) {
super(obj);
}
/
* newInstance
* @param obj @see {@link #obj}
* @return proxy instance
*/
public static Object newInstance(Object obj) {
return java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(), getObjectInterfaces(obj.getClass()), new DynamicBlisterProxy(obj));
}
/
* newInstance
* @param interfaces to implement
* @return proxy instance
*/
public static Object newInstance(Class
interfaces) {
return java.lang.reflect.Proxy.newProxyInstance(DynamicProxy.class.getClassLoader(), getAllInterfaces(interfaces), new DynamicBlisterProxy(null));
}
/
* Works on historized objects in blister.
* If the proxy holds a real object, the callInsteadInvoking will only be called, if
* the method of an enhancing interface is called.
* If the proxy holds no real object, all methodcalls will use the callInsteadInvoking.
*
* @see de.idvag.blister.blst.pub.helper.proxy.DynamicProxy#callInsteadInvoking(java.lang.reflect.Method, java.lang.Object
)
*/
protected Object callInsteadInvoking(Object proxyInstance, Method method, Object
args) {
//if there is no real object, call the bean mechanism
if (getObject() == null) {
Object value = super.callInsteadInvoking(proxyInstance, method, args);
//wenn ein ArtIF.getKey() aufgerufen wird, muss die echte Art aus DB geholt werden!
if (value == null && isRequestingArtKey(proxyInstance, method)) {
value = evaluateOriginalArtKey(proxyInstance);
}
return value;
} else {
try{
if (method.getName().startsWith(ClassReflector.GET_PREFIX)) {
//if it is a getter and the DynamicBeanProxy has stored a Proxy as result, return this!
Object proxyResult = super.callInsteadInvoking(proxyInstance, method, args);
if (hasStoredProxyClass(proxyResult))
return proxyResult;
else
//else, call the original getter method
return getHistoryObject(method.getName(), getObject());
}
else if (method.getName().startsWith(ClassReflector.SET_PREFIX)) {
try {
setHistoryObject(method.getName(), getObject(), args0
);
} catch (Exception e) {
trace(e.getMessage());
//perhaps this method doesn't exist, because, it is no history enhancing --> call the bean method
super.callInsteadInvoking(proxyInstance, method, args);
}
}
else
trace("DynamicBlisterProxy.callInsteadInvoking() has nothing to do. No getter and no setter invoked!");
} catch(Exception ex) {
throw new RuntimeException(ex);
}
}
return null;
}
/
* evaluates key of database object of type art.
* @param method method with declaring class information.
* @return key of Art
*/
private Object evaluateOriginalArtKey(Object proxyInstance) {
Method methodGetValue;
Class clazz = getFirstImplementingInterface(proxyInstance);
try {
methodGetValue = clazz.getMethod("getValue", new Class0
);
Object value = methodGetValue.invoke(proxyInstance, new Object0
);
//Partei-Arten
if (ArtIF.class.isAssignableFrom(clazz)) {
ArtBrokerIF brokerIF = (ArtBrokerIF)PersistenceBrokerFactory.getBroker(clazz);
ArtIF originalArt = brokerIF.findByValue((String)value);
value = originalArt.getKey();
//Blister-Arten
} else if (de.idvag.blister.blst.arten.priv.persist.ArtIF.class.isAssignableFrom(clazz)) {
gen.de.idvag.blister.blst.arten.priv.persist.ArtBrokerIF
brokerIF = (gen.de.idvag.blister.blst.arten.priv.persist.ArtBrokerIF)PersistenceBrokerFactory.getBroker(clazz);
de.idvag.blister.blst.arten.priv.persist.ArtIF
originalArt = brokerIF.findByValue((String)value);
value = originalArt.getKey();
}
setBeanAttribute("key", value);
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/
* isRequestingArtKey
* @param method to be called
* @return true, if method call getKey() of an ArtIF instance.
*/
private boolean isRequestingArtKey(Object proxyInstance, Method method) {
Class clazz = getFirstImplementingInterface(proxyInstance);
if (EnumPersistentIF.class.isAssignableFrom(clazz)) {
if (method.getName().equals("getKey"))
return true;
}
return false;
}
/
* getHistoryObject
* @param name name
* @param instance instance
* @return HistoryObject
* @throws Exception ..
*/
public static Object getHistoryObject(String name, Object instance) throws Exception {
return ReflectionUtil.getByReflection(name+"()", instance, new Object
{new Date()});
}
/
* setHistoryObject
* @param name name
* @param instance instance
* @param value value
* @throws Exception ..
*/
public static void setHistoryObject(String name, Object instance, Object value) throws Exception {
try {
ReflectionUtil.getByReflection(name+"()", instance, new Object
{value, Period.createDatePeriod()});
} catch (RuntimeException e) {
//zweiter Versuch: vertauschte Parameter
ReflectionUtil.getByReflection(name+"()", instance, new Object
{Period.createDatePeriod(), value});
}
}
/
* getCurrentTransaction
* @return
* @throws PersistenceException
*/
TransactionContextIF getCurrentTransaction() throws PersistenceException {
return blisterKundeBrokerIF.getCurrentTransactionContext();
}
/
* callAfterInvoking
* @see de.idvag.blister.blst.pub.helper.proxy.DynamicProxy#callAfterInvoking(java.lang.reflect.Method, java.lang.Object
)
*/
protected void callAfterInvoking(Method method, Object
args) {
//super.callAfterInvoking(method, args);
}
/
* callBeforeInvoking
* @see de.idvag.blister.blst.pub.helper.proxy.DynamicProxy#callBeforeInvoking(java.lang.reflect.Method, java.lang.Object
)
*/
protected void callBeforeInvoking(Method method, Object
args) {
//zum verändern eines jump persistent-objects muss dieses durch register() modifizierbar gesetzt werden.
if (method.getName().startsWith(ClassReflector.SET_PREFIX)
&& (getObject() instanceof PersistentObjectIF)) {
try {
PersistentObjectIF po = (PersistentObjectIF)getObject();
if (!getCurrentTransaction().isRegistered(po)) {
trace("DynamicBlisterProxy.callBeforeInvoking(): register persistent object for modifying: " + po);
setObject(getCurrentTransaction().register(po));
}
} catch (PersistenceException e) {
throw new RuntimeException(e);
}
}
}
]]>
|