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.rmi.server;
9   
10  import java.io.Serializable;
11  import java.rmi.Remote;
12  import java.util.ArrayList;
13  import javax.naming.Binding;
14  import javax.naming.CompositeName;
15  import javax.naming.Context;
16  import javax.naming.Name;
17  import javax.naming.NameClassPair;
18  import javax.naming.NameParser;
19  import javax.naming.NamingEnumeration;
20  import javax.naming.NamingException;
21  import org.apache.avalon.excalibur.naming.RemoteContext;
22  import org.apache.avalon.excalibur.naming.rmi.RMINamingProvider;
23  
24  /***
25   * The RMI implementation of provider.
26   *
27   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
28   * @version $Revision: 1.2 $
29   */
30  public class RMINamingProviderImpl
31      implements Serializable, RMINamingProvider
32  {
33      protected Context           m_root;
34  
35      public RMINamingProviderImpl( final Context root )
36      {
37          m_root = root;
38      }
39  
40      public NameParser getNameParser()
41          throws NamingException
42      {
43          return m_root.getNameParser( new CompositeName() );
44      }
45  
46      public void bind( final Name name, final String className, final Object object )
47          throws NamingException
48      {
49          final Binding binding = new Binding( name.toString(), className, object, true );
50          m_root.bind( name, binding );
51      }
52  
53      public void rebind( final Name name, final String className, final Object object )
54          throws NamingException
55      {
56          final Binding binding = new Binding( name.toString(), className, object, true );
57          m_root.rebind( name, binding );
58      }
59  
60      public Context createSubcontext( final Name name )
61          throws NamingException
62      {
63          m_root.createSubcontext( name );
64  
65          final RemoteContext context = new RemoteContext( null, name );
66          return context;
67      }
68  
69      public void destroySubcontext( final Name name )
70          throws NamingException
71      {
72          m_root.destroySubcontext( name );
73      }
74  
75      public NameClassPair[] list( final Name name )
76          throws NamingException
77      {
78          //Remember that the bindings returned by this
79          //actually have a nested Binding as an object
80          final NamingEnumeration enum = m_root.listBindings( name );
81          final ArrayList pairs = new ArrayList();
82  
83          while( enum.hasMore() )
84          {
85              final Binding binding = (Binding)enum.next();
86              final Object object = binding.getObject();
87  
88              String className = null;
89  
90              //check if it is an entry or a context
91              if( object instanceof Binding )
92              {
93                  //must be an entry
94                  final Binding entry = (Binding)binding.getObject();
95                  className = entry.getObject().getClass().getName();
96              }
97              else if( object instanceof Context )
98              {
99                  //must be a context
100                 className = RemoteContext.class.getName();
101             }
102             else
103             {
104                 className = object.getClass().getName();
105             }
106 
107             pairs.add( new NameClassPair( binding.getName(), className ) );
108         }
109 
110         return (NameClassPair[])pairs.toArray( new NameClassPair[ 0 ] );
111     }
112 
113     public Binding[] listBindings( final Name name )
114         throws NamingException
115     {
116         //Remember that the bindings returned by this
117         //actually have a nested Binding as an object
118         final NamingEnumeration enum = m_root.listBindings( name );
119         final ArrayList bindings = new ArrayList();
120 
121         while( enum.hasMore() )
122         {
123             final Binding binding = (Binding)enum.next();
124             Object object = binding.getObject();
125             String className = null;
126 
127             //check if it is an entry or a context
128             if( object instanceof Binding )
129             {
130                 //must be an entry
131                 final Binding entry = (Binding)binding.getObject();
132                 object = entry.getObject();
133                 className = object.getClass().getName();
134             }
135             else if( object instanceof Context )
136             {
137                 //must be a context
138                 className = RemoteContext.class.getName();
139                 object = new RemoteContext( null, name );
140             }
141             else
142             {
143                 className = object.getClass().getName();
144             }
145 
146             final Binding result =
147                 new Binding( binding.getName(), className, object );
148             bindings.add( result );
149         }
150 
151         return (Binding[])bindings.toArray( new Binding[ 0 ] );
152     }
153 
154     public Object lookup( final Name name )
155         throws NamingException
156     {
157         Object object = m_root.lookup( name );
158 
159         //check if it is an entry or a context
160         if( object instanceof Binding )
161         {
162             object = ((Binding)object).getObject();
163         }
164         else if( object instanceof Context )
165         {
166             //must be a context
167             object = new RemoteContext( null, name.getPrefix( name.size() - 1 ) );
168         }
169 
170         return object;
171     }
172 
173     public void unbind( final Name name )
174         throws NamingException
175     {
176         m_root.unbind( name );
177     }
178 }