| Previous | Table of Contents | Next |
Figure 5.4 Results of calling list() and listBindings().
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.*;
// example use: java Print
public class Print
{
public static void main(String[] args)
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
com.sun.jndi.fscontext.RefFSContextFactory);
env.put(Context.PROVIDER_URL,file:/tmp/marketing);
try
{
Context initCtx = new InitialContext(env);
// We assume we are reading in a file.
File f =(File)initCtx.lookup(reports/report1.txt);
//Print the file located
if(f != null)
{
BufferedReader br =
new BufferedReader(new FileReader(f));
String l = null;
while((l = br.readLine()) != null)
System.out.println(l);
}
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace(System.out);
}
}
}
The bold code above performs the lookup on the current context looking for reports/report1.txt. If the context had been / instead of /tmp/marketing, a lookup would have to explicitly be set to tmp/marketing/reports/report1.txt to perform the same lookup operation. All of these searching operations rely on the concept of object bindings, which we examine next.
A binding is the name of an object, the name of the objects class, and the object itself. Context provides methods for manipulating these bindings. Table 5.5 lists these methods.
The examples in this section revolve around the fsContext service provider, but it is important to keep in mind that these same operations can be used for removing an object from a CORBA repository or adding personal contact information to an LDAP directory server. All the functions described in this section are useful with any provider. How that provider actually implements each function depends on the provider. The documentation on each provider will tell you how these functions work for that specific provider. For example, unbind deletes a file when using the file system provider, but with RMI it simply removes an object reference from the remote registry. Lets look at the unbind function first.
| Table 5.5 Binding-Related Methods of Context | |
| Method | Use |
|---|---|
| bind(Name, Object), bind(String, Object) | Binds the name to the object in the context. Throws an exception if the name is already in use. |
| createSubcontext(Name), createSubcontext(String) | Creates and binds a new context to the name. |
| destroySubcontext(Name), destroySubcontext(String) | Destroys the named context and removes it from the namespace. |
| rebind(Name, Object), rebind(String, Object) | Binds the name to the object, overwriting any existing binding. |
| rename(Name oldName, Name newName), rename(String oldName, String newName | Binds newName to the object bound to oldName and unbinds oldName. |
| unbind(Name), unbind(String) | Unbinds the named object from the namespace. |
Deleting an Object Binding
To delete report2.txt from the marketing reports directory, execute the command java Delete report2.txt to initiate the following code:
import javax.naming.*;
import java.util.Hashtable;
// Deletes files from the marketing directory
// example use: java Delete filename
public class Delete
{
public static void main(String[] args)
{
// Define a starting point for operation.
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 +
initialContextString);
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
com.sun.jndi.fscontext.RefFSContextFactory);
env.put(Context.PROVIDER_URL,file: + initalContextString);
try
{
Context initCtx = new InitialContext(env);
// Delete file or other object...
System.out.println(Attempting to unbind + args[0]);
initCtx.unbind(args[0]);
System.out.println(Done.);
}catch(NamingException e)
{
System.out.println(e);
e.printStackTrace(System.out);
}
}
}
The Delete program sets an initial context of /tmp/marketing/reports and requests that the user enter the name of a file to delete in that directory. The unbind method is called to unbind or delete the selected file. The initial context is set for safety as this program deletes files; we wouldnt want you to accidentally delete important files on your computer.
NOTE: The fsContext provider will not delete directories, it will only delete files through the unbind method. Other providers may perform differently.
Renaming and Moving an Object
Using the rename method of a context allows you to change the name bound to the object. In the case of the file system provider, this is equivalent to changing the name of a file or directory. However, there is one difference: If the path to the named object differs from the path to the new name, the object is explicitly moved in the context. For example, if you rename /tmp/marketing to /tmp/products, the directory marketing will be renamed products. However, if you rename /tmp/marketing/reports/report1.txt to /tmp/report1.txt, the report1.txt file (object) will be moved into the subcontext /tmp. The program Rename in the code listed below takes two command line inputs: a file or directory name or path and the new path name. Try renaming/moving files and directories to get a feel for how this works.
| Previous | Table of Contents | Next |