View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    */
8   package org.apache.avalon.excalibur.naming.memory;
9   
10  import java.util.Enumeration;
11  import java.util.HashMap;
12  import java.util.Hashtable;
13  import java.util.Map;
14  import java.util.NoSuchElementException;
15  import javax.naming.*;
16  import javax.naming.Context;
17  import org.apache.avalon.excalibur.naming.AbstractLocalContext;
18  import org.apache.avalon.excalibur.naming.Namespace;
19  
20  /***
21   * Start of a generic Context implementation.
22   *
23   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
24   * @version $Revision: 1.2 $
25   */
26  public class MemoryContext
27      extends AbstractLocalContext
28  {
29      protected Hashtable    m_bindings;
30  
31      protected MemoryContext( final Namespace namespace,
32                               final Hashtable environment,
33                               final Context parent,
34                               final Hashtable bindings )
35      {
36          super( namespace, environment, parent );
37          m_bindings = bindings;
38      }
39  
40      public MemoryContext( final Namespace namespace,
41                            final Hashtable environment,
42                            final Context parent )
43      {
44          this( namespace, environment, parent, new Hashtable( 11 ) );
45      }
46  
47      protected Context newContext()
48          throws NamingException
49      {
50          return new MemoryContext( m_namespace, m_environment, m_parent );
51      }
52  
53      protected Context cloneContext()
54          throws NamingException
55      {
56          return new MemoryContext( m_namespace, m_environment, m_parent, m_bindings );
57      }
58  
59      protected void doLocalBind( final Name name, final Object object )
60          throws NamingException
61      {
62          m_bindings.put( name.get( 0 ), object );
63      }
64  
65      protected NamingEnumeration doLocalList()
66          throws NamingException
67      {
68          return new MemoryNamingEnumeration( this, m_namespace, m_bindings, false );
69      }
70  
71      protected NamingEnumeration doLocalListBindings()
72          throws NamingException
73      {
74          return new MemoryNamingEnumeration( this, m_namespace, m_bindings, true );
75      }
76  
77      /***
78       * Actually lookup raw entry in local context.
79       * When overidding this it is not neccesary to resolve references etc.
80       *
81       * @param name the name in local context (size() == 1)
82       * @return the bound object
83       * @exception NamingException if an error occurs
84       */
85      protected Object doLocalLookup( final Name name )
86          throws NamingException
87      {
88          final Object object = m_bindings.get( name.get( 0 ) );
89          if( null == object ) throw new NameNotFoundException( name.get( 0 ) );
90          return object;
91      }
92  
93      /***
94       * Actually unbind raw entry in local context.
95       *
96       * @param name the name in local context (size() == 1)
97       * @exception NamingException if an error occurs
98       */
99      protected void doLocalUnbind( final Name name )
100         throws NamingException
101     {
102         m_bindings.remove( name.get( 0 ) );
103     }
104 }
105