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 2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: NameServiceProxy.java,v 1.2 2003/08/07 13:32:58 tanderson Exp $
44   */
45  package org.exolab.jms.jndi.mipc;
46  
47  import java.util.Vector;
48  
49  import javax.naming.Binding;
50  import javax.naming.Context;
51  import javax.naming.Name;
52  import javax.naming.NameClassPair;
53  import javax.naming.NameParser;
54  import javax.naming.NamingException;
55  
56  import org.apache.avalon.excalibur.naming.NamingProvider;
57  
58  import org.exolab.core.ipc.Client;
59  
60  
61  /***
62   * The underlying communication interface for remote contexts over the tcp
63   * connector.
64   *
65   * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
66   * @version $Revision: 1.2 $
67   */
68  public class NameServiceProxy implements NamingProvider {
69  
70      /***
71       * The server connection
72       */
73      private Client _connection;
74  
75  
76      public NameServiceProxy(Client connection) {
77          _connection = connection;
78      }
79  
80      public NameParser getNameParser() throws NamingException {
81          NameParser parser;
82          Vector request = pack("getNameParser", 0);
83          synchronized (_connection) {
84              send(request);
85              parser = (NameParser) checkReply("getNameParser");
86          }
87          return parser;
88      }
89  
90      public void bind(Name name, String className, Object object)
91          throws NamingException {
92          Vector request = pack("bind", 3);
93          request.add(name);
94          request.add(className);
95          request.add(object);
96          synchronized (_connection) {
97              send(request);
98              checkReply("bind");
99          }
100     }
101 
102     public void rebind(Name name, String className, Object object)
103         throws NamingException {
104         Vector request = pack("rebind", 3);
105         request.add(name);
106         request.add(className);
107         request.add(object);
108         synchronized (_connection) {
109             send(request);
110             checkReply("rebind");
111         }
112     }
113 
114     public Context createSubcontext(Name name) throws NamingException {
115         Context subcontext;
116         Vector request = pack("createSubContext", 1);
117         request.add(name);
118         synchronized (_connection) {
119             send(request);
120             subcontext = (Context) checkReply("createSubContext");
121         }
122         return subcontext;
123     }
124 
125     public void destroySubcontext(Name name) throws NamingException {
126         Vector request = pack("destroySubContext", 1);
127         request.add(name);
128         synchronized (_connection) {
129             send(request);
130             checkReply("destroySubContext");
131         }
132     }
133 
134     public NameClassPair[] list(Name name) throws NamingException {
135         NameClassPair[] pairs;
136         Vector request = pack("list", 1);
137         request.add(name);
138         synchronized (_connection) {
139             send(request);
140             pairs = (NameClassPair[]) checkReply("list");
141         }
142         return pairs;
143     }
144 
145     public Binding[] listBindings(Name name) throws NamingException {
146         Binding[] bindings;
147         Vector request = pack("listBindings", 1);
148         request.add(name);
149         synchronized (_connection) {
150             send(request);
151             bindings = (Binding[]) checkReply("listBindings");
152         }
153         return bindings;
154     }
155 
156     public Object lookup(Name name) throws NamingException {
157         Object object;
158         Vector request = pack("lookup", 1);
159         request.add(name);
160         synchronized (_connection) {
161             send(request);
162             object = checkReply("lookup");
163         }
164         return object;
165     }
166 
167     public void unbind(Name name) throws NamingException {
168         Vector request = pack("unbind", 1);
169         request.add(name);
170         synchronized (_connection) {
171             send(request);
172             checkReply("unbind");
173         }
174     }
175 
176     /***
177      * Pack all the data that is required by the server in a vector.
178      * Set the size of the vector to be exactly the right size for efficiency.
179      *
180      * @param method The function to activate on the server.
181      * @param numParams The number of paramaters this method will require.
182      * @return Vector The vector containing all the data.
183      */
184     protected Vector pack(String method, int numParams) {
185         Vector request = new Vector(3 + numParams);
186         request.add(getClassName());
187         request.add(method);
188         request.add("fooey");
189         return request;
190     }
191 
192     /***
193      * Return the Server class name that will handle this request.
194      *
195      * @return The class name.
196      */
197     protected String getClassName() {
198         return "org.exolab.jms.jndi.mipc.IpcJmsJndiServer";
199     }
200 
201     /***
202      * A convenience method to check the success of operations which return
203      * a true on sucess.
204      *
205      * @param method The requested server function.
206      * @throws NamingException on any failure.
207      */
208     protected Object checkReply(String method) throws NamingException {
209         Object result = null;
210 
211         try {
212             Vector v = (Vector) _connection.receive();
213             if (v != null) {
214                 Boolean b = (Boolean) v.get(0);
215                 if (!b.booleanValue()) {
216                     NamingException error = (NamingException) v.get(1);
217                     throw error;
218                 } else {
219                     result = v.get(1);
220                 }
221             } else {
222                 throw new NamingException("Unknown connection error for "
223                     + method);
224             }
225         } catch (NamingException error) {
226             throw error;
227         } catch (Exception error) {
228             // rethrow as a NamingException
229             throw new NamingException("Operation " + method + " failed: "
230                 + error);
231         }
232         return result;
233     }
234 
235     /***
236      * A convenience method to send a packed command to the server.
237      *
238      * @throws NamingException on any failure.
239      */
240     protected void send(Vector v) throws NamingException {
241         try {
242             _connection.send(v);
243         } catch (Exception error) {
244             // rethrow as a NamingException
245             throw new NamingException("Operation failed: " + error);
246         }
247     }
248 
249 } //-- NameServiceProxy