001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.space;
020
021import org.jpos.core.Configuration;
022import org.jpos.core.ConfigurationException;
023import org.jpos.core.Configurable;
024
025import javax.naming.InitialContext;
026import javax.naming.NamingException;
027import java.io.Serializable;
028import java.rmi.RemoteException;
029import java.rmi.registry.LocateRegistry;
030import java.rmi.registry.Registry;
031import java.rmi.server.*;
032import java.util.Set;
033
034/**
035 * RMI Space Proxy 
036 * @author Alejandro Revilla
037 * @author Niclas Hedhman
038 * @version $Revision$ $Date$
039 * @since 1.4.9
040 */
041@SuppressWarnings("unchecked")
042public class SpaceProxy implements RemoteSpace, Configurable {
043    Space sp;
044    Configuration cfg;
045    private RemoteRef ref;
046    private RemoteStub stub;
047    public SpaceProxy () throws RemoteException {
048        super();
049        sp = SpaceFactory.getSpace ();
050        startService ();
051    }
052    public SpaceProxy (String spaceUri) throws RemoteException {
053        super ();
054        sp = SpaceFactory.getSpace (spaceUri);
055        startService ();
056    }
057
058    private void startService () throws RemoteException 
059    {
060        try {
061            LocateRegistry.createRegistry (Registry.REGISTRY_PORT);
062        } catch (ExportException ignored) {
063            // NOPMD: ok to happen
064        }
065        stub = UnicastRemoteObject.exportObject (this);
066        ref  = stub.getRef();
067    }
068
069    public void out (Serializable key, Serializable value) 
070        throws RemoteException
071    {
072        sp.out (key, value);
073    }
074    public void out (Serializable key, Serializable value, long timeout)
075        throws RemoteException
076    {
077        sp.out (key, value, timeout);
078    }
079    public Serializable in (Serializable key)
080        throws RemoteException
081    {
082        return (Serializable) sp.in (key);
083    }
084    public Serializable rd  (Serializable key)
085        throws RemoteException
086    {
087        return (Serializable) sp.rd (key);
088    }
089    public Serializable in  (Serializable key, long timeout)
090        throws RemoteException
091    {
092        return (Serializable) sp.in (key, timeout);
093    }
094    public Serializable rd  (Serializable key, long timeout)
095        throws RemoteException
096    {
097        return (Serializable) sp.rd (key, timeout);
098    }
099    public Serializable inp (Serializable key)
100        throws RemoteException
101    {
102        return (Serializable) sp.inp (key);
103    }
104    public Serializable rdp (Serializable key)
105        throws RemoteException
106    {
107        return (Serializable) sp.rdp (key);
108    }
109    public void shutdown() {
110        try {
111            if (UnicastRemoteObject.unexportObject (this, false)) 
112                return;
113            Thread.sleep (5000);
114            UnicastRemoteObject.unexportObject (this, true);
115        } catch (Exception ignored) {
116            // NOPMD: nothing to do .. we're shutting down ...
117        }
118    }
119    public void setConfiguration (Configuration cfg) 
120        throws ConfigurationException
121    {
122        this.cfg = cfg;
123        try {
124            InitialContext ctx = new InitialContext ();
125            ctx.rebind (cfg.get ("name"), stub);
126        } catch (NamingException e) {
127            throw new ConfigurationException (e);
128        }
129    }
130    public Set getKeySet () {
131        if (sp instanceof LocalSpace) 
132            return ((LocalSpace)sp).getKeySet ();
133        else
134            return null;
135    }
136    public String toString() {
137        if (ref == null)
138            return getClass().getName() + "[<unexported>]";
139        else
140            return getClass().getName() + "[" + ref.remoteToString() + "]";
141    }
142    public int hashCode() {
143        if (ref == null )
144            return super.hashCode();
145        else
146            return ref.remoteHashCode();
147    }
148    public boolean equals (Object obj) {
149        if (obj instanceof RemoteObject) {
150            if (ref == null) {
151                return obj == this;
152            } else {
153                RemoteRef otherRef = ((RemoteObject)obj).getRef();
154                return ref.remoteEquals (otherRef);
155            }
156        } else if (obj != null) {
157            return obj.equals (this);
158        } else {
159            return false;
160        }
161    }
162}
163