Bind.java Source code

Java tutorial

Introduction

Here is the source code for Bind.java

Source

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);
    }
}