Java Tutorial/J2EE Application/Context
Содержание
- 1 Access a specific object by using the lookup or lookupLink methods
- 2 Bind class.
- 3 Create Context
- 4 Deleting an Object Binding
- 5 Renaming and Moving an Object
- 6 Replacing and Adding Objects
- 7 returns a NamingEnumeration of people
- 8 Search Filter Symbols
- 9 Using a search result, you could you could use the following program to print the results:
Access a specific object by using the lookup or lookupLink methods
<source lang="java">
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; public class Print {
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/tmp/marketing"); Context initCtx = new InitialContext(env); File f = (File) initCtx.lookup("reports/report1.txt"); if (f != null) { BufferedReader br = new BufferedReader(new FileReader(f)); String l = null; while ((l = br.readLine()) != null) System.out.println(l); } }
}</source>
Bind class.
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.Reference; import javax.naming.Referenceable; import javax.naming.StringRefAddr; public class Bind {
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/tmp"); Context initCtx = new InitialContext(env); initCtx.rebind("Susan", new Car("Toyota", "Camry")); Car c = (Car) initCtx.lookup("Susan"); System.out.println(c); }
} class Car implements Referenceable {
String make; String model; public Car(String mk, String md) { make = mk; model = md; } public Reference getReference() throws NamingException { String cName = Car.class.getName(); StringRefAddr cRef = new StringRefAddr("Car Description", make + ":" + model); String cfName = "asdf"; Reference ref = new Reference(cName, cRef, cfName, null); return ref; } public String toString() { return (make + " " + model); }
}</source>
Create Context
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingEnumeration; public class List {
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/tmp/marketing"); Object item = null; Context initCtx = new InitialContext(env); NamingEnumeration nl = initCtx.list("reports"); if (nl == null) System.out.println("\nNo items in name list"); else while (nl.hasMore()) { item = nl.next(); System.out.println("item"s class is " + item.getClass().getName()); System.out.println(item); System.out.println(""); } }
}</source>
Deleting an Object Binding
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; public class Delete {
public static void main(String[] args) throws Exception { String initalContextString = "/tmp/marketing/reports"; if (args.length < 1) { System.out.println("Usage: java Delete filename"); System.exit(-1); } System.out.println("This program assumes the context is " + initalContextString); Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:" + initalContextString); Context initCtx = new InitialContext(env); System.out.println("Attempting to unbind " + args[0]); initCtx.unbind(args[0]); System.out.println("Done."); }
}</source>
Renaming and Moving an Object
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; public class Rename {
public static void main(String[] args) throws Exception { String initialContextString = "/"; if (args.length < 2) { System.out.println("Useage: java Rename filename1 filename2"); System.exit(-1); } Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:" + initialContextString); Context initCtx = new InitialContext(env); System.out.println("Renaming " + args[0] + " to " + args[1]); initCtx.rename(args[0], args[1]); }
}</source>
Replacing and Adding Objects
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class Changes {
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/tmp/marketing"); Context initCtx = new InitialContext(env); initCtx.rename("reports", "oldreports"); initCtx.createSubcontext("reports"); }
}</source>
returns a NamingEnumeration of people
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchResult; public class FilterExample {
public static void main(String[] argc) throws Exception { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample"); DirContext dctx = new InitialDirContext(env); String filter = "(&(cn=E*)(account>1005))"; NamingEnumeration result = dctx.search("ou=People", filter, null); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); } }
}</source>
Search Filter Symbols
<source lang="java">
Symbol Description & Logical and. All items in the list must be true, as in (&(a=1)(b=2) ). | Logical or. One or more of the items must be true, as in (|(a=1)(b=2) ). ! Not. The negated item must be false, as in (!(3=4)) is true. = Checks for equality based on the matching rule of the attribute. ~= Checks for approximate equality based on the matching rule of the attribute. >= Checks that the attribute is greater than the value. <= Checks that the attribute is less than the value. =* Checks for existence of the attribute.
- In an equality test; represents a wildcard representing zero or more characters at that position, as in (name=Alb*).
\ Used for escaping *, (, and ) inside an attribute value.</source>
Using a search result, you could you could use the following program to print the results:
<source lang="java">
import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchResult; public class AttributeExample {
public static void main(String[] argc) throws Exception { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample"); DirContext dctx = new InitialDirContext(env); Attributes attrs = new BasicAttributes(true); attrs.put(new BasicAttribute("email")); attrs.put(new BasicAttribute("website", "www.pri.ru")); NamingEnumeration result = dctx.search("ou=People", attrs); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); Attributes srchAttrs = sr.getAttributes(); NamingEnumeration attributes = srchAttrs.getAll(); while (attributes.hasMore()) { Attribute attr = (Attribute) attributes.next(); System.out.println("Attribute: " + attr.getID()); NamingEnumeration values = attr.getAll(); while (values.hasMore()) { Object value = values.next(); System.out.println("Value = " + value); } } } }
}</source>