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 2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: RegistryHelper.java,v 1.1 2004/11/26 01:51:06 tanderson Exp $
44   */
45  package org.exolab.jms.net.rmi;
46  
47  import java.rmi.AlreadyBoundException;
48  import java.rmi.NotBoundException;
49  import java.rmi.RemoteException;
50  import java.rmi.registry.Registry;
51  
52  import org.exolab.jms.net.connector.ResourceException;
53  import org.exolab.jms.net.uri.URI;
54  
55  
56  /***
57   * Helper class for RMI registry operations
58   *
59   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60   * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:06 $
61   */
62  final class RegistryHelper {
63  
64      /***
65       * Bind suffix for invoker factories
66       */
67      private static final String BIND_SUFFIX =
68              RMIInvokerFactory.class.getName();
69  
70  
71      /***
72       * Prevent construction of helper class
73       */
74      private RegistryHelper() {
75      }
76  
77      /***
78       * Bind an invoker factory in the registry
79       *
80       * @param factory  the invoker factory
81       * @param uri      the URI of the managed connection, to uniquely identify
82       *                 the factory
83       * @param registry the registry to use
84       * @throws ResourceException if the bind fails
85       */
86      public static void bind(RMIInvokerFactory factory, URI uri,
87                              Registry registry) throws ResourceException {
88  
89          String name = getName(uri);
90  
91          try {
92              registry.bind(name, factory);
93          } catch (AlreadyBoundException exception) {
94              throw new ResourceException("Binding exists for " + name,
95                                          exception);
96          } catch (RemoteException exception) {
97              throw new ResourceException("Failed to bind connection factory",
98                                          exception);
99          }
100     }
101 
102     /***
103      * Unbind an invoker factory from the registry
104      *
105      * @param factory  the invoker factory
106      * @param uri      the URI of the managed connection, to uniquely identify
107      *                 the factory
108      * @param registry the registry to use
109      * @throws ResourceException if the unbind fails
110      */
111     public static void unbind(RMIInvokerFactory factory, URI uri,
112                               Registry registry) throws ResourceException {
113         String name = getName(uri);
114         try {
115             registry.unbind(name);
116         } catch (NotBoundException exception) {
117             throw new ResourceException("No binding exists for " + name,
118                                         exception);
119         } catch (RemoteException exception) {
120             throw new ResourceException("Failed to unbind connection factory",
121                                         exception);
122         }
123     }
124 
125     /***
126      * Returns the invoker factory bind name. This is the path portion of the
127      * connection URI suffixed with <code>"RMIInvokerFactory"</code>
128      *
129      * @param uri the connection URI
130      * @return the invoker factory bind name
131      */
132     public static String getName(URI uri) {
133         String path = uri.getPath();
134         if (path == null) {
135             path = "/";
136         } else if (!path.startsWith("/")) {
137             path = "/" + path;
138         }
139         if (!path.endsWith("/")) {
140             path += "/";
141         }
142 
143         return path + BIND_SUFFIX;
144     }
145 
146     /***
147      * Determines if there are any bindings in the registry
148      *
149      * @param registry the RMI registry to lookup
150      * @return <code>true</code> if one or more bindings exist
151      * @throws RemoteException if the operation fails
152      */
153     public static boolean hasBindings(Registry registry)
154             throws RemoteException {
155         String[] names = registry.list();
156         return (names.length != 0);
157     }
158 
159 }