KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > client > net > JmsServerStubImpl


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 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JmsServerStubImpl.java,v 1.4 2005/05/24 13:36:38 tanderson Exp $
44  */

45 package org.exolab.jms.client.net;
46
47 import java.rmi.AccessException JavaDoc;
48 import java.rmi.NotBoundException JavaDoc;
49 import java.rmi.RemoteException JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.Map JavaDoc;
52 import javax.jms.ExceptionListener JavaDoc;
53 import javax.jms.InvalidClientIDException JavaDoc;
54 import javax.jms.JMSException JavaDoc;
55 import javax.jms.JMSSecurityException JavaDoc;
56
57 import org.exolab.jms.client.JmsServerStubIfc;
58 import org.exolab.jms.net.connector.Caller;
59 import org.exolab.jms.net.connector.CallerListener;
60 import org.exolab.jms.net.orb.ORB;
61 import org.exolab.jms.net.registry.Registry;
62 import org.exolab.jms.server.ServerConnection;
63 import org.exolab.jms.server.ServerConnectionFactory;
64
65
66 /**
67  * This class is responsible for returning a reference to the remote JMS
68  * server.
69  *
70  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
71  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
72  * @version $Revision: 1.4 $ $Date: 2005/05/24 13:36:38 $
73  */

74 public class JmsServerStubImpl implements JmsServerStubIfc, CallerListener {
75
76     /**
77      * The ORB.
78      */

79     private ORB _orb;
80
81     /**
82      * Properties used to establish a connection to the remote server.
83      */

84     private final Map JavaDoc _properties;
85
86     /**
87      * The server URI;
88      */

89     private final String JavaDoc _serverURI;
90
91     /**
92      * Default user to connect to server. May be <code>null</code>.
93      */

94     private final String JavaDoc _defaultUser;
95
96     /**
97      * Default user's password. May be <code>null</code>.
98      */

99     private final String JavaDoc _defaultPassword;
100
101     /**
102      * The exception listener, which is shared by all the connections to the
103      * server.
104      */

105     private ExceptionListener JavaDoc _listener = null;
106
107
108     /**
109      * Construct a new <code>JmsServerStubImpl</code>.
110      *
111      * @param properties properties to initialise this with
112      * @param environment the environment used. May be <code>null</code>
113      */

114     public JmsServerStubImpl(Map JavaDoc properties, Map JavaDoc environment) {
115         if (properties == null) {
116             throw new IllegalArgumentException JavaDoc("Argument 'properties' is null");
117         }
118         _properties = properties;
119
120         _serverURI = (String JavaDoc) properties.get(ORB.PROVIDER_URI);
121         if (_serverURI == null) {
122             throw new IllegalArgumentException JavaDoc(
123                     "Argument 'properties' does not contain property "
124                     + ORB.PROVIDER_URI);
125         }
126         if (environment != null) {
127             _defaultUser = (String JavaDoc) environment.get(ORB.SECURITY_PRINCIPAL);
128             _defaultPassword = (String JavaDoc) environment.get(
129                     ORB.SECURITY_CREDENTIALS);
130         } else {
131             _defaultUser = null;
132             _defaultPassword = null;
133         }
134     }
135
136     /**
137      * Creates a connection with the specified user identity.
138      * <p/>
139      * The connection is created in stopped mode. No messages will be delivered
140      * until the <code>Connection.start</code> method is explicitly called.
141      * <p/>
142      * If <code>clientID</code> is specified, it indicates the pre-configured
143      * client identifier associated with the client
144      * <code>ConnectionFactory</code> object.
145      *
146      * @param clientID the pre-configured client identifier. May be
147      * <code>null</code>.
148      * @param user the caller's user name. May be <code>null</code>
149      * @param password the caller's password. May be <code>null</code>
150      * @return a newly created connection
151      * @throws InvalidClientIDException if the JMS client specifies an invalid
152      * or duplicate client ID.
153      * @throws JMSException if the JMS provider fails to create the
154      * connection due to some internal error.
155      * @throws JMSSecurityException if client authentication fails due to an
156      * invalid user name or password.
157      */

158     public ServerConnection createConnection(String JavaDoc clientID, String JavaDoc user,
159                                              String JavaDoc password)
160             throws JMSException JavaDoc {
161         ServerConnection stub;
162
163         if (user == null) {
164             user = _defaultUser;
165             password = _defaultPassword;
166         }
167
168         ServerConnectionFactory factory
169                 = getServerConnectionFactory(user, password);
170
171         ServerConnection connection
172                 = factory.createConnection(clientID, user, password);
173         stub = new JmsConnectionStubImpl(this, connection, _orb,
174                                          _serverURI, user, password);
175         return stub;
176     }
177
178     /**
179      * Set the exception listener so that the client can be notified of client
180      * disconnection events.
181      *
182      * @param listener the exception listener
183      */

184     public void setExceptionListener(ExceptionListener JavaDoc listener) {
185         _listener = listener;
186     }
187
188     /**
189      * Notifies that a caller has been disconnected.
190      *
191      * @param caller the caller that was disconnected
192      */

193     public void disconnected(Caller caller) {
194         if (_listener != null) {
195             _listener.onException(new JMSException JavaDoc("Lost connection"));
196         }
197     }
198
199     /**
200      * Looks up and returns the {@link ServerConnectionFactory} instance bound
201      * in the registry.
202      *
203      * @param user the caller's user name. May be <code>null</code>
204      * @param password the caller's password. May be <code>null</code>
205      * @return the bound {@link ServerConnectionFactory}
206      * @throws JMSException if lookup fails
207      */

208     private synchronized ServerConnectionFactory getServerConnectionFactory(
209             String JavaDoc user, String JavaDoc password)
210             throws JMSException JavaDoc {
211         ServerConnectionFactory factory = null;
212         Map JavaDoc properties = _properties;
213
214         if (user != null) {
215             properties = new HashMap JavaDoc(_properties);
216             properties.put(ORB.SECURITY_PRINCIPAL, user);
217             properties.put(ORB.SECURITY_CREDENTIALS, password);
218         }
219         Registry registry = null;
220         try {
221             if (_orb == null) {
222                 _orb = SharedORB.getInstance();
223             }
224             registry = _orb.getRegistry(properties);
225         } catch (AccessException JavaDoc exception) {
226             JMSSecurityException JavaDoc error = new JMSSecurityException JavaDoc(
227                     exception.getMessage());
228             error.setLinkedException(exception);
229             throw error;
230         } catch (RemoteException JavaDoc exception) {
231             JMSException JavaDoc error = new JMSException JavaDoc(
232                     "Failed to get registry service for URL: " + _serverURI);
233             error.setLinkedException(exception);
234             throw error;
235         }
236
237         try {
238             factory = (ServerConnectionFactory) registry.lookup("server");
239         } catch (NotBoundException JavaDoc exception) {
240             throw new JMSException JavaDoc("Server is not bound in the registry for URL: "
241                                    + _serverURI);
242         } catch (RemoteException JavaDoc exception) {
243             JMSException JavaDoc error = new JMSException JavaDoc(
244                     "Failed to lookup OpenJMS server for URL: " + _serverURI);
245             error.setLinkedException(exception);
246             throw error;
247         }
248         try {
249             _orb.addCallerListener(_serverURI, this);
250         } catch (RemoteException JavaDoc exception) {
251             JMSException JavaDoc error = new JMSException JavaDoc(
252                     "Failed to register for disconnection notification for "
253                     + "URL: " + _serverURI);
254             error.setLinkedException(exception);
255             throw error;
256         }
257         return factory;
258     }
259
260 }
261
Popular Tags