| Miscellaneous

## Adding Link Reference Support

> > Prerequisite: You should be familiar with link references before reading this section. Link references are discussed in the Miscellaneous lesson.

> A service provider typically supports link references if it supports lookups that can return link references. However, because most naming/directory services have their own notions of (native) symbolic links (such as aliases and/or referrals), link references might be supported only by service providers that also support federation.

> As with support for referrals, support for link references is an integral part of the service provider implementation. It affects all operations that involve name resolution. Furthermore, link references can be implemented in different ways. Following are some tips that you can follow, but the implementation that works for your service provider will depend on the features of the underlying naming/directory service. Especially important in this respect is how you've structured your code to handle federation.

> #### Dereferencing a Link

> A link reference is represented by the class `LinkRef` . Here is an example of a method for dereferencing a `LinkRef`.

> >     public static Object derefLink(LinkRef ref, Context currCtx,
> >         Hashtable env) throws NamingException {

> >         String link = ref.getLinkName();

> >         if (link.startsWith("./")) {
> >     	// A relative link; assume that currCtx is the
> >     	// immediate context in which the link is bound
> >     	return currCtx.lookup(link.substring(2));
> >         } else {
> >     	// An absolute link; resolve it to the initial context
> >     	Context ctx = new InitialContext(env);
> >     	try {
> >     	    return ctx.lookup(link);
> >     	} finally {
> >     	    ctx.close();  // Close when you're done
> >     	}
> >         }
> >     }

> #### When to Dereference

> `lookupLink()` should not dereference links. All other normal resolution of names for all methods in the `Context` interface and its subinterfaces always follows links.

> #### Detecting Cycles

> Resolution of the link name itself might cause resolution to pass through other links. The result might be a cycle of links whose resolution can't terminate normally.

> Several approaches are possible to detect and avoid cycles. One is to maintain a fixed-sized stack. In this case, each followed link is pushed onto the stack with the corresponding remaining name. When the stack is full, your provider should throw a `LinkLoopException`. Resolution of a link proceeds by resolving each link on the stack. When resolution of the link on the top of the stack completes, it is popped off and resolution on the next link on the stack begins. When the stack is empty, the resolution of the original link is complete.

| Miscellaneous