001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jndi;
009:
010: import java.io.BufferedInputStream;
011: import java.io.BufferedReader;
012: import java.io.InputStreamReader;
013: import java.io.Serializable;
014: import java.rmi.RemoteException;
015: import javax.naming.Context;
016: import javax.naming.InitialContext;
017: import javax.naming.Reference;
018: import javax.naming.StringRefAddr;
019: import javax.naming.NamingEnumeration;
020:
021: /**
022: *
023: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
024: */
025:
026: public class Client {
027:
028: public static void main(String[] args) throws Exception {
029: test();
030: }
031:
032: public static void referenceTest() throws Exception {
033: System.setProperty("java.naming.factory.initial",
034: JNDIProperties.INITIAL_CONTEXT_FACTORY);
035: System.setProperty(Context.OBJECT_FACTORIES,
036: JNDIProperties.INITIAL_CONTEXT_FACTORY);
037: System.setProperty(Context.URL_PKG_PREFIXES,
038: JNDIProperties.URL_PKG_PREFIXES);
039: System.setProperty("java.naming.provider.url",
040: JNDIProperties.PROVIDER_URL);
041:
042: Context ctx = new InitialContext();
043:
044: StringRefAddr addr = new StringRefAddr("URL", "file:/tmp");
045: Reference fsRef = new Reference("java.io.BufferedInputStream",
046: addr);
047: ctx.bind("external", fsRef);
048: // fsRef.getClassName()
049: BufferedInputStream is = (BufferedInputStream) ctx
050: .lookup("external");
051: BufferedReader in = new BufferedReader(
052: new InputStreamReader(is));
053: System.out.println(in.readLine());
054: }
055:
056: public static void test() throws Exception {
057: System.setProperty("java.naming.factory.initial",
058: JNDIProperties.INITIAL_CONTEXT_FACTORY);
059: System.setProperty("java.naming.factory.url.pkgs",
060: "org.huihoo.jfox.jndi");
061: System.setProperty("java.naming.provider.url",
062: JNDIProperties.PROVIDER_URL);
063:
064: System.out.println("new InitialContext");
065: Context ctx = new InitialContext();
066:
067: System.out.println("createSubcontext: yyy/zzz");
068: ctx.createSubcontext("yyy/zzz");
069:
070: System.out.println("createSubcontext: yyy/zzz/aaa");
071: ctx.createSubcontext("yyy/zzz/aaa");
072:
073: System.out.println("destroySubcontext: yyy/zzz/aaa");
074: ctx.destroySubcontext("yyy/zzz/aaa");
075:
076: System.out.println("rebind: yyy/zzz/ccc");
077: ctx.rebind("/yyy/zzz/ccc", new Test());
078:
079: System.out.println("lookup: yyy/zzz/ccc");
080: Object ref = ctx.lookup("yyy/zzz/ccc");
081:
082: System.out.println("Invoke get method: " + ref);
083: // Test tc = (Test)(ref);
084: Test tc = (Test) javax.rmi.PortableRemoteObject.narrow(ref,
085: Test.class);
086: System.out.println(tc.get());
087:
088: listBindings(ctx, "yyy/zzz");
089:
090: }
091:
092: private static void listBindings(Context ctx, String name)
093: throws Exception {
094: System.out.println("ListBindings: " + name);
095: NamingEnumeration enu = ctx.listBindings(name);
096: while (enu.hasMore()) {
097: System.out.println(enu.next());
098: }
099: }
100: }
101:
102: class Test implements Serializable {
103:
104: /*
105: public TestClass() throws RemoteException {
106: PortableRemoteObject.exportObject(this);
107: }
108: */
109:
110: public String get() throws RemoteException {
111: return "Hello,World!";
112: }
113: }
|