001    /*
002    @license.text@
003     */
004    package biz.hammurapi.config;
005    
006    import java.net.MalformedURLException;
007    import java.rmi.Naming;
008    import java.rmi.NotBoundException;
009    import java.rmi.RemoteException;
010    import java.rmi.server.UnicastRemoteObject;
011    
012    import org.w3c.dom.Element;
013    import org.w3c.dom.Node;
014    
015     /**
016      * Base class for components exposed through RMI
017      * @author Pavel Vlasov
018      * @revision $Revision$
019      */
020    public class RmiComponent extends UnicastRemoteObject implements Component, DomConfigurable {
021    
022            private int port;
023            private String name;
024            private String bindName;
025            protected Object owner;
026    
027            public RmiComponent() throws RemoteException {
028                    super();
029            }
030    
031            public void start() throws ConfigurationException {
032                    bindName = "//localhost:"+port+"/"+name;
033                    try {
034                            Naming.rebind(bindName, this);
035                    } catch (RemoteException e) {
036                            throw new ConfigurationException("Could not bind to name '"+bindName+"' - "+e, e);
037                    } catch (MalformedURLException e) {
038                            throw new ConfigurationException("Could not bind to name '"+bindName+"' - "+e, e);
039                    }
040            }
041    
042            public void stop() throws ConfigurationException {
043                    if (bindName!=null) {
044                            try {
045                                    Naming.unbind(bindName);
046                                    unexportObject(this, false);
047                                    bindName=null;
048                            } catch (RemoteException e) {
049                                    throw new ConfigurationException("Could not unbind/unexport '"+bindName+"' - "+e, e);
050                            } catch (MalformedURLException e) {
051                                    throw new ConfigurationException("Could not unbind name '"+bindName+"' - "+e, e);
052                            } catch (NotBoundException e) {
053                                    throw new ConfigurationException("Could not unbind name '"+bindName+"' - "+e, e);
054                            }
055                    }
056            }
057    
058            public void setOwner(Object owner) {
059                    this.owner=owner;
060            }
061    
062            public void configure(Node configNode, Context context) {
063                    Element ce = (Element) configNode;
064                    if (ce.hasAttribute("port")) {
065                            port=Integer.parseInt(ce.getAttribute("port"));
066                    }
067                    
068                    name=ce.getAttribute("name");
069            }
070    
071    }