Coverage Report - org.mule.impl.jndi.SimpleContext
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleContext
14%
10/69
15%
3/20
1.455
SimpleContext$1
0%
0/7
N/A
1.455
SimpleContext$2
0%
0/7
N/A
1.455
 
 1  
 /*
 2  
  * $Id: SimpleContext.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.impl.jndi;
 12  
 
 13  
 import java.util.HashMap;
 14  
 import java.util.Hashtable;
 15  
 import java.util.Iterator;
 16  
 import java.util.Map;
 17  
 import java.util.jar.Attributes;
 18  
 
 19  
 import javax.naming.Binding;
 20  
 import javax.naming.CompositeName;
 21  
 import javax.naming.Context;
 22  
 import javax.naming.Name;
 23  
 import javax.naming.NameAlreadyBoundException;
 24  
 import javax.naming.NameClassPair;
 25  
 import javax.naming.NameNotFoundException;
 26  
 import javax.naming.NameParser;
 27  
 import javax.naming.NamingEnumeration;
 28  
 import javax.naming.NamingException;
 29  
 import javax.naming.OperationNotSupportedException;
 30  
 
 31  
 public class SimpleContext implements Context
 32  
 {
 33  
     /** What holds the bindings. */
 34  6
     protected Map bindings = new HashMap();
 35  
 
 36  
     /** Context's environment. */
 37  
     private Hashtable environment;
 38  
 
 39  
     public SimpleContext()
 40  
     {
 41  6
         super();
 42  6
     }
 43  
 
 44  
     public Object lookupLink(Name name) throws NamingException
 45  
     {
 46  
         // unsupported
 47  0
         return null;
 48  
     }
 49  
 
 50  
     public void rename(Name oldName, Name newName) throws NamingException
 51  
     {
 52  
         // unsupported
 53  0
     }
 54  
 
 55  
     public NameParser getNameParser(Name name) throws NamingException
 56  
     {
 57  
         // unsupported
 58  0
         return null;
 59  
     }
 60  
 
 61  
     public NamingEnumeration list(Name name) throws NamingException
 62  
     {
 63  
         // unsupported
 64  0
         return null;
 65  
     }
 66  
 
 67  
     public Object lookup(Name name) throws NamingException
 68  
     {
 69  0
         return lookup(name.toString());
 70  
     }
 71  
 
 72  
     public Object lookup(String name) throws NamingException
 73  
     {
 74  6
         Object rc = bindings.get(name);
 75  6
         if (rc == null)
 76  
         {
 77  2
             throw new NameNotFoundException(name);
 78  
         }
 79  4
         return rc;
 80  
     }
 81  
 
 82  
     public void bind(Name name, Object obj) throws NamingException
 83  
     {
 84  0
         bind(name.toString(), obj);
 85  0
     }
 86  
 
 87  
     public void bind(String name, Object obj) throws NamingException
 88  
     {
 89  12
         if (bindings.containsKey(name))
 90  
         {
 91  0
             throw new NameAlreadyBoundException(name);
 92  
         }
 93  12
         bindings.put(name, obj);
 94  12
     }
 95  
 
 96  
     public void rebind(Name name, Object obj) throws NamingException
 97  
     {
 98  0
         rebind(name.toString(), obj);
 99  0
     }
 100  
 
 101  
     public void rebind(String name, Object obj) throws NamingException
 102  
     {
 103  0
         bindings.put(name, obj);
 104  0
     }
 105  
 
 106  
     public void unbind(Name name) throws NamingException
 107  
     {
 108  0
         unbind(name.toString());
 109  0
     }
 110  
 
 111  
     public void unbind(String name) throws NamingException
 112  
     {
 113  0
         if (bindings.remove(name) == null)
 114  
         {
 115  0
             throw new NameNotFoundException(name);
 116  
         }
 117  0
     }
 118  
 
 119  
     public void rename(Attributes.Name oldName, Attributes.Name newName) throws NamingException
 120  
     {
 121  0
         rename(oldName.toString(), newName.toString());
 122  0
     }
 123  
 
 124  
     public void rename(String oldName, String newName) throws NamingException
 125  
     {
 126  0
         if (!bindings.containsKey(oldName))
 127  
         {
 128  0
             throw new NameNotFoundException(oldName);
 129  
         }
 130  0
         if (bindings.containsKey(newName))
 131  
         {
 132  0
             throw new NameAlreadyBoundException(newName);
 133  
         }
 134  
 
 135  0
         bindings.put(newName, bindings.remove(oldName));
 136  0
     }
 137  
 
 138  
     public NamingEnumeration list(Attributes.Name name) throws NamingException
 139  
     {
 140  0
         return list(name.toString());
 141  
     }
 142  
 
 143  
     public NamingEnumeration list(String name) throws NamingException
 144  
     {
 145  0
         if (name.length() > 0)
 146  
         {
 147  0
             throw new OperationNotSupportedException("subcontexts not supported");
 148  
         }
 149  0
         final Iterator i = bindings.entrySet().iterator();
 150  0
         return new NamingEnumeration()
 151  
         {
 152  
             public Object next()
 153  
             {
 154  0
                 Map.Entry e = (Map.Entry) i.next();
 155  0
                 return new NameClassPair((String) e.getKey(), e.getValue().getClass().getName());
 156  
             }
 157  
 
 158  
             public boolean hasMore()
 159  
             {
 160  0
                 return i.hasNext();
 161  
             }
 162  
 
 163  
             public void close()
 164  
             {
 165  
                 // noop
 166  0
             }
 167  
 
 168  
             public boolean hasMoreElements()
 169  
             {
 170  0
                 return hasMore();
 171  
             }
 172  
 
 173  0
             public Object nextElement()
 174  
             {
 175  0
                 return next();
 176  
             }
 177  
         };
 178  
     }
 179  
 
 180  
     public NamingEnumeration listBindings(Name name) throws NamingException
 181  
     {
 182  0
         return listBindings(name.toString());
 183  
     }
 184  
 
 185  
     public NamingEnumeration listBindings(String name) throws NamingException
 186  
     {
 187  0
         if (name.length() > 0)
 188  
         {
 189  0
             throw new OperationNotSupportedException("subcontexts not supported");
 190  
         }
 191  0
         final Iterator i = bindings.entrySet().iterator();
 192  0
         return new NamingEnumeration()
 193  
         {
 194  
             public Object next()
 195  
             {
 196  0
                 Map.Entry e = (Map.Entry) i.next();
 197  0
                 return new Binding((String) e.getKey(), e.getValue());
 198  
             }
 199  
 
 200  
             public boolean hasMore()
 201  
             {
 202  0
                 return i.hasNext();
 203  
             }
 204  
 
 205  
             public void close()
 206  
             {
 207  
                 // noop
 208  0
             }
 209  
 
 210  
             public boolean hasMoreElements()
 211  
             {
 212  0
                 return hasMore();
 213  
             }
 214  
 
 215  0
             public Object nextElement()
 216  
             {
 217  0
                 return next();
 218  
             }
 219  
 
 220  
         };
 221  
     }
 222  
 
 223  
     public void destroySubcontext(Name name) throws NamingException
 224  
     {
 225  0
         destroySubcontext(name.toString());
 226  0
     }
 227  
 
 228  
     public void destroySubcontext(String name) throws NamingException
 229  
     {
 230  0
         throw new OperationNotSupportedException("subcontexts not supported");
 231  
     }
 232  
 
 233  
     public Context createSubcontext(Name name) throws NamingException
 234  
     {
 235  0
         return createSubcontext(name.toString());
 236  
     }
 237  
 
 238  
     public Context createSubcontext(String name) throws NamingException
 239  
     {
 240  0
         throw new OperationNotSupportedException("subcontexts not supported");
 241  
     }
 242  
 
 243  
     public Object lookupLink(Attributes.Name name) throws NamingException
 244  
     {
 245  0
         return lookupLink(name.toString());
 246  
     }
 247  
 
 248  
     public Object lookupLink(String name) throws NamingException
 249  
     {
 250  0
         return lookup(name);
 251  
     }
 252  
 
 253  
     public NameParser getNameParser(Attributes.Name name)
 254  
     {
 255  0
         return getNameParser(name.toString());
 256  
     }
 257  
 
 258  
     public NameParser getNameParser(String name)
 259  
     {
 260  0
         throw new UnsupportedOperationException("getNameParser");
 261  
     }
 262  
 
 263  
     public String composeName(String name, String prefix) throws NamingException
 264  
     {
 265  0
         return composeName(new CompositeName(name), new CompositeName(prefix)).toString();
 266  
     }
 267  
 
 268  
     public Name composeName(Name name, Name prefix) throws NamingException
 269  
     {
 270  0
         Name result = (Name) prefix.clone();
 271  0
         result.addAll(name);
 272  0
         return result;
 273  
     }
 274  
 
 275  
     public Object addToEnvironment(String key, Object val)
 276  
     {
 277  0
         if (environment == null)
 278  
         {
 279  0
             environment = new Hashtable();
 280  
         }
 281  0
         return environment.put(key, val);
 282  
     }
 283  
 
 284  
     public Object removeFromEnvironment(String key)
 285  
     {
 286  0
         if (environment == null)
 287  
         {
 288  0
             environment = new Hashtable();
 289  
         }
 290  0
         return environment.remove(key);
 291  
     }
 292  
 
 293  
     public Hashtable getEnvironment()
 294  
     {
 295  0
         if (environment == null)
 296  
         {
 297  0
             environment = new Hashtable();
 298  
         }
 299  0
         return environment;
 300  
     }
 301  
 
 302  
     public void close()
 303  
     {
 304  
         // noop
 305  0
     }
 306  
 
 307  
     public String getNameInNamespace()
 308  
     {
 309  0
         return "";
 310  
     }
 311  
 
 312  
 }