1 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 75 76 public class WSIFJndiHelper { 77 private static Context ctx; 78 79 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 108 public static void bindService(WSIFServiceRef ref, String jndiName) 109 throws NamingException { 110 recursiveBind(jndiName, ref); 111 } 112 113 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 148 public static void bindStub(WSIFServiceStubRef ref, String jndiName) 149 throws NamingException { 150 recursiveBind(jndiName, ref); 151 } 152 153 159 public static void unbindServiceOrStub(String name) throws NamingException { 160 recursiveUnbind(name); 161 } 162 163 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 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 219 public static Context getInitialContext() throws NamingException { 220 if (ctx == null) 221 ctx = new InitialContext (); 222 return ctx; 223 } 224 225 229 public static void setInitialContext(Context c) { 230 ctx = c; 231 } 232 } | Popular Tags |