01: /*
02: * hgcommons 7
03: * Hammurapi Group Common Library
04: * Copyright (C) 2003 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.config;
24:
25: import java.net.MalformedURLException;
26: import java.rmi.Naming;
27: import java.rmi.NotBoundException;
28: import java.rmi.RemoteException;
29: import java.rmi.server.UnicastRemoteObject;
30:
31: import org.w3c.dom.Element;
32: import org.w3c.dom.Node;
33:
34: /**
35: * Base class for components exposed through RMI
36: * @author Pavel Vlasov
37: * @revision $Revision$
38: */
39: public class RmiComponent extends UnicastRemoteObject implements
40: Component, DomConfigurable {
41:
42: private int port;
43: private String name;
44: private String bindName;
45: protected Object owner;
46:
47: public RmiComponent() throws RemoteException {
48: super ();
49: }
50:
51: public void start() throws ConfigurationException {
52: bindName = "//localhost:" + port + "/" + name;
53: try {
54: Naming.rebind(bindName, this );
55: } catch (RemoteException e) {
56: throw new ConfigurationException("Could not bind to name '"
57: + bindName + "' - " + e, e);
58: } catch (MalformedURLException e) {
59: throw new ConfigurationException("Could not bind to name '"
60: + bindName + "' - " + e, e);
61: }
62: }
63:
64: public void stop() throws ConfigurationException {
65: if (bindName != null) {
66: try {
67: Naming.unbind(bindName);
68: unexportObject(this , false);
69: bindName = null;
70: } catch (RemoteException e) {
71: throw new ConfigurationException(
72: "Could not unbind/unexport '" + bindName
73: + "' - " + e, e);
74: } catch (MalformedURLException e) {
75: throw new ConfigurationException(
76: "Could not unbind name '" + bindName + "' - "
77: + e, e);
78: } catch (NotBoundException e) {
79: throw new ConfigurationException(
80: "Could not unbind name '" + bindName + "' - "
81: + e, e);
82: }
83: }
84: }
85:
86: public void setOwner(Object owner) {
87: this .owner = owner;
88: }
89:
90: public void configure(Node configNode, Context context) {
91: Element ce = (Element) configNode;
92: if (ce.hasAttribute("port")) {
93: port = Integer.parseInt(ce.getAttribute("port"));
94: }
95:
96: name = ce.getAttribute("name");
97: }
98:
99: }
|