1 8 package org.apache.avalon.excalibur.naming.rmi.server; 9 10 import java.io.Serializable; 11 import java.util.ArrayList; 12 import javax.naming.Binding; 13 import javax.naming.CompositeName; 14 import javax.naming.Context; 15 import javax.naming.Name; 16 import javax.naming.NameClassPair; 17 import javax.naming.NameParser; 18 import javax.naming.NamingEnumeration; 19 import javax.naming.NamingException; 20 import org.apache.avalon.excalibur.naming.RemoteContext; 21 import org.apache.avalon.excalibur.naming.rmi.RMINamingProvider; 22 23 29 public class RMINamingProviderImpl 30 implements Serializable, RMINamingProvider 31 { 32 private Context m_root; 33 34 public RMINamingProviderImpl( final Context root ) 35 { 36 m_root = root; 37 } 38 39 public NameParser getNameParser() 40 throws NamingException 41 { 42 return m_root.getNameParser( new CompositeName() ); 43 } 44 45 public void bind( final Name name, final String className, final Object object ) 46 throws NamingException 47 { 48 final Binding binding = new Binding( name.toString(), className, object, true ); 49 m_root.bind( name, binding ); 50 } 51 52 public void rebind( final Name name, final String className, final Object object ) 53 throws NamingException 54 { 55 final Binding binding = new Binding( name.toString(), className, object, true ); 56 m_root.rebind( name, binding ); 57 } 58 59 public Context createSubcontext( final Name name ) 60 throws NamingException 61 { 62 m_root.createSubcontext( name ); 63 64 final RemoteContext context = new RemoteContext( null, name ); 65 return context; 66 } 67 68 public void destroySubcontext( final Name name ) 69 throws NamingException 70 { 71 m_root.destroySubcontext( name ); 72 } 73 74 public NameClassPair[] list( final Name name ) 75 throws NamingException 76 { 77 final NamingEnumeration enum = m_root.listBindings( name ); 80 final ArrayList pairs = new ArrayList(); 81 82 while( enum.hasMore() ) 83 { 84 final Binding binding = (Binding)enum.next(); 85 final Object object = binding.getObject(); 86 87 String className = null; 88 89 if( object instanceof Binding ) 91 { 92 final Binding entry = (Binding)binding.getObject(); 94 className = entry.getObject().getClass().getName(); 95 } 96 else if( object instanceof Context ) 97 { 98 className = RemoteContext.class.getName(); 100 } 101 else 102 { 103 className = object.getClass().getName(); 104 } 105 106 pairs.add( new NameClassPair( binding.getName(), className ) ); 107 } 108 109 return (NameClassPair[])pairs.toArray( new NameClassPair[ 0 ] ); 110 } 111 112 public Binding[] listBindings( final Name name ) 113 throws NamingException 114 { 115 final NamingEnumeration enum = m_root.listBindings( name ); 118 final ArrayList bindings = new ArrayList(); 119 120 while( enum.hasMore() ) 121 { 122 final Binding binding = (Binding)enum.next(); 123 Object object = binding.getObject(); 124 String className = null; 125 126 if( object instanceof Binding ) 128 { 129 final Binding entry = (Binding)binding.getObject(); 131 object = entry.getObject(); 132 className = object.getClass().getName(); 133 } 134 else if( object instanceof Context ) 135 { 136 className = RemoteContext.class.getName(); 138 object = new RemoteContext( null, name ); 139 } 140 else 141 { 142 className = object.getClass().getName(); 143 } 144 145 final Binding result = 146 new Binding( binding.getName(), className, object ); 147 bindings.add( result ); 148 } 149 150 return (Binding[])bindings.toArray( new Binding[ 0 ] ); 151 } 152 153 public Object lookup( final Name name ) 154 throws NamingException 155 { 156 Object object = m_root.lookup( name ); 157 158 if( object instanceof Binding ) 160 { 161 object = ( (Binding)object ).getObject(); 162 } 163 else if( object instanceof Context ) 164 { 165 object = new RemoteContext( null, name.getPrefix( name.size() - 1 ) ); 167 } 168 169 return object; 170 } 171 172 public void unbind( final Name name ) 173 throws NamingException 174 { 175 m_root.unbind( name ); 176 } 177 } 178 | Popular Tags |