KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jnp > client > Main


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jnp.client;
23
24 import java.util.Properties JavaDoc;
25
26 import javax.naming.Binding JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.LinkRef JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.NameClassPair JavaDoc;
32 import javax.naming.NameNotFoundException JavaDoc;
33 import javax.naming.NamingEnumeration JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.Reference JavaDoc;
36 import javax.naming.StringRefAddr JavaDoc;
37
38 /**
39  * This is a test client of the NamingServer. It calls the server
40  * in various ways to test the functionality.
41  *
42  * <p>The JNDI configuration is provided in the jndi.properties file.
43  *
44  * @see org.jnp.interfaces.NamingContext
45  *
46  * @author oberg
47  * @author Scott_Stark@displayscape.com
48  * @version $Revision: 37459 $
49  */

50 public class Main
51    implements Runnable JavaDoc
52 {
53     org.jnp.server.Main remoteServer;
54
55    /**
56     * Start the test
57     *
58     * @param args
59     * @exception Exception
60     */

61     public static void main(String JavaDoc[] args)
62       throws Exception JavaDoc
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    // Constructors --------------------------------------------------
72

73    // Public --------------------------------------------------------
74

75    public void printName(String JavaDoc name)
76       throws NamingException JavaDoc
77    {
78       Context JavaDoc ctx = (Context JavaDoc)new InitialContext JavaDoc().lookup("");
79       Name JavaDoc 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    /**
86     * Show a JNDI context tree on system out.
87     *
88     * @param ctx
89     * @exception NamingException
90     */

91    public void showTree(Context JavaDoc ctx)
92       throws NamingException JavaDoc
93    {
94       showTree(ctx, Integer.MAX_VALUE);
95    }
96    public void showTree(Context JavaDoc ctx, int maxDepth)
97       throws NamingException JavaDoc
98    {
99       System.out.println("----------------------------");
100       showTree("/", ctx, 0, maxDepth);
101       System.out.println("----------------------------");
102    }
103
104    // Runnable implementation ---------------------------------------
105

106    /**
107     * Run the tests
108     *
109     */

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          // Locate naming service (environment/setup is provided through the jndi.properties file)
122
InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
123          Context JavaDoc ctx = iniCtx;
124
125          // Lookup the java: context
126
Context JavaDoc java = (Context JavaDoc) iniCtx.lookup("java:");
127          System.out.println("java: "+java);
128
129          // Create subcontext
130
Context JavaDoc test = ctx.createSubcontext("test");
131          System.out.println("test created:"+test);
132          
133          // Create objects
134
Object JavaDoc hello1 = "Hello1";
135          System.out.println(hello1);
136          Object JavaDoc hello2 = "Hello2";
137          System.out.println(hello2);
138          
139          // Bind object
140
ctx.bind("/test/server", hello1);
141          System.out.println("test/server bound");
142          
143          // Bind object
144
test.bind("server2", hello2);
145          System.out.println("test/server2 bound");
146
147          // Lookup object
148
Object JavaDoc 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          // Lookup object
154
test = (Context JavaDoc)ctx.lookup("test");
155          Object JavaDoc server2 = test.lookup("server");
156          System.out.println("test then server lookup:"+server2);
157
158          // Rebind object
159
iniCtx.rebind("test/server2", hello2);
160          System.out.println("test/server2 rebound");
161
162          showTree(ctx);
163          
164          // Rename object using absolute and relative names
165
test.rename("/test/server2", "server3");
166          System.out.println("test/server2 renamed to test/server3");
167
168          // Lookup object
169
try
170          {
171             test.lookup("server2");
172          } catch (NameNotFoundException JavaDoc e)
173          {
174             System.out.println("Server2 was not found (which is OK)");
175          }
176          
177          Object JavaDoc server3 = test.lookup("server3");
178          System.out.println("Server3:"+server3);
179
180          // Print tree
181
showTree(ctx);
182          
183          // Try URL context factory
184
ctx = (Context JavaDoc) iniCtx.lookup("jnp://localhost/");
185          System.out.println("Looked up URL context");
186          
187          showTree(ctx);
188
189          // Try complete URL
190
System.out.println("Looked up using URL: " +iniCtx.lookup("jnp://localhost:1099/test/server3"));
191
192          // Bind using complete URL
193
iniCtx.bind("jnp://localhost/helloserver",hello2);
194          System.out.println("Bound helloserver");
195          
196          // Rename using URL
197
iniCtx.rename("helloserver","jnp://localhost/test/helloserver");
198          System.out.println("Renamed helloserver to test/helloserver");
199          
200          // Bind to root using absolute and relative names
201

202          test.bind("/helloserver2",test.lookup("helloserver"));
203          System.out.println("Bound test/helloserver to /helloserver2");
204             
205          // Create LinkRef
206
test.bind("/helloserver3", new LinkRef JavaDoc("/test/server3"));
207          test.bind("helloserver4", new LinkRef JavaDoc("server3"));
208          System.out.println("test/helloserver3="+ctx.lookup("helloserver3"));
209          
210          // Create LinkRef to context
211
ctx.createSubcontext("test2");
212          ctx.bind("test2/helloworld", ctx.lookup("test/server3"));
213          test.bind("test2link", new LinkRef JavaDoc("/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          // Show root context using listBindings
218
System.out.println();
219          System.out.println("Show root bindings");
220          ctx = iniCtx;
221          NamingEnumeration JavaDoc enumeration = ctx.listBindings("");
222          while (enumeration.hasMoreElements())
223          {
224             Binding JavaDoc b = (Binding JavaDoc)enumeration.next();
225             System.out.println(b);
226          }
227          
228          showTree(ctx);
229
230          // Test a URL Reference to a filesystem context
231
StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("URL", "file:/tmp");
232          Reference JavaDoc fsRef = new Reference JavaDoc("javax.naming.Context", addr);
233          ctx.bind("external", fsRef);
234          Context JavaDoc tmpfs = (Context JavaDoc) ctx.lookup("external");
235          System.out.println("+++ tmp filesystem context:");
236          showTree(tmpfs, 2);
237
238          // Create an initial context that is rooted at /test
239
Properties JavaDoc env = new Properties JavaDoc();
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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(env);
257          server = ctx.lookup("test/server");
258          System.out.println("+ PROVIDER_URL=jnp://localhost:1099/ lookup(test/server):"+server);
259
260          // Test accessing a remote by accessing a non-default local server
261
runRemoteServer();
262          System.out.println("+++ Started second jnp server on port 10099");
263          test = (Context JavaDoc) iniCtx.lookup("test");
264          showTree(test);
265
266          env = new Properties JavaDoc();
267          env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
268          env.setProperty(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
269          ctx = (Context JavaDoc) new InitialContext JavaDoc(env).lookup("jnp://localhost:10099/");
270          System.out.println(ctx.getEnvironment());
271          // Create subcontext
272
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 JavaDoc("jnp://localhost:1099/test"));
276          Context JavaDoc external = (Context JavaDoc) new InitialContext JavaDoc(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 JavaDoc e)
282       {
283          e.printStackTrace(System.err);
284       }
285    }
286
287    private void runRemoteServer() throws Exception JavaDoc
288    {
289        remoteServer = new org.jnp.server.Main();
290        remoteServer.setPort(10099);
291        remoteServer.start();
292    }
293
294     /**
295     * Print the contents of a JNDI context recursively
296     *
297     * @param indent indentation string
298     * @param ctx the JNDI context
299     * @exception NamingException thrown if any problems occur
300     */

301     private void showTree(String JavaDoc indent, Context JavaDoc ctx, int depth, int maxDepth)
302         throws NamingException JavaDoc
303     {
304         if( depth == maxDepth )
305             return;
306         NamingEnumeration JavaDoc enumeration = ctx.list("");
307         while (enumeration.hasMoreElements())
308         {
309             NameClassPair JavaDoc ncp = (NameClassPair JavaDoc)enumeration.next();
310             System.out.println(indent+ncp);
311             if (ncp.getClassName().indexOf("Context") != -1)
312                showTree(indent+ncp.getName()+"/", (Context JavaDoc)ctx.lookup(ncp.getName()), depth+1, maxDepth);
313         }
314     }
315
316    // Inner classes -------------------------------------------------
317
}
318
Popular Tags