1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: UnicastObject.java,v 1.2 2005/05/27 13:44:45 tanderson Exp $
44 */
45 package org.exolab.jms.net.orb;
46
47 import java.rmi.NoSuchObjectException;
48 import java.rmi.StubNotFoundException;
49 import java.rmi.server.ExportException;
50
51 import org.exolab.jms.net.proxy.Proxy;
52
53 /***
54 * <code>UnicastObject</code> is a convenience class which may be sublassed from
55 * in order to provide remoting of behaviour.
56 *
57 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
58 * @version $Revision: 1.2 $ $Date: 2005/05/27 13:44:45 $
59 */
60 public abstract class UnicastObject {
61
62 /***
63 * The ORB to export this with.
64 */
65 private ORB _orb;
66
67 /***
68 * The ORB to export this on. If <code>null</code>, the ORB default export
69 * URI will be used.
70 */
71 private final String _uri;
72
73 /***
74 * The proxy.
75 */
76 private Proxy _proxy;
77
78
79 /***
80 * Construct a new <code>UnicastObject</code>.
81 *
82 * @param orb the ORB to export this with
83 * @throws ExportException if the object cannot be exported
84 * @throws StubNotFoundException if the proxy class cannot be found
85 */
86 protected UnicastObject(ORB orb)
87 throws ExportException, StubNotFoundException {
88 this(orb, null);
89 }
90
91 /***
92 * Construct a new <code>UnicastObject</code>.
93 *
94 * @param orb the ORB to export this with
95 * @param uri the URI to export this on. If <code>null</code>, use the ORB
96 * default export URI
97 * @throws ExportException if the object cannot be exported
98 * @throws StubNotFoundException if the proxy class cannot be found
99 */
100 protected UnicastObject(ORB orb, String uri)
101 throws ExportException, StubNotFoundException {
102 this(orb, uri, false);
103 }
104
105 /***
106 * Construct a new <code>UnicastObject</code>.
107 *
108 * @param orb the ORB to export this with
109 * @param uri the URI to export this on
110 * @param exportTo if <code>false</code>, {@link ORB#exportObject(Object,
111 * String)} is used to export this. If <code>true</code> and
112 * <code>uri</code> is non-null,
113 * {@link ORB#exportObjectTo(Object, String)} is used,
114 * otherwise {@link ORB#exportObjectTo(Object)} is used.
115 * @throws ExportException if the object cannot be exported
116 * @throws StubNotFoundException if the proxy class cannot be found
117 */
118 protected UnicastObject(ORB orb, String uri, boolean exportTo)
119 throws ExportException, StubNotFoundException {
120 if (orb == null) {
121 throw new IllegalArgumentException("Argument 'orb' is null");
122 }
123 if (!exportTo) {
124 _proxy = orb.exportObject(this, uri);
125 } else if (uri != null) {
126 _proxy = orb.exportObjectTo(this, uri);
127 } else {
128 _proxy = orb.exportObjectTo(this);
129 }
130 _orb = orb;
131 _uri = uri;
132 }
133
134 /***
135 * Returns a proxy to invoke methods on this.
136 *
137 * @return a proxy to invoke methods on this
138 */
139 public Proxy getProxy() {
140 return _proxy;
141 }
142
143 /***
144 * Unexport this object.
145 *
146 * @throws NoSuchObjectException if the object isn't exported
147 */
148 public void unexportObject() throws NoSuchObjectException {
149 _orb.unexportObject(this);
150 }
151
152 /***
153 * Returns the ORB.
154 *
155 * @return the ORB
156 */
157 protected ORB getORB() {
158 return _orb;
159 }
160
161 /***
162 * Returns the URI this was exported on.
163 *
164 * @return the URI this was exported on, or <code>null</code> if the ORB
165 * default URI was used.
166 */
167 protected String getURI() {
168 return _uri;
169 }
170 }