001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package jndi;
059:
060: import java.util.StringTokenizer;
061:
062: import javax.naming.Context;
063: import javax.naming.InitialContext;
064: import javax.naming.NameAlreadyBoundException;
065: import javax.naming.NamingException;
066: import org.apache.wsif.naming.WSIFServiceRef;
067: import org.apache.wsif.naming.WSIFServiceStubRef;
068:
069: /**
070: * A JNDI helper class for binding and unbinding
071: * services and stubs
072: *
073: * @author Owen Burroughs <owenb@apache.org>
074: **/
075:
076: public class WSIFJndiHelper {
077: private static Context ctx;
078:
079: /**
080: * Given the elements of a WSIFServiceRef, create and bind the
081: * service using JNDI under the specified name.
082: * @param wsdl The location of the wsdl file
083: * @param sNS The namespace for the service as specified in the wsdl
084: * @param sName The name of the service required, as specified in the wsdl
085: * @param ptNS The namespace of the port type required, as specified in the wsdl
086: * @param ptName The name of the port type required, as specified in the wsdl
087: * @param jndiName The full JNDI name under which the service will be bound
088: * @exception A NamingException thrown if an error occured when working with JNDI
089: */
090: public static void bindService(String wsdl, String sNS,
091: String sName, String ptNS, String ptName, String jndiName)
092: throws NamingException {
093: recursiveBind(jndiName, new WSIFServiceRef(wsdl, sNS, sName,
094: ptNS, ptName));
095: }
096:
097: /**
098: * Given a WSIFServiceRef, create and bind the
099: * service using JNDI under the specified name.
100: * @param ref A WSIFServiceRef object reference for the service
101: * @param jndiName The full JNDI name under which the service will be bound
102: * @exception A NamingException thrown if an error occured when working with JNDI
103: */
104: public static void bindService(WSIFServiceRef ref, String jndiName)
105: throws NamingException {
106: recursiveBind(jndiName, ref);
107: }
108:
109: /**
110: * Given the elements of a WSIFServiceStubRef, create and bind the
111: * stub using JNDI under the specified name.
112: * @param wsdl The location of the wsdl file
113: * @param sNS The namespace for the service as specified in the wsdl
114: * @param sName The name of the service required, as specified in the wsdl
115: * @param ptNS The namespace of the port type required, as specified in the wsdl
116: * @param ptName The name of the port type required, as specified in the wsdl
117: * @param portName The name of the preferred port to use
118: * @param cls The fully qualified name of the interface class for the stub
119: * @param jndiName The full JNDI name under which the service will be bound
120: * @exception A NamingException thrown if an error occured when working with JNDI
121: */
122: public static void bindStub(String wsdl, String sNS, String sName,
123: String ptNS, String ptName, String portName, String cls,
124: String jndiName) throws NamingException {
125: recursiveBind(jndiName, new WSIFServiceStubRef(wsdl, sNS,
126: sName, ptNS, ptName, portName, cls));
127: }
128:
129: /**
130: * Given a WSIFServiceStubRef, create and bind the
131: * stub using JNDI under the specified name.
132: * @param ref A WSIFServiceRef object reference for the service
133: * @param jndiName The full JNDI name under which the service will be bound
134: * @exception A NamingException thrown if an error occured when working with JNDI
135: */
136: public static void bindStub(WSIFServiceStubRef ref, String jndiName)
137: throws NamingException {
138: recursiveBind(jndiName, ref);
139: }
140:
141: /**
142: * A helper method to unbind a service or stub using JNDI. Any subcontexts left
143: * empty by the unbinding will be destoyed.
144: * @param name The full JNDI name of service or stub to be unbound.
145: * @exception A NamingException thrown if an error occured when working with JNDI
146: */
147: public static void unbindServiceOrStub(String name)
148: throws NamingException {
149: recursiveUnbind(name);
150: }
151:
152: /**
153: * A helper method to recursively bind an object using JNDI. Any subcontexts
154: * required that do not currently exist will be created.
155: * @param name The full JNDI name under which the object will be bound.
156: * @param obj The object to bind
157: * @exception A NamingException thrown if an error occured when working with JNDI
158: */
159: public static void recursiveBind(String name, Object obj)
160: throws NamingException {
161: String[] tokens = parseString(name);
162: Context startingContext = getInitialContext();
163: for (int i = 0; i < tokens.length - 1; i++) {
164: try {
165: startingContext.createSubcontext(tokens[i]);
166: } catch (NameAlreadyBoundException nab) {
167: }
168: }
169: startingContext.bind(name, obj);
170: }
171:
172: /**
173: * A helper method to unbind an object using JNDI. Any subcontexts left
174: * empty by the unbinding will be destoyed.
175: * @param name The full JNDI name of object to be unbound.
176: * @exception A NamingException thrown if an error occured when working with JNDI
177: */
178: public static void recursiveUnbind(String name)
179: throws NamingException {
180: String[] tokens = parseString(name);
181: Context startingContext = getInitialContext();
182: startingContext.unbind(name);
183:
184: for (int i = tokens.length - 2; i >= 0; i--) {
185: startingContext.destroySubcontext(tokens[i]);
186: }
187: }
188:
189: private static String[] parseString(String s) {
190: StringTokenizer st = new StringTokenizer(s, "/");
191: int i = st.countTokens();
192: int j = 0;
193: String[] tokens = new String[i];
194: while (st.hasMoreTokens()) {
195: if (j > 0) {
196: tokens[j] = tokens[j - 1] + "/" + st.nextToken();
197: } else {
198: tokens[j] = st.nextToken();
199: }
200: j++;
201: }
202: return tokens;
203: }
204:
205: /**
206: * Gets the current InitialContext
207: * @return Returns a Context
208: */
209: public static Context getInitialContext() throws NamingException {
210: if (ctx == null)
211: ctx = new InitialContext();
212: return ctx;
213: }
214:
215: /**
216: * Sets the InitialContext to be used
217: * @param ctx The InitialContext
218: */
219: public static void setInitialContext(Context c) {
220: ctx = c;
221: }
222: }
|