1 22 package org.jnp.client; 23 24 import java.util.Properties ; 25 26 import javax.naming.Binding ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.naming.LinkRef ; 30 import javax.naming.Name ; 31 import javax.naming.NameClassPair ; 32 import javax.naming.NameNotFoundException ; 33 import javax.naming.NamingEnumeration ; 34 import javax.naming.NamingException ; 35 import javax.naming.Reference ; 36 import javax.naming.StringRefAddr ; 37 38 50 public class Main 51 implements Runnable 52 { 53 org.jnp.server.Main remoteServer; 54 55 61 public static void main(String [] args) 62 throws Exception 63 { 64 System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 65 System.setProperty("java.naming.factory.url.pkgs", "org.jnp.interfaces"); 66 System.setProperty("java.naming.provider.url", "localhost"); 67 System.setErr(System.out); 68 new Main().run(); 69 } 70 71 73 75 public void printName(String name) 76 throws NamingException 77 { 78 Context ctx = (Context )new InitialContext ().lookup(""); 79 Name n = ctx.getNameParser("").parse(name); 80 System.out.println("'"+name+"'.size = "+n.size()); 81 for (int i = 0; i < n.size(); i++) 82 System.out.println(" ["+i+"]"+n.get(i)); 83 } 84 85 91 public void showTree(Context ctx) 92 throws NamingException 93 { 94 showTree(ctx, Integer.MAX_VALUE); 95 } 96 public void showTree(Context ctx, int maxDepth) 97 throws NamingException 98 { 99 System.out.println("----------------------------"); 100 showTree("/", ctx, 0, maxDepth); 101 System.out.println("----------------------------"); 102 } 103 104 106 110 public void run() 111 { 112 try 113 { 114 printName("jnp://localhost/"); 115 printName("jnp://localhost:1099/"); 116 printName("jnp://localhost:1099/root"); 117 printName("jnp://localhost"); 118 printName("jnp:/localhost/"); 119 printName("jnp:localhost/"); 120 121 InitialContext iniCtx = new InitialContext (); 123 Context ctx = iniCtx; 124 125 Context java = (Context ) iniCtx.lookup("java:"); 127 System.out.println("java: "+java); 128 129 Context test = ctx.createSubcontext("test"); 131 System.out.println("test created:"+test); 132 133 Object hello1 = "Hello1"; 135 System.out.println(hello1); 136 Object hello2 = "Hello2"; 137 System.out.println(hello2); 138 139 ctx.bind("/test/server", hello1); 141 System.out.println("test/server bound"); 142 143 test.bind("server2", hello2); 145 System.out.println("test/server2 bound"); 146 147 Object server = ctx.lookup("test/server2"); 149 System.out.println("test/server2 lookup:"+server); 150 server = ctx.lookup("jnp://localhost/test/server2"); 151 System.out.println("jnp://localhost/test/server2 lookup:"+server); 152 153 test = (Context )ctx.lookup("test"); 155 Object server2 = test.lookup("server"); 156 System.out.println("test then server lookup:"+server2); 157 158 iniCtx.rebind("test/server2", hello2); 160 System.out.println("test/server2 rebound"); 161 162 showTree(ctx); 163 164 test.rename("/test/server2", "server3"); 166 System.out.println("test/server2 renamed to test/server3"); 167 168 try 170 { 171 test.lookup("server2"); 172 } catch (NameNotFoundException e) 173 { 174 System.out.println("Server2 was not found (which is OK)"); 175 } 176 177 Object server3 = test.lookup("server3"); 178 System.out.println("Server3:"+server3); 179 180 showTree(ctx); 182 183 ctx = (Context ) iniCtx.lookup("jnp://localhost/"); 185 System.out.println("Looked up URL context"); 186 187 showTree(ctx); 188 189 System.out.println("Looked up using URL: " +iniCtx.lookup("jnp://localhost:1099/test/server3")); 191 192 iniCtx.bind("jnp://localhost/helloserver",hello2); 194 System.out.println("Bound helloserver"); 195 196 iniCtx.rename("helloserver","jnp://localhost/test/helloserver"); 198 System.out.println("Renamed helloserver to test/helloserver"); 199 200 202 test.bind("/helloserver2",test.lookup("helloserver")); 203 System.out.println("Bound test/helloserver to /helloserver2"); 204 205 test.bind("/helloserver3", new LinkRef ("/test/server3")); 207 test.bind("helloserver4", new LinkRef ("server3")); 208 System.out.println("test/helloserver3="+ctx.lookup("helloserver3")); 209 210 ctx.createSubcontext("test2"); 212 ctx.bind("test2/helloworld", ctx.lookup("test/server3")); 213 test.bind("test2link", new LinkRef ("/test2")); 214 System.out.println("test2/helloworld="+ctx.lookup("test2/helloworld")); 215 System.out.println("test/test2link/helloworld="+ctx.lookup("test/test2link/helloworld")); 216 217 System.out.println(); 219 System.out.println("Show root bindings"); 220 ctx = iniCtx; 221 NamingEnumeration enumeration = ctx.listBindings(""); 222 while (enumeration.hasMoreElements()) 223 { 224 Binding b = (Binding )enumeration.next(); 225 System.out.println(b); 226 } 227 228 showTree(ctx); 229 230 StringRefAddr addr = new StringRefAddr ("URL", "file:/tmp"); 232 Reference fsRef = new Reference ("javax.naming.Context", addr); 233 ctx.bind("external", fsRef); 234 Context tmpfs = (Context ) ctx.lookup("external"); 235 System.out.println("+++ tmp filesystem context:"); 236 showTree(tmpfs, 2); 237 238 Properties env = new Properties (); 240 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 241 env.setProperty(Context.URL_PKG_PREFIXES, "org.jnp.interfaces"); 242 env.setProperty(Context.PROVIDER_URL, "jnp://localhost/test"); 243 System.out.println("+++ Test jnp URL passed as PROVIDER_URL"); 244 ctx = new InitialContext (env); 245 server = ctx.lookup("server"); 246 System.out.println("+ PROVIDER_URL=jnp://localhost/test lookup(server):"+server); 247 env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/test"); 248 ctx = new InitialContext (env); 249 server = ctx.lookup("server"); 250 System.out.println("+ PROVIDER_URL=jnp://localhost:1099/test lookup(server):"+server); 251 env.setProperty(Context.PROVIDER_URL, "jnp://localhost"); 252 ctx = new InitialContext (env); 253 server = ctx.lookup("test/server"); 254 System.out.println("+ PROVIDER_URL=jnp://localhost lookup(test/server):"+server); 255 env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/"); 256 ctx = new InitialContext (env); 257 server = ctx.lookup("test/server"); 258 System.out.println("+ PROVIDER_URL=jnp://localhost:1099/ lookup(test/server):"+server); 259 260 runRemoteServer(); 262 System.out.println("+++ Started second jnp server on port 10099"); 263 test = (Context ) iniCtx.lookup("test"); 264 showTree(test); 265 266 env = new Properties (); 267 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 268 env.setProperty(Context.URL_PKG_PREFIXES, "org.jnp.interfaces"); 269 ctx = (Context ) new InitialContext (env).lookup("jnp://localhost:10099/"); 270 System.out.println(ctx.getEnvironment()); 271 test = ctx.createSubcontext("test2"); 273 System.out.println("10099 test2 created:"+test); 274 System.out.println("10099 test2.env:"+test.getEnvironment()); 275 test.bind("external", new LinkRef ("jnp://localhost:1099/test")); 276 Context external = (Context ) new InitialContext (env).lookup("jnp://localhost:10099/test2/external"); 277 System.out.println("jnp://localhost:10099/test2 = "+external); 278 System.out.println("jnp://localhost:10099/test2.env = "+external.getEnvironment()); 279 remoteServer.stop(); 280 } 281 catch (Exception e) 282 { 283 e.printStackTrace(System.err); 284 } 285 } 286 287 private void runRemoteServer() throws Exception 288 { 289 remoteServer = new org.jnp.server.Main(); 290 remoteServer.setPort(10099); 291 remoteServer.start(); 292 } 293 294 301 private void showTree(String indent, Context ctx, int depth, int maxDepth) 302 throws NamingException 303 { 304 if( depth == maxDepth ) 305 return; 306 NamingEnumeration enumeration = ctx.list(""); 307 while (enumeration.hasMoreElements()) 308 { 309 NameClassPair ncp = (NameClassPair )enumeration.next(); 310 System.out.println(indent+ncp); 311 if (ncp.getClassName().indexOf("Context") != -1) 312 showTree(indent+ncp.getName()+"/", (Context )ctx.lookup(ncp.getName()), depth+1, maxDepth); 313 } 314 } 315 316 } 318 | Popular Tags |