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 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: AdminConnectionFactory.java,v 1.12 2003/08/18 10:15:32 tanderson Exp $
44   */
45  package org.exolab.jms.administration;
46  
47  import java.net.MalformedURLException;
48  
49  import javax.jms.JMSException;
50  import javax.jms.JMSSecurityException;
51  
52  import org.exolab.core.util.URI;
53  import org.exolab.jms.administration.http.HttpJmsAdminConnection;
54  import org.exolab.jms.administration.http.SslHttpJmsAdminConnection;
55  import org.exolab.jms.administration.intravm.IntravmJmsAdminConnection;
56  import org.exolab.jms.administration.mipc.IpcJmsAdminConnection;
57  import org.exolab.jms.administration.mipc.SslIpcJmsAdminConnection;
58  import org.exolab.jms.administration.rmi.RmiJmsAdminConnection;
59  import org.exolab.jms.config.types.SchemeType;
60  
61  
62  /***
63   * This class is a factory for creating {@link AdminConnection} instances.
64   *
65   * @version     $Revision: 1.12 $ $Date: 2003/08/18 10:15:32 $
66   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67   * @author      <a href="mailto:knut@lerpold.no">Knut Lerpold</a>
68   * @see         JmsAdminServerIfc
69   */
70  public class AdminConnectionFactory {
71  
72      /***
73       * Attempt to establish an admin connection
74       *
75       * @param url the OpenJMS server URL
76       * @param username the admin user name
77       * @param password the admin user password
78       * @return an admin connection
79       * @throws JMSException if a connection cannot be established
80       * @throws MalformedURLException if <code>url</code> is invalid
81       */
82      private static AdminConnection getConnection(String url, String username,
83                                                   String password)
84          throws JMSException, MalformedURLException {
85          // made private for the time being. Need to sort out the 
86          // AdminConnection interface post 0.7.6
87          AdminConnection result = null;
88  
89          URI parser = null;
90          try {
91              parser = new URI(url);
92          } catch (URI.MalformedURIException exception) {
93              throw new MalformedURLException(exception.getMessage());
94          }
95  
96          String protocol = parser.getScheme();
97          String host = parser.getHost();
98          int port = parser.getPort();
99          String path = parser.getPath();
100         if (host != null) {
101             if (host.trim().length() == 0) {
102                 host = null;
103             }
104         }
105         if (path != null) {
106             if (path.trim().length() == 0) {
107                 path = null;
108             } else if (path.startsWith("/")) {
109                 path = path.substring(1);
110                 if (path.length() == 0) {
111                     path = null;
112                 }
113             }
114         }
115 
116         SchemeType scheme = null;
117         try {
118             scheme = SchemeType.valueOf(protocol);
119         } catch (IllegalArgumentException exception) {
120             throw new MalformedURLException("Invalid scheme: " + protocol);
121         }
122 
123         // check URL validity
124         switch (scheme.getType()) {
125             case SchemeType.TCP_TYPE:
126             case SchemeType.TCPS_TYPE:
127                 if (host == null || port == -1 || path != null) {
128                     throw new MalformedURLException("Invalid URL: " + url);
129                 }
130                 break;
131             case SchemeType.RMI_TYPE:
132             case SchemeType.HTTP_TYPE:
133             case SchemeType.HTTPS_TYPE:
134                 if (host == null || port == -1) {
135                     throw new MalformedURLException("Invalid URL: " + url);
136                 }
137                 break;
138             case SchemeType.EMBEDDED_TYPE:
139                 if (host != null || port != -1 || path != null) {
140                     throw new MalformedURLException("Invalid URL: " + url);
141                 }
142                 break;
143         }
144 
145         // create the connection
146         switch (scheme.getType()) {
147             case SchemeType.TCP_TYPE:
148                 result = new IpcJmsAdminConnection(host, port, username,
149                     password);
150                 break;
151             case SchemeType.TCPS_TYPE:
152                 result = new SslIpcJmsAdminConnection(host, port, username,
153                     password);
154                 break;
155             case SchemeType.RMI_TYPE:
156                 result = new RmiJmsAdminConnection(host, port, path,
157                     username, password);
158                 break;
159             case SchemeType.HTTP_TYPE:
160                 result = new HttpJmsAdminConnection(host, port, path,
161                     username, password);
162                 break;
163             case SchemeType.HTTPS_TYPE:
164                 result = new SslHttpJmsAdminConnection(host, port, path,
165                     username, password);
166                 break;
167             case SchemeType.EMBEDDED_TYPE:
168                 result = new IntravmJmsAdminConnection(username, password);
169                 break;
170         }
171 
172         return result;
173     }
174 
175     /***
176      * Create an unauthenticated admin connection, using the specified server
177      * URL
178      *
179      * @param url the OpenJMS server URL
180      * @return JmsAdminServerIfc the administration API
181      * @throws JMSException if a connection cannot be established
182      * @throws MalformedURLException if <code>url</code> is invalid
183      */
184     public static JmsAdminServerIfc create(String url)
185         throws JMSException, MalformedURLException {
186         return create(url, null, null);
187     }
188 
189     /***
190      * Create an authenticated admin connection, using the specified server
191      * URL, name, and password
192      *
193      * @param url the OpenJMS server URL
194      * @param username the admin user name
195      * @param password the admin user password
196      * @return JmsAdminServerIfc the administration API
197      * @throws JMSException if a connection cannot be established
198      * @throws MalformedURLException if <code>url</code> is invalid
199      */
200     public static JmsAdminServerIfc create(String url, String username,
201                                            String password)
202         throws JMSException, MalformedURLException {
203         JmsAdminServerIfc result = null;
204 
205         URI parser = null;
206         try {
207             parser = new URI(url);
208         } catch (URI.MalformedURIException exception) {
209             throw new MalformedURLException(exception.getMessage());
210         }
211         String protocol = parser.getScheme();
212         String host = parser.getHost();
213         int port = parser.getPort();
214         String path = parser.getPath();
215         if (host != null) {
216             if (host.trim().length() == 0) {
217                 host = null;
218             }
219         }
220         if (path != null) {
221             if (path.trim().length() == 0) {
222                 path = null;
223             } else if (path.startsWith("/")) {
224                 path = path.substring(1);
225                 if (path.length() == 0) {
226                     path = null;
227                 }
228             }
229         }
230 
231         SchemeType scheme = null;
232         try {
233             scheme = SchemeType.valueOf(protocol);
234         } catch (IllegalArgumentException exception) {
235             throw new MalformedURLException("Invalid scheme: " + protocol);
236         }
237 
238         // check URL validity
239         switch (scheme.getType()) {
240             case SchemeType.TCP_TYPE:
241             case SchemeType.TCPS_TYPE:
242                 if (host == null || port == -1 || path != null) {
243                     throw new MalformedURLException("Invalid URL: " + url);
244                 }
245                 break;
246             case SchemeType.RMI_TYPE:
247             case SchemeType.HTTP_TYPE:
248             case SchemeType.HTTPS_TYPE:
249                 if (host == null || port == -1) {
250                     throw new MalformedURLException("Invalid URL: " + url);
251                 }
252                 break;
253             case SchemeType.EMBEDDED_TYPE:
254                 if (host != null || port != -1 || path != null) {
255                     throw new MalformedURLException("Invalid URL: " + url);
256                 }
257                 break;
258         }
259 
260         // create the connection
261         switch (scheme.getType()) {
262             case SchemeType.TCP_TYPE:
263                 result = new IpcJmsAdminConnection(host, port, username,
264                     password);
265                 break;
266             case SchemeType.TCPS_TYPE:
267                 result = new SslIpcJmsAdminConnection(host, port, username,
268                     password);
269                 break;
270             case SchemeType.RMI_TYPE:
271                 result = new RmiJmsAdminConnection(host, port, path,
272                     username, password);
273                 break;
274             case SchemeType.HTTP_TYPE:
275                 result = new HttpJmsAdminConnection(host, port, path,
276                     username, password);
277                 break;
278             case SchemeType.HTTPS_TYPE:
279                 result = new SslHttpJmsAdminConnection(host, port, path,
280                     username, password);
281                 break;
282             case SchemeType.EMBEDDED_TYPE:
283                 result = new IntravmJmsAdminConnection(username, password);
284                 break;
285         }
286         return result;
287     }
288 
289 } //-- AdminConnectionFactory
290