Source code: jndi/WSIFJndiHelper.java
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;
61
62 import javax.naming.Context;
63 import javax.naming.InitialContext;
64 import javax.naming.NameAlreadyBoundException;
65 import javax.naming.NamingException;
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 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 wsdl,
92 String sNS,
93 String sName,
94 String ptNS,
95 String ptName,
96 String jndiName)
97 throws NamingException {
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 jndiName)
109 throws NamingException {
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 wsdl,
128 String sNS,
129 String sName,
130 String ptNS,
131 String ptName,
132 String portName,
133 String cls,
134 String jndiName)
135 throws NamingException {
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 jndiName)
149 throws NamingException {
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 name) throws NamingException {
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 name, Object obj)
171 throws NamingException {
172 String[] tokens = parseString(name);
173 Context startingContext = getInitialContext();
174 for (int i = 0; i < tokens.length - 1; i++) {
175 try {
176 startingContext.createSubcontext(tokens[i]);
177 } catch (NameAlreadyBoundException 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 name) throws NamingException {
190 String[] tokens = parseString(name);
191 Context 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[] parseString(String s) {
200 StringTokenizer st = new StringTokenizer(s, "/");
201 int i = st.countTokens();
202 int j = 0;
203 String[] tokens = new String[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 getInitialContext() throws NamingException {
220 if (ctx == null)
221 ctx = new InitialContext();
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 c) {
230 ctx = c;
231 }
232 }