ResultConverter.java :  » Database-DBMS » Ozone-1.1 » org » ozoneDB » core » Java Open Source

Java Open Source » Database DBMS » Ozone 1.1 
Ozone 1.1 » org » ozoneDB » core » ResultConverter.java
// You can redistribute this software and/or modify it under the terms of
// the Ozone Core License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: ResultConverter.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $

package org.ozoneDB.core;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import org.ozoneDB.*;

import org.ozoneDB.core.DbRemote.ProxyObjectGate;

/**
 * The base class for the classes that convert the parameter and results
 * of methods invocations that go through Database or ExternalDatabase.
 * 
 * 
 * @author <a href="http://www.softwarebuero.de/">SMB</a>
 * @author <a href="http://www.medium.net/">Medium.net</a>
 * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
 */
public final class ResultConverter extends Object {
    
    
    public static void updateProxyLinks( Object obj, OzoneInterface db ) throws Exception {
        String name = obj != null ? obj.getClass().getName() : "(null)";
        System.out.println( "*** Proxy test: " + name );
        
        if (obj == null) {
        //do nothing
        } else if (obj instanceof OzoneProxy) {
            ((OzoneProxy)obj).link = db;
            System.out.println( "   *** link changed *** " + db );
        } else {
            Class cl = obj.getClass();
            int mdf = cl.getModifiers();
            if (Modifier.isTransient( mdf ) || Modifier.isStatic( mdf ) || Modifier.isFinal( mdf )) {
            //do nothing
            } else if (cl.isPrimitive()) {
            //do nothing
            } else if (cl.isArray()) {
                int len = Array.getLength( obj );
                for (int j = 0; j < len; j++) {
                    Object member = Array.get( obj, j );
                    updateProxyLinks( member, db );
                } 
            } else {
                Field[] fields = cl.getFields();
                for (int i = 0; i < fields.length; i++) {
                    // System.out.println (fields[i].toString());
                    Object member = fields[i].get( obj );
                    updateProxyLinks( member, db );
                } 
            } 
        } 
    } 
    
    /**
    Substitute OzoneCompatible by a corresponding proxy. Do this recursive
    for all members.
    */
    public static Object substituteOzoneCompatibles(Object obj) throws Exception {
    return substituteOzoneCompatibles(obj,null);
    }
    
    /**
    Substitute OzoneCompatible by a corresponding proxy. Do this recursive
    for all members.

        @param proxyObjectGate
            the client ProxyObjectGate whose objectsReferencesByClient should be filled with all OzoneProxy-Objects
            found herein or null, if no database client's ProxyObjectGate objectsReferencesByClient should be updated.
     */
    public static Object substituteOzoneCompatibles(Object obj,ProxyObjectGate proxyObjectGate) throws Exception {
        //        String name = obj != null ? obj.getClass().getName() : "(null)";
        //        System.out.println ("***OzoneCompatible test:" + name);
        
        //FIXME: should be recursive
//      if (obj != null && obj instanceof OzoneCompatible) {
        if (obj instanceof OzoneCompatible) {
            // System.out.println ("*** substitute ***");
            OzoneProxy proxy = ((OzoneCompatible)obj).container().ozoneProxy();

            if (proxyObjectGate!=null) {
                proxyObjectGate.addObjectReferencedByClient(proxy);
            }

            return proxy;
        // return (((OzoneCompatible)obj).objID()!=null) ? ((OzoneCompatible)obj).container().ozoneProxy() : obj;
        } 

        if (proxyObjectGate!=null) { // If the object is not OzoneCompatible, it may be OzoneProxy, which should also be registered at the client.
          if (obj instanceof OzoneProxy) {
                proxyObjectGate.addObjectReferencedByClient((OzoneProxy) obj);
          }
        }
        return obj;
    } 
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.