001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jnp.client;
023:
024: /*
025: * Copyright 1999 by dreamBean Software,
026: * All rights reserved.
027: */
028:
029: import java.util.Properties;
030:
031: import javax.naming.Binding;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.LinkRef;
035: import javax.naming.Name;
036: import javax.naming.NameClassPair;
037: import javax.naming.NameNotFoundException;
038: import javax.naming.NamingEnumeration;
039: import javax.naming.NamingException;
040: import javax.naming.Reference;
041: import javax.naming.StringRefAddr;
042:
043: /**
044: * This is a test client of the NamingServer. It calls the server
045: * in various ways to test the functionality.
046: *
047: * <p>The JNDI configuration is provided in the jndi.properties file.
048: *
049: * @see org.jnp.interfaces.NamingContext
050: *
051: * @author oberg
052: * @author Scott_Stark@displayscape.com
053: * @version $Revision: 57199 $
054: */
055: public class Main implements Runnable {
056: org.jnp.server.Main remoteServer;
057:
058: /**
059: * Start the test
060: *
061: * @param args
062: * @exception Exception
063: */
064: public static void main(String[] args) throws Exception {
065: System.setProperty("java.naming.factory.initial",
066: "org.jnp.interfaces.NamingContextFactory");
067: System.setProperty("java.naming.factory.url.pkgs",
068: "org.jnp.interfaces");
069: System.setProperty("java.naming.provider.url", "localhost");
070: System.setErr(System.out);
071: new Main().run();
072: }
073:
074: // Constructors --------------------------------------------------
075:
076: // Public --------------------------------------------------------
077:
078: public void printName(String name) throws NamingException {
079: Context ctx = (Context) new InitialContext().lookup("");
080: Name n = ctx.getNameParser("").parse(name);
081: System.out.println("'" + name + "'.size = " + n.size());
082: for (int i = 0; i < n.size(); i++)
083: System.out.println(" [" + i + "]" + n.get(i));
084: }
085:
086: /**
087: * Show a JNDI context tree on system out.
088: *
089: * @param ctx
090: * @exception NamingException
091: */
092: public void showTree(Context ctx) throws NamingException {
093: showTree(ctx, Integer.MAX_VALUE);
094: }
095:
096: public void showTree(Context ctx, int maxDepth)
097: throws NamingException {
098: System.out.println("----------------------------");
099: showTree("/", ctx, 0, maxDepth);
100: System.out.println("----------------------------");
101: }
102:
103: // Runnable implementation ---------------------------------------
104:
105: /**
106: * Run the tests
107: *
108: */
109: public void run() {
110: try {
111: printName("jnp://localhost/");
112: printName("jnp://localhost:1099/");
113: printName("jnp://localhost:1099/root");
114: printName("jnp://localhost");
115: printName("jnp:/localhost/");
116: printName("jnp:localhost/");
117:
118: // Locate naming service (environment/setup is provided through the jndi.properties file)
119: InitialContext iniCtx = new InitialContext();
120: Context ctx = iniCtx;
121:
122: // Lookup the java: context
123: Context java = (Context) iniCtx.lookup("java:");
124: System.out.println("java: " + java);
125:
126: // Create subcontext
127: Context test = ctx.createSubcontext("test");
128: System.out.println("test created:" + test);
129:
130: // Create objects
131: Object hello1 = "Hello1";
132: System.out.println(hello1);
133: Object hello2 = "Hello2";
134: System.out.println(hello2);
135:
136: // Bind object
137: ctx.bind("/test/server", hello1);
138: System.out.println("test/server bound");
139:
140: // Bind object
141: test.bind("server2", hello2);
142: System.out.println("test/server2 bound");
143:
144: // Lookup object
145: Object server = ctx.lookup("test/server2");
146: System.out.println("test/server2 lookup:" + server);
147: server = ctx.lookup("jnp://localhost/test/server2");
148: System.out.println("jnp://localhost/test/server2 lookup:"
149: + server);
150:
151: // Lookup object
152: test = (Context) ctx.lookup("test");
153: Object server2 = test.lookup("server");
154: System.out.println("test then server lookup:" + server2);
155:
156: // Rebind object
157: iniCtx.rebind("test/server2", hello2);
158: System.out.println("test/server2 rebound");
159:
160: showTree(ctx);
161:
162: // Rename object using absolute and relative names
163: test.rename("/test/server2", "server3");
164: System.out.println("test/server2 renamed to test/server3");
165:
166: // Lookup object
167: try {
168: test.lookup("server2");
169: } catch (NameNotFoundException e) {
170: System.out
171: .println("Server2 was not found (which is OK)");
172: }
173:
174: Object server3 = test.lookup("server3");
175: System.out.println("Server3:" + server3);
176:
177: // Print tree
178: showTree(ctx);
179:
180: // Try URL context factory
181: ctx = (Context) iniCtx.lookup("jnp://localhost/");
182: System.out.println("Looked up URL context");
183:
184: showTree(ctx);
185:
186: // Try complete URL
187: System.out
188: .println("Looked up using URL: "
189: + iniCtx
190: .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",
198: "jnp://localhost/test/helloserver");
199: System.out
200: .println("Renamed helloserver to test/helloserver");
201:
202: // Bind to root using absolute and relative names
203:
204: test.bind("/helloserver2", test.lookup("helloserver"));
205: System.out
206: .println("Bound test/helloserver to /helloserver2");
207:
208: // Create LinkRef
209: test.bind("/helloserver3", new LinkRef("/test/server3"));
210: test.bind("helloserver4", new LinkRef("server3"));
211: System.out.println("test/helloserver3="
212: + ctx.lookup("helloserver3"));
213:
214: // Create LinkRef to context
215: ctx.createSubcontext("test2");
216: ctx.bind("test2/helloworld", ctx.lookup("test/server3"));
217: test.bind("test2link", new LinkRef("/test2"));
218: System.out.println("test2/helloworld="
219: + ctx.lookup("test2/helloworld"));
220: System.out.println("test/test2link/helloworld="
221: + ctx.lookup("test/test2link/helloworld"));
222:
223: // Show root context using listBindings
224: System.out.println();
225: System.out.println("Show root bindings");
226: ctx = iniCtx;
227: NamingEnumeration e = ctx.listBindings("");
228: while (e.hasMoreElements()) {
229: Binding b = (Binding) e.next();
230: System.out.println(b);
231: }
232:
233: showTree(ctx);
234:
235: // Test a URL Reference to a filesystem context
236: StringRefAddr addr = new StringRefAddr("URL", "file:/tmp");
237: Reference fsRef = new Reference("javax.naming.Context",
238: addr);
239: ctx.bind("external", fsRef);
240: Context tmpfs = (Context) ctx.lookup("external");
241: System.out.println("+++ tmp filesystem context:");
242: showTree(tmpfs, 2);
243:
244: // Create an initial context that is rooted at /test
245: Properties env = new Properties();
246: env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
247: "org.jnp.interfaces.NamingContextFactory");
248: env.setProperty(Context.URL_PKG_PREFIXES,
249: "org.jnp.interfaces");
250: env.setProperty(Context.PROVIDER_URL,
251: "jnp://localhost/test");
252: System.out
253: .println("+++ Test jnp URL passed as PROVIDER_URL");
254: ctx = new InitialContext(env);
255: server = ctx.lookup("server");
256: System.out
257: .println("+ PROVIDER_URL=jnp://localhost/test lookup(server):"
258: + server);
259: env.setProperty(Context.PROVIDER_URL,
260: "jnp://localhost:1099/test");
261: ctx = new InitialContext(env);
262: server = ctx.lookup("server");
263: System.out
264: .println("+ PROVIDER_URL=jnp://localhost:1099/test lookup(server):"
265: + server);
266: env.setProperty(Context.PROVIDER_URL, "jnp://localhost");
267: ctx = new InitialContext(env);
268: server = ctx.lookup("test/server");
269: System.out
270: .println("+ PROVIDER_URL=jnp://localhost lookup(test/server):"
271: + server);
272: env.setProperty(Context.PROVIDER_URL,
273: "jnp://localhost:1099/");
274: ctx = new InitialContext(env);
275: server = ctx.lookup("test/server");
276: System.out
277: .println("+ PROVIDER_URL=jnp://localhost:1099/ lookup(test/server):"
278: + server);
279:
280: // Test accessing a remote by accessing a non-default local server
281: runRemoteServer();
282: System.out
283: .println("+++ Started second jnp server on port 10099");
284: test = (Context) iniCtx.lookup("test");
285: showTree(test);
286:
287: env = new Properties();
288: env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
289: "org.jnp.interfaces.NamingContextFactory");
290: env.setProperty(Context.URL_PKG_PREFIXES,
291: "org.jnp.interfaces");
292: ctx = (Context) new InitialContext(env)
293: .lookup("jnp://localhost:10099/");
294: System.out.println(ctx.getEnvironment());
295: // Create subcontext
296: test = ctx.createSubcontext("test2");
297: System.out.println("10099 test2 created:" + test);
298: System.out.println("10099 test2.env:"
299: + test.getEnvironment());
300: test.bind("external", new LinkRef(
301: "jnp://localhost:1099/test"));
302: Context external = (Context) new InitialContext(env)
303: .lookup("jnp://localhost:10099/test2/external");
304: System.out.println("jnp://localhost:10099/test2 = "
305: + external);
306: System.out.println("jnp://localhost:10099/test2.env = "
307: + external.getEnvironment());
308: remoteServer.stop();
309: } catch (Exception e) {
310: e.printStackTrace(System.err);
311: }
312: }
313:
314: private void runRemoteServer() throws Exception {
315: remoteServer = new org.jnp.server.Main();
316: remoteServer.setPort(10099);
317: remoteServer.start();
318: }
319:
320: /**
321: * Print the contents of a JNDI context recursively
322: *
323: * @param indent indentation string
324: * @param ctx the JNDI context
325: * @exception NamingException thrown if any problems occur
326: */
327: private void showTree(String indent, Context ctx, int depth,
328: int maxDepth) throws NamingException {
329: if (depth == maxDepth)
330: return;
331: NamingEnumeration e = ctx.list("");
332: while (e.hasMoreElements()) {
333: NameClassPair ncp = (NameClassPair) e.next();
334: System.out.println(indent + ncp);
335: if (ncp.getClassName().indexOf("Context") != -1)
336: showTree(indent + ncp.getName() + "/", (Context) ctx
337: .lookup(ncp.getName()), depth + 1, maxDepth);
338: }
339: }
340:
341: // Inner classes -------------------------------------------------
342: }
|