Oracle8i Enterprise JavaBeans and CORBA Developer's Guide Release 8.1.5 A64683-01 |
|
Lister demonstrates (1) Using Service Context createSubcontext() method to create a new session. (2) Authentication using the session context login() method. (3) Recursively listing the instance published object tree. Source files ============ Lister.java ----------- Invoke the Lister client program from the command line by doing: % Lister serviceURL username password where the serviceURL is a session IIOP service, such as sess_iiop://<hostname>:<dispatcher_port> for example: % Lister sess_iiop://localhost:2222 scott tiger The lister client first gets a JNDI Initial Context object, ic. Note that environment passed to the InitialContext() method has only the Context.URL_PKG_PREFIXES value ("oracle.aurora.jndi"), and not the username, password, and authentication type, as do many of the other examples. This is because Lister will authenticate by getting a server login object, after first instantiating a new session. The next call in Lister is look up the service on the Context object, passing in the service identifier string. Once the service is obtained, a new named session is created. Note that the session name must start with a colon (:). The session context is then used to activate the login server at the standard published location /etc/login. This server object is preconfigured for you when the database is built. If it is not there, see your DBA or system administrator. The Lister client then creates a login client, and invokes its authenticate() method, passing in the username and password (with a null role). The client then uses the SessionCtx object to walk the published object directory hierarchy, starting from the root ("/"). The name of the file, its creation date, and the file owner are printed as each object is encountered. This example could be expanded to list other attributes of each published object, such as the access permissions. See the Session Shell examples for a complete listing (ls -l) of all published object attributes and associated files. Compiling and Running the Example ================================= On UNIX, enter the command 'make' in the shell to compile and run the Lister client program. On Windows NT, use the batch file to compile and run.
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingEnumeration; import javax.naming.Binding; import javax.naming.NamingException; import javax.naming.CommunicationException; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.jndi.sess_iiop.SessionCtx; import oracle.aurora.jndi.sess_iiop.ActivationException; import oracle.aurora.AuroraServices.PublishedObject; import oracle.aurora.AuroraServices.objAttribsHolder; import oracle.aurora.AuroraServices.objAttribs; import oracle.aurora.AuroraServices.ctxAttribs; import oracle.aurora.jts.client.AuroraTransactionService; import oracle.aurora.AuroraServices.LoginServer; import oracle.aurora.client.Login; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; import java.util.Hashtable; public class Lister { public static void main (String[] args) throws Exception { if (args.length != 3) { System.out.println("usage: Lister serviceURL user password"); System.exit(1); } String serviceURL = args [0]; String username = args [1]; String password = args [2]; // Prepare a simplified Initial Context as we are going to do // everything by hand. Hashtable env = new Hashtable(); env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext(env); // Get a SessionCtx that represents a database instance. ServiceCtx service = (ServiceCtx) ic.lookup(serviceURL); // Create a session in the instance. // The session name must start with a colon(:). SessionCtx session = (SessionCtx) service.createSubcontext(":session1"); session.login(username, password, null); // Print a header line. System.out.println ("\n\nName Create Date Owner"); listOneDirectory ("/", session); } public static void listOneDirectory (String name, SessionCtx ctx) throws Exception { System.out.print(name); for (int i = name.length(); i < 30; i++) System.out.print(" "); ctxAttribs attribs = null; try { attribs = ctx.getAttributes(); } catch (org.omg.CORBA.NO_PERMISSION e) { return; } System.out.print(attribs.creation_ts); for (int i = 30 + attribs.creation_ts.length(); i < 55; i++) System.out.print(" "); System.out.print(attribs.owner); /* * You could also add output for the access permissions: * attribs.read * attribs.write * attribs.execute */ System.out.println(); // Show the sub entries listEntries(ctx, name); } public static void listEntries (Context context, String prefix) throws Exception { NamingEnumeration bindings = context.list(""); while (bindings.hasMore()){ Binding binding = (Binding) bindings.next(); String name = binding.getName(); Object object = context.lookup(name); if (object instanceof SessionCtx) listOneDirectory(prefix + name + "/", (SessionCtx) object); else if (object instanceof PublishedObject) listOneObject(prefix + name, (PublishedObject) object); else // We should never get here. System.out.println(prefix + name + ": " + object.getClass()); } } public static void listOneObject (String name, PublishedObject obj) throws Exception { objAttribsHolder holder = new objAttribsHolder(); try { obj.get_attributes(holder); } catch (org.omg.CORBA.NO_PERMISSION e) { return; } objAttribs attribs = holder.value; System.out.print(name); for (int i = name.length(); i < 30; i++) System.out.print(" "); System.out.print(attribs.creation_ts); for (int i = 30 + attribs.creation_ts.length(); i < 55; i++) System.out.print(" "); System.out.print(attribs.owner); /* * You could also add output for: * attribs.class_name * attribs.schema * attribs.helper * and the access permissions: * attribs.read * attribs.write * attribs.execute */ System.out.println(); } }