KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jndi > WSIFJndiHelper


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package jndi;
59
60 import java.util.StringTokenizer JavaDoc;
61
62 import javax.naming.Context JavaDoc;
63 import javax.naming.InitialContext JavaDoc;
64 import javax.naming.NameAlreadyBoundException JavaDoc;
65 import javax.naming.NamingException JavaDoc;
66 import org.apache.wsif.naming.WSIFServiceRef;
67 import org.apache.wsif.naming.WSIFServiceStubRef;
68
69 /**
70  * A JNDI helper class for binding and unbinding
71  * services and stubs
72  *
73  * @author Owen Burroughs <owenb@apache.org>
74  **/

75
76 public class WSIFJndiHelper {
77     private static Context JavaDoc ctx;
78
79     /**
80      * Given the elements of a WSIFServiceRef, create and bind the
81      * service using JNDI under the specified name.
82      * @param wsdl The location of the wsdl file
83      * @param sNS The namespace for the service as specified in the wsdl
84      * @param sName The name of the service required, as specified in the wsdl
85      * @param ptNS The namespace of the port type required, as specified in the wsdl
86      * @param ptName The name of the port type required, as specified in the wsdl
87      * @param jndiName The full JNDI name under which the service will be bound
88      * @exception A NamingException thrown if an error occured when working with JNDI
89      */

90     public static void bindService(
91             String JavaDoc wsdl,
92             String JavaDoc sNS,
93             String JavaDoc sName,
94             String JavaDoc ptNS,
95             String JavaDoc ptName,
96             String JavaDoc jndiName)
97             throws NamingException JavaDoc {
98         recursiveBind(jndiName, new WSIFServiceRef(wsdl, sNS, sName, ptNS, ptName));
99     }
100
101     /**
102      * Given a WSIFServiceRef, create and bind the
103      * service using JNDI under the specified name.
104      * @param ref A WSIFServiceRef object reference for the service
105      * @param jndiName The full JNDI name under which the service will be bound
106      * @exception A NamingException thrown if an error occured when working with JNDI
107      */

108     public static void bindService(WSIFServiceRef ref, String JavaDoc jndiName)
109             throws NamingException JavaDoc {
110         recursiveBind(jndiName, ref);
111     }
112
113     /**
114      * Given the elements of a WSIFServiceStubRef, create and bind the
115      * stub using JNDI under the specified name.
116      * @param wsdl The location of the wsdl file
117      * @param sNS The namespace for the service as specified in the wsdl
118      * @param sName The name of the service required, as specified in the wsdl
119      * @param ptNS The namespace of the port type required, as specified in the wsdl
120      * @param ptName The name of the port type required, as specified in the wsdl
121      * @param portName The name of the preferred port to use
122      * @param cls The fully qualified name of the interface class for the stub
123      * @param jndiName The full JNDI name under which the service will be bound
124      * @exception A NamingException thrown if an error occured when working with JNDI
125      */

126     public static void bindStub(
127             String JavaDoc wsdl,
128             String JavaDoc sNS,
129             String JavaDoc sName,
130             String JavaDoc ptNS,
131             String JavaDoc ptName,
132             String JavaDoc portName,
133             String JavaDoc cls,
134             String JavaDoc jndiName)
135             throws NamingException JavaDoc {
136         recursiveBind(
137             jndiName,
138             new WSIFServiceStubRef(wsdl, sNS, sName, ptNS, ptName, portName, cls));
139     }
140
141     /**
142      * Given a WSIFServiceStubRef, create and bind the
143      * stub using JNDI under the specified name.
144      * @param ref A WSIFServiceRef object reference for the service
145      * @param jndiName The full JNDI name under which the service will be bound
146      * @exception A NamingException thrown if an error occured when working with JNDI
147      */

148     public static void bindStub(WSIFServiceStubRef ref, String JavaDoc jndiName)
149             throws NamingException JavaDoc {
150         recursiveBind(jndiName, ref);
151     }
152
153     /**
154      * A helper method to unbind a service or stub using JNDI. Any subcontexts left
155      * empty by the unbinding will be destoyed.
156      * @param name The full JNDI name of service or stub to be unbound.
157      * @exception A NamingException thrown if an error occured when working with JNDI
158      */

159     public static void unbindServiceOrStub(String JavaDoc name) throws NamingException JavaDoc {
160         recursiveUnbind(name);
161     }
162
163     /**
164      * A helper method to recursively bind an object using JNDI. Any subcontexts
165      * required that do not currently exist will be created.
166      * @param name The full JNDI name under which the object will be bound.
167      * @param obj The object to bind
168      * @exception A NamingException thrown if an error occured when working with JNDI
169      */

170     public static void recursiveBind(String JavaDoc name, Object JavaDoc obj)
171             throws NamingException JavaDoc {
172         String JavaDoc[] tokens = parseString(name);
173         Context JavaDoc startingContext = getInitialContext();
174         for (int i = 0; i < tokens.length - 1; i++) {
175             try {
176                 startingContext.createSubcontext(tokens[i]);
177             } catch (NameAlreadyBoundException JavaDoc nab) {
178             }
179         }
180         startingContext.bind(name, obj);
181     }
182
183     /**
184      * A helper method to unbind an object using JNDI. Any subcontexts left
185      * empty by the unbinding will be destoyed.
186      * @param name The full JNDI name of object to be unbound.
187      * @exception A NamingException thrown if an error occured when working with JNDI
188      */

189     public static void recursiveUnbind(String JavaDoc name) throws NamingException JavaDoc {
190         String JavaDoc[] tokens = parseString(name);
191         Context JavaDoc startingContext = getInitialContext();
192         startingContext.unbind(name);
193
194         for (int i = tokens.length - 2; i >= 0; i--) {
195             startingContext.destroySubcontext(tokens[i]);
196         }
197     }
198
199     private static String JavaDoc[] parseString(String JavaDoc s) {
200         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, "/");
201         int i = st.countTokens();
202         int j = 0;
203         String JavaDoc[] tokens = new String JavaDoc[i];
204         while (st.hasMoreTokens()) {
205             if (j > 0) {
206                 tokens[j] = tokens[j - 1] + "/" + st.nextToken();
207             } else {
208                 tokens[j] = st.nextToken();
209             }
210             j++;
211         }
212         return tokens;
213     }
214
215     /**
216      * Gets the current InitialContext
217      * @return Returns a Context
218      */

219     public static Context JavaDoc getInitialContext() throws NamingException JavaDoc {
220         if (ctx == null)
221             ctx = new InitialContext JavaDoc();
222         return ctx;
223     }
224
225     /**
226      * Sets the InitialContext to be used
227      * @param ctx The InitialContext
228      */

229     public static void setInitialContext(Context JavaDoc c) {
230         ctx = c;
231     }
232 }
Popular Tags