Source code: org/media/naming/MemoryContextFactory.java
1 /*
2 * $COPYRIGHT$
3 * $Id: MemoryContextFactory.java,v 1.3 2002/07/11 21:48:16 neuro Exp $
4 *
5 * Date Author Changes
6 * May 20 2001 Remus Pereni Created
7 */
8
9 package org.media.naming;
10
11
12 import java.util.Hashtable;
13 import javax.naming.Context;
14 import javax.naming.NamingException;
15 import javax.naming.CompositeName;
16 import javax.naming.NotContextException;
17 import javax.naming.NoPermissionException;
18 import javax.naming.spi.InitialContextFactory;
19
20
21 /**
22 * Implements a context factory for {@link MemoryContext}. When set properly
23 * {@link javax.naming.InitialContext} will return a {@link
24 * MemoryContext} referencing the named path in the shared memory space.
25 * <p>
26 * To use this context factory the JNDI properties file must include
27 * the following properties:
28 * <pre>
29 * java.naming.factory.initial=org.media.naming.MemoryContextFactory
30 * java.naming.provider.url=
31 * </pre>
32 * Any non-empty URL will return a context to that path in the object tree,
33 * relative to the same shared root. The returned context is read/write.
34 * <p>
35 * This class was inspired by the tyrex.naming package from the
36 * <a href="http://tyrex.exolab.org">Tyrex</a> project.
37 * All credits for the good stuff should go to Assaf Arkin and
38 * the <a href="http://www.exolab.org">Exolab</a> group,
39 * all the bad stuff blame it on
40 * <a href="mailto:remus@nolimits.ro">me</a> :).
41 * </p>
42 *
43 * @author <a href="remus@nolimits.ro">Remus Pereni</a>
44 * @version $Revision: 1.3 $ $Date: 2002/07/11 21:48:16 $
45 * @see MemoryContext
46 */
47 public final class MemoryContextFactory
48 implements InitialContextFactory
49 {
50
51
52 /**
53 * The shared root of the binding tree.
54 */
55 private static final MemoryBinding _root = new MemoryBinding();
56
57
58 /**
59 * Returns a binding in the specified path. If the binding does
60 * not exist, the full path is created and a new binding is returned.
61 * The binding is always obtained from the shared root.
62 *
63 * @param path The path
64 * @return The memory binding for the path
65 * @throws NamingException Name is invalid
66 */
67 static synchronized MemoryBinding getBindings( String path )
68 throws NamingException
69 {
70 MemoryBinding binding;
71 MemoryBinding newBinding;
72 CompositeName name;
73 int i;
74
75 name = new CompositeName( path );
76 binding = _root;
77 for ( i = 0 ; i < name.size() ; ++i ) {
78 if ( name.get( i ).length() > 0 ) {
79 try {
80 newBinding = (MemoryBinding) binding.get( name.get( i ) );
81 if ( newBinding == null ) {
82 newBinding = new MemoryBinding();
83 binding.put( name.get( i ), newBinding );
84 }
85 binding = newBinding;
86 } catch ( ClassCastException except ) {
87 throw new NotContextException( path + " does not specify a context" );
88 }
89 }
90 }
91 return binding;
92 }
93
94
95 /**
96 * Returns an initial context based on the {@link javax.naming.Context#PROVIDER_URL}
97 * environment attribute. If this attribute is missing or an empty
98 * string, a new memory context be returned. Otherwise, the specified
99 * context will be returned.
100 */
101 public Context getInitialContext( Hashtable env )
102 throws NamingException
103 {
104 String url = null;
105
106 if ( env.get( Context.PROVIDER_URL ) != null )
107 url = env.get( Context.PROVIDER_URL ).toString();
108 if ( url == null || url.length() == 0 )
109 return new MemoryContext( new MemoryBinding(), env );
110 else
111 return new MemoryContext( getBindings( url ), env );
112 }
113
114
115 }
116