001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: ResultConverter.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $
008:
009: package org.ozoneDB.core;
010:
011: import java.io.*;
012: import java.util.*;
013: import java.lang.reflect.*;
014: import org.ozoneDB.*;
015:
016: import org.ozoneDB.core.DbRemote.ProxyObjectGate;
017:
018: /**
019: * The base class for the classes that convert the parameter and results
020: * of methods invocations that go through Database or ExternalDatabase.
021: *
022: *
023: * @author <a href="http://www.softwarebuero.de/">SMB</a>
024: * @author <a href="http://www.medium.net/">Medium.net</a>
025: * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
026: */
027: public final class ResultConverter extends Object {
028:
029: public static void updateProxyLinks(Object obj, OzoneInterface db)
030: throws Exception {
031: String name = obj != null ? obj.getClass().getName() : "(null)";
032: System.out.println("*** Proxy test: " + name);
033:
034: if (obj == null) {
035: //do nothing
036: } else if (obj instanceof OzoneProxy) {
037: ((OzoneProxy) obj).link = db;
038: System.out.println(" *** link changed *** " + db);
039: } else {
040: Class cl = obj.getClass();
041: int mdf = cl.getModifiers();
042: if (Modifier.isTransient(mdf) || Modifier.isStatic(mdf)
043: || Modifier.isFinal(mdf)) {
044: //do nothing
045: } else if (cl.isPrimitive()) {
046: //do nothing
047: } else if (cl.isArray()) {
048: int len = Array.getLength(obj);
049: for (int j = 0; j < len; j++) {
050: Object member = Array.get(obj, j);
051: updateProxyLinks(member, db);
052: }
053: } else {
054: Field[] fields = cl.getFields();
055: for (int i = 0; i < fields.length; i++) {
056: // System.out.println (fields[i].toString());
057: Object member = fields[i].get(obj);
058: updateProxyLinks(member, db);
059: }
060: }
061: }
062: }
063:
064: /**
065: Substitute OzoneCompatible by a corresponding proxy. Do this recursive
066: for all members.
067: */
068: public static Object substituteOzoneCompatibles(Object obj)
069: throws Exception {
070: return substituteOzoneCompatibles(obj, null);
071: }
072:
073: /**
074: Substitute OzoneCompatible by a corresponding proxy. Do this recursive
075: for all members.
076:
077: @param proxyObjectGate
078: the client ProxyObjectGate whose objectsReferencesByClient should be filled with all OzoneProxy-Objects
079: found herein or null, if no database client's ProxyObjectGate objectsReferencesByClient should be updated.
080: */
081: public static Object substituteOzoneCompatibles(Object obj,
082: ProxyObjectGate proxyObjectGate) throws Exception {
083: // String name = obj != null ? obj.getClass().getName() : "(null)";
084: // System.out.println ("***OzoneCompatible test:" + name);
085:
086: //FIXME: should be recursive
087: // if (obj != null && obj instanceof OzoneCompatible) {
088: if (obj instanceof OzoneCompatible) {
089: // System.out.println ("*** substitute ***");
090: OzoneProxy proxy = ((OzoneCompatible) obj).container()
091: .ozoneProxy();
092:
093: if (proxyObjectGate != null) {
094: proxyObjectGate.addObjectReferencedByClient(proxy);
095: }
096:
097: return proxy;
098: // return (((OzoneCompatible)obj).objID()!=null) ? ((OzoneCompatible)obj).container().ozoneProxy() : obj;
099: }
100:
101: if (proxyObjectGate != null) { // If the object is not OzoneCompatible, it may be OzoneProxy, which should also be registered at the client.
102: if (obj instanceof OzoneProxy) {
103: proxyObjectGate
104: .addObjectReferencedByClient((OzoneProxy) obj);
105: }
106: }
107: return obj;
108: }
109: }
|