View Javadoc

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-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: RmiJmsServerStub.java,v 1.15 2003/08/07 13:32:54 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 04/18/2000    jima    Created
47   */
48  package org.exolab.jms.client.rmi;
49  
50  import java.rmi.Naming;
51  import java.rmi.RemoteException;
52  import java.util.Hashtable;
53  
54  import javax.jms.ExceptionListener;
55  import javax.jms.JMSException;
56  
57  import org.exolab.jms.client.JmsConnectionStubIfc;
58  import org.exolab.jms.client.JmsServerStubIfc;
59  import org.exolab.jms.server.rmi.RemoteJmsServerConnectionIfc;
60  import org.exolab.jms.server.rmi.RemoteJmsServerIfc;
61  
62  
63  /***
64   * This class is responsible for returning a reference to the remote JMS
65   * server.
66   *
67   * @version     $Revision: 1.15 $ $Date: 2003/08/07 13:32:54 $
68   * @author      <a href="mailto:jima@intalio.com">Jim Alateras</a>
69   */
70  public class RmiJmsServerStub implements JmsServerStubIfc {
71  
72      /***
73       * This is a reference to the remote server stub that is constructed during
74       * object initialisation.
75       */
76      private RemoteJmsServerIfc _delegate = null;
77  
78      /***
79       * The server URL
80       */
81      private final String _serverURL;
82  
83      /***
84       * The exception listener, which is shared by all the connections to the
85       * server
86       */
87      private ExceptionListener _listener = null;
88  
89      /***
90       * This is the client ping interval time for all rmi clients connecting
91       * to this server. A value of 0 disables the ping functionality.
92       */
93      private final int _pingInterval;
94  
95      /***
96       * The constructor instantiate server based on the environment variables
97       * passed in
98       *
99       * @param environment the environment
100      */
101     public RmiJmsServerStub(Hashtable environment) {
102         if (environment == null) {
103             throw new IllegalArgumentException("Argument environment is null");
104         }
105 
106         _serverURL = (String) environment.get(RmiJmsConstants.SERVER_URL);
107         if (_serverURL == null) {
108             throw new IllegalArgumentException(
109                 "Environment does not contain property " +
110                 RmiJmsConstants.SERVER_URL);
111         }
112 
113         String interval = (String) environment.get(
114             RmiJmsConstants.RMI_CLIENT_PING_INTERVAL);
115         if (interval == null) {
116             throw new IllegalArgumentException(
117                 "Environment does not contain property " +
118                 RmiJmsConstants.RMI_CLIENT_PING_INTERVAL);
119         }
120         try {
121             _pingInterval = Integer.parseInt(interval);
122         } catch (NumberFormatException exception) {
123             throw new IllegalArgumentException(
124                 "Property " + RmiJmsConstants.RMI_CLIENT_PING_INTERVAL +
125                 " is invalid, value=" + interval);
126         }
127     }
128 
129     /***
130      * This class attempts to return a delegate remote stub to the RMI JMS
131      * Server. If there are any problems creating or returning the singleton
132      * then throw JMSException
133      *
134      * @return remote reference to stub
135      * @throws JMSException if the stub cannot be obtained
136      */
137     public synchronized RemoteJmsServerIfc getDelegate() throws JMSException {
138         if (_delegate == null) {
139             try {
140                 _delegate = (RemoteJmsServerIfc) Naming.lookup(_serverURL);
141             } catch (Exception exception) {
142                 String msg = "Failed to locate server, url=" + _serverURL +
143                     ": " + exception.getMessage();
144                 JMSException error = new JMSException(msg);
145                 error.setLinkedException(error);
146                 throw error;
147             }
148         }
149 
150         return _delegate;
151     }
152 
153     /***
154      * Create a connection to the JMS Server. If one cannot be established then
155      * throw JMSException exception
156      *
157      * @param id identity of client
158      * @param username the client username
159      * @param password  the client password
160      * @return created stub
161      * @throws JMSException if a connection cannot be established
162      */
163     public JmsConnectionStubIfc createConnection(String id, String username,
164                                                  String password)
165         throws JMSException {
166         JmsConnectionStubIfc stub = null;
167 
168         try {
169             RemoteJmsServerConnectionIfc connection =
170                 getDelegate().createConnection(id, username, password);
171             if (connection != null) {
172                 stub = new RmiJmsConnectionStub(connection, _pingInterval,
173                     this);
174             } else {
175                 throw new JMSException("Failed to create remote connection");
176             }
177         } catch (RemoteException exception) {
178             JMSException error = new JMSException(
179                 "Failed to create connection: " + exception.getMessage());
180             error.setLinkedException(exception);
181             throw error;
182         }
183 
184         return stub;
185     }
186 
187     // implementation of JmsServerStubIfc.setExceptionListener
188     public void setExceptionListener(ExceptionListener listener) {
189         _listener = listener;
190     }
191 
192     /***
193      * Return a reference to the exception listener
194      *
195      * @param ExceptionListener
196      */
197     public ExceptionListener getExceptionListener() {
198         return _listener;
199     }
200 
201 } //-- RmiJmsServerStub