FileDocCategorySizeDatePackage
BaseFacade.javaAPI DocExample8826Mon Apr 03 19:58:06 BST 2000com.imaginary.lwp

BaseFacade

public abstract class BaseFacade extends Object implements Serializable

Fields Summary
private HashMap
cache
private Entity
entity
private Home
home
private transient ArrayList
listeners
private String
lastUpdateID
private long
lastUpdateTime
private long
objectID
Constructors Summary
public BaseFacade()

    
      
        super();
    
public BaseFacade(long oid)

        super();
        objectID = oid;
    
public BaseFacade(Entity ent)

        super();
        entity = ent;
        objectID = entity.getObjectID();
    
Methods Summary
public voidaddPropertyChangeListener(java.beans.PropertyChangeListener l)

        if( listeners == null ) {
            listeners = new ArrayList();
        }
        listeners.add(l);
    
public voidaddPropertyChangeListener(java.lang.String p, java.beans.PropertyChangeListener l)

        if( listeners == null ) {
            listeners = new ArrayList();
        }
        listeners.add(l);
    
public voidassign(long oid)

        if( objectID != -1L ) {
            throw new FacadeReuseException("Facade already assigned.");
        }
        else {
            objectID = oid;
        }
    
public voidassign(long oid, Entity ent)

        assign(oid);
        entity = ent;
    
public voidassign(long oid, java.util.HashMap vals)

        assign(oid);
    
protected booleancontains(java.lang.String attr)

        return cache.containsKey(attr);
    
public booleanequals(java.lang.Object ob)

        if( ob instanceof BaseFacade ) {
            BaseFacade ref = (BaseFacade)ob;

            return (ref.getObjectID() == getObjectID());
        }
        else {
            return false;
        }
    
protected voidfirePropertyChange()

        firePropertyChange(new PropertyChangeEvent(this, null, null, null));
    
protected voidfirePropertyChange(java.beans.PropertyChangeEvent evt)

        Iterator it;

        if( listeners == null ) {
            return;
        }
        it = listeners.iterator();
        while( it.hasNext() ) {
            PropertyChangeListener l = (PropertyChangeListener)it.next();

            l.propertyChange(evt);
        }
    
protected java.lang.Objectget(java.lang.String attr)

        return cache.get(attr);
    
public EntitygetEntity()

        if( entity == null ) {
            reconnect();
        }
        return entity;
    
public java.lang.StringgetLastUpdateID()

        if( lastUpdateID == null ) {
            try {
                lastUpdateID = getEntity().getLastUpdateID();
            }
            catch( RemoteException e ) {
                reconnect();
                lastUpdateID = getEntity().getLastUpdateID();
            }
        }
        return lastUpdateID;
    
public longgetLastUpdateTime()

        if( lastUpdateTime == -1L ) {
            try {
                lastUpdateTime = getEntity().getLastUpdateTime();
            }
            catch( RemoteException e ) {
                reconnect();
                lastUpdateTime = getEntity().getLastUpdateTime();
            }
        }
        return lastUpdateTime;
    
public longgetObjectID()

        return objectID;
    
public booleanhasListeners(java.lang.String prop)

        if( listeners == null ) {
            return false;
        }
        if( listeners.size() > 0 ) {
            return true;
        }
        else {
            return false;
        }
    
public inthashCode()

        Long l = new Long(getObjectID());

        return l.hashCode();
    
protected voidput(java.lang.String attr, java.lang.Object val)

        cache.put(attr, val);
    
protected voidreconnect()

        final BaseFacade ref = this;
        Thread t;

        if( home == null ) {
            String url = System.getProperty(LWPProperties.RMI_URL);
            ObjectServer svr;

            try {
                svr = (ObjectServer)Naming.lookup(url);
            }
            catch( MalformedURLException e ) {
                throw new RemoteException(e.getMessage());
            }
            catch( NotBoundException e ) {
                throw new RemoteException(e.getMessage());
            }
            try {
                Identifier id = Identifier.currentIdentifier();

                if( id == null ) {
                    id = Identifier.getServerID();
                }
                home = (Home)svr.lookup(id, getClass().getName());
            }
            catch( LookupException e ) {
                throw new RemoteException(e.getMessage());
            }
        }
        try {
            Identifier id = Identifier.currentIdentifier();
            
            entity = home.findByObjectID(id, objectID);
            lastUpdateID = entity.getLastUpdateID();
            lastUpdateTime = entity.getLastUpdateTime();
        }
        catch( PersistenceException e ) {
            throw new RemoteException(e.getMessage());
        }
        catch( FindException e ) {
            throw new RemoteException(e.getMessage());
        }
        catch( RemoteException e ) {
            e.printStackTrace();
            String url = System.getProperty(LWPProperties.RMI_URL);
            ObjectServer svr;
            
            try {
                svr = (ObjectServer)Naming.lookup(url);
            }
            catch( MalformedURLException salt ) {
                throw new RemoteException(salt.getMessage());
            }
            catch( NotBoundException salt ) {
                throw new RemoteException(salt.getMessage());
            }
            try {
                Identifier id = Identifier.currentIdentifier();

                if( id == null ) {
                    id = Identifier.getServerID();
                }
                home = (Home)svr.lookup(id, getClass().getName());
                entity = home.findByObjectID(id, objectID);
                lastUpdateID = entity.getLastUpdateID();
                lastUpdateTime = entity.getLastUpdateTime();
            }
            catch( LookupException salt ) {
                throw new RemoteException(salt.getMessage());
            }
            catch( PersistenceException salt ) {
                throw new RemoteException(salt.getMessage());
            }
            catch( FindException salt ) {
                throw new RemoteException(salt.getMessage());
            }
        }
        t = new Thread() {
            public void run() {
                while( true ) {
                    synchronized( ref ) {
                        if( entity == null ) {
                            return;
                        }
                        try {
                            if( lastUpdateTime == -1L ) {
                                lastUpdateTime = entity.getLastUpdateTime();
                            }
                            if( entity.isChanged(lastUpdateTime) ) {
                                lastUpdateTime = entity.getLastUpdateTime();
                                lastUpdateID = entity.getLastUpdateID();
                                firePropertyChange();
                            }
                        }
                        catch( RemoteException e ) {
                            // this will force a reload
                            entity = null;
                            return;
                        }
                    }
                    try { Thread.sleep(30000); }
                    catch( InterruptedException e ) { }
                }
            }
        };
        t.setPriority(Thread.MIN_PRIORITY);
        t.start();
    
public voidremovePropertyChangeListener(java.beans.PropertyChangeListener l)

        if( listeners == null ) {
            return;
        }
        listeners.remove(l);
    
public voidremovePropertyChangeListener(java.lang.String p, java.beans.PropertyChangeListener l)

        if( listeners == null ) {
            return;
        }
        listeners.remove(l);
    
public synchronized voidreset()

        cache.clear();
        lastUpdateTime = -1L;
        lastUpdateID = null;