Coverage Report - org.apache.myfaces.mc.test.core.mock.DefaultContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultContext
0%
0/185
0%
0/66
2.157
DefaultContext$1
N/A
N/A
2.157
DefaultContext$ListBindingEnumeration
0%
0/4
N/A
2.157
DefaultContext$ListEnumeration
0%
0/4
N/A
2.157
DefaultContext$LocalNamingEnumeration
0%
0/6
N/A
2.157
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.mc.test.core.mock;
 20  
 
 21  
 import javax.naming.Binding;
 22  
 import javax.naming.CompositeName;
 23  
 import javax.naming.Context;
 24  
 import javax.naming.LinkRef;
 25  
 import javax.naming.Name;
 26  
 import javax.naming.NameClassPair;
 27  
 import javax.naming.NameNotFoundException;
 28  
 import javax.naming.NameParser;
 29  
 import javax.naming.NamingEnumeration;
 30  
 import javax.naming.NamingException;
 31  
 import javax.naming.NotContextException;
 32  
 import javax.naming.OperationNotSupportedException;
 33  
 import javax.naming.Reference;
 34  
 import javax.naming.spi.NamingManager;
 35  
 
 36  
 import java.io.Serializable;
 37  
 import java.util.HashMap;
 38  
 import java.util.Hashtable;
 39  
 import java.util.Iterator;
 40  
 import java.util.Map;
 41  
 
 42  
 /**
 43  
  * A simple spring based JNDI context which is mutable
 44  
  *
 45  
  * NOTE: Code copied from org.apache.xbean.spring.jndi.DefaultContext
 46  
  *
 47  
  * @version $Revision: 657 $
 48  
  */
 49  
 public class DefaultContext implements Context, Serializable
 50  
 {
 51  
 
 52  
     private static final long serialVersionUID = -5754338187296859149L;
 53  0
     protected static final NameParser NAMED_PARSER = new NameParserImpl();
 54  
 
 55  0
     private boolean freeze = false;
 56  
 
 57  
     protected final Hashtable environment;        // environment for this context
 58  
     protected final Map bindings;         // bindings at my level
 59  
     protected final Map treeBindings;     // all bindings under me
 60  
 
 61  0
     private boolean frozen = false;
 62  0
     private String nameInNamespace = "";
 63  
     public static final String SEPARATOR = "/";
 64  
 
 65  
     public DefaultContext()
 66  0
     {
 67  0
         environment = new Hashtable();
 68  0
         bindings = new HashMap();
 69  0
         treeBindings = new HashMap();
 70  0
     }
 71  
 
 72  
     public DefaultContext(Hashtable env)
 73  0
     {
 74  0
         if (env == null)
 75  
         {
 76  0
             this.environment = new Hashtable();
 77  
         }
 78  
         else
 79  
         {
 80  0
             this.environment = new Hashtable(env);
 81  
         }
 82  0
         this.bindings = new HashMap();
 83  0
         this.treeBindings = new HashMap();
 84  0
     }
 85  
 
 86  
     public DefaultContext(Hashtable environment, Map bindings)
 87  0
     {
 88  0
         if (environment == null)
 89  
         {
 90  0
             this.environment = new Hashtable();
 91  
         }
 92  
         else
 93  
         {
 94  0
             this.environment = new Hashtable(environment);
 95  
         }
 96  0
         this.bindings = bindings;
 97  0
         treeBindings = new HashMap();
 98  0
         frozen = true;
 99  0
     }
 100  
 
 101  
     public DefaultContext(Hashtable environment, Map bindings, String nameInNamespace)
 102  
     {
 103  0
         this(environment, bindings);
 104  0
         this.nameInNamespace = nameInNamespace;
 105  0
     }
 106  
 
 107  
     protected DefaultContext(DefaultContext clone, Hashtable env)
 108  0
     {
 109  0
         this.bindings = clone.bindings;
 110  0
         this.treeBindings = clone.treeBindings;
 111  0
         this.environment = new Hashtable(env);
 112  0
     }
 113  
 
 114  
     protected DefaultContext(DefaultContext clone, Hashtable env, String nameInNamespace)
 115  
     {
 116  0
         this(clone, env);
 117  0
         this.nameInNamespace = nameInNamespace;
 118  0
     }
 119  
 
 120  
     public Object addToEnvironment(String propName, Object propVal) throws NamingException
 121  
     {
 122  0
         return environment.put(propName, propVal);
 123  
     }
 124  
 
 125  
     public Hashtable getEnvironment() throws NamingException
 126  
     {
 127  0
         return (Hashtable) environment.clone();
 128  
     }
 129  
 
 130  
     public Object removeFromEnvironment(String propName) throws NamingException
 131  
     {
 132  0
         return environment.remove(propName);
 133  
     }
 134  
 
 135  
     public Object lookup(String name) throws NamingException
 136  
     {
 137  0
         if (name.length() == 0)
 138  
         {
 139  0
             return this;
 140  
         }
 141  0
         Object result = treeBindings.get(name);
 142  0
         if (result == null)
 143  
         {
 144  0
             result = bindings.get(name);
 145  
         }
 146  0
         if (result == null)
 147  
         {
 148  0
             int pos = name.indexOf(':');
 149  0
             if (pos > 0)
 150  
             {
 151  0
                 String scheme = name.substring(0, pos);
 152  0
                 Context ctx = NamingManager.getURLContext(scheme, environment);
 153  0
                 if (ctx == null)
 154  
                 {
 155  0
                     throw new NamingException("scheme " + scheme + " not recognized");
 156  
                 }
 157  0
                 return ctx.lookup(name);
 158  
             }
 159  
             else
 160  
             {
 161  
                 // Split out the first name of the path
 162  
                 // and look for it in the bindings map.
 163  0
                 CompositeName path = new CompositeName(name);
 164  
 
 165  0
                 if (path.size() == 0)
 166  
                 {
 167  0
                     return this;
 168  
                 }
 169  
                 else
 170  
                 {
 171  0
                     String first = path.get(0);
 172  0
                     Object obj = bindings.get(first);
 173  0
                     if (obj == null)
 174  
                     {
 175  0
                         throw new NameNotFoundException(name);
 176  
                     }
 177  0
                     else if (obj instanceof Context && path.size() > 1)
 178  
                     {
 179  0
                         Context subContext = (Context) obj;
 180  0
                         obj = subContext.lookup(path.getSuffix(1));
 181  
                     }
 182  0
                     return obj;
 183  
                 }
 184  
             }
 185  
         }
 186  0
         if (result instanceof LinkRef)
 187  
         {
 188  0
             LinkRef ref = (LinkRef) result;
 189  0
             result = lookup(ref.getLinkName());
 190  
         }
 191  0
         if (result instanceof Reference)
 192  
         {
 193  
             try
 194  
             {
 195  0
                 result = NamingManager.getObjectInstance(result, null, null, this.environment);
 196  
             }
 197  0
             catch (NamingException e)
 198  
             {
 199  0
                 throw e;
 200  
             }
 201  0
             catch (Exception e)
 202  
             {
 203  0
                 throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
 204  0
             }
 205  
         }
 206  0
         if (result instanceof DefaultContext)
 207  
         {
 208  0
             String prefix = getNameInNamespace();
 209  0
             if (prefix.length() > 0)
 210  
             {
 211  0
                 prefix = prefix + SEPARATOR;
 212  
             }
 213  0
             result = new DefaultContext((DefaultContext) result, environment, prefix + name);
 214  
         }
 215  0
         return result;
 216  
     }
 217  
 
 218  
     public Object lookup(Name name) throws NamingException
 219  
     {
 220  0
         return lookup(name.toString());
 221  
     }
 222  
 
 223  
     public Object lookupLink(String name) throws NamingException
 224  
     {
 225  0
         return lookup(name);
 226  
     }
 227  
 
 228  
     public Name composeName(Name name, Name prefix) throws NamingException
 229  
     {
 230  0
         Name result = (Name) prefix.clone();
 231  0
         result.addAll(name);
 232  0
         return result;
 233  
     }
 234  
 
 235  
     public String composeName(String name, String prefix) throws NamingException
 236  
     {
 237  0
         CompositeName result = new CompositeName(prefix);
 238  0
         result.addAll(new CompositeName(name));
 239  0
         return result.toString();
 240  
     }
 241  
 
 242  
     public NamingEnumeration list(String name) throws NamingException
 243  
     {
 244  0
         Object o = lookup(name);
 245  0
         if (o == this)
 246  
         {
 247  0
             return new DefaultContext.ListEnumeration();
 248  
         }
 249  0
         else if (o instanceof Context)
 250  
         {
 251  0
             return ((Context) o).list("");
 252  
         }
 253  
         else
 254  
         {
 255  0
             throw new NotContextException();
 256  
         }
 257  
     }
 258  
 
 259  
     public NamingEnumeration listBindings(String name) throws NamingException
 260  
     {
 261  0
         Object o = lookup(name);
 262  0
         if (o == this)
 263  
         {
 264  0
             return new DefaultContext.ListBindingEnumeration();
 265  
         }
 266  0
         else if (o instanceof Context)
 267  
         {
 268  0
             return ((Context) o).listBindings("");
 269  
         }
 270  
         else
 271  
         {
 272  0
             throw new NotContextException();
 273  
         }
 274  
     }
 275  
 
 276  
     public Object lookupLink(Name name) throws NamingException
 277  
     {
 278  0
         return lookupLink(name.toString());
 279  
     }
 280  
 
 281  
     public NamingEnumeration list(Name name) throws NamingException
 282  
     {
 283  0
         return list(name.toString());
 284  
     }
 285  
 
 286  
     public NamingEnumeration listBindings(Name name) throws NamingException
 287  
     {
 288  0
         return listBindings(name.toString());
 289  
     }
 290  
 
 291  
     public void bind(Name name, Object value) throws NamingException
 292  
     {
 293  0
         bind(name.toString(), value);
 294  0
     }
 295  
 
 296  
     public void bind(String name, Object value) throws NamingException
 297  
     {
 298  0
         checkFrozen();
 299  0
         internalBind(name, value);
 300  0
     }
 301  
 
 302  
     public void close() throws NamingException
 303  
     {
 304  
         // ignore
 305  0
     }
 306  
 
 307  
     public Context createSubcontext(Name name) throws NamingException
 308  
     {
 309  0
         throw new OperationNotSupportedException();
 310  
     }
 311  
 
 312  
     public Context createSubcontext(String name) throws NamingException
 313  
     {
 314  0
         throw new OperationNotSupportedException();
 315  
     }
 316  
 
 317  
     public void destroySubcontext(Name name) throws NamingException
 318  
     {
 319  0
         throw new OperationNotSupportedException();
 320  
     }
 321  
 
 322  
     public void destroySubcontext(String name) throws NamingException
 323  
     {
 324  0
         throw new OperationNotSupportedException();
 325  
     }
 326  
 
 327  
     public String getNameInNamespace() throws NamingException
 328  
     {
 329  0
         return nameInNamespace;
 330  
     }
 331  
 
 332  
     public NameParser getNameParser(Name name) throws NamingException
 333  
     {
 334  0
         return NAMED_PARSER;
 335  
     }
 336  
 
 337  
     public NameParser getNameParser(String name) throws NamingException
 338  
     {
 339  0
         return NAMED_PARSER;
 340  
     }
 341  
 
 342  
     public void rebind(Name name, Object value) throws NamingException
 343  
     {
 344  0
         rebind(name.toString(), value);
 345  0
     }
 346  
 
 347  
     public void rebind(String name, Object value) throws NamingException
 348  
     {
 349  0
         checkFrozen();
 350  0
         internalBind(name, value, true);
 351  0
     }
 352  
 
 353  
     public void rename(Name oldName, Name newName) throws NamingException
 354  
     {
 355  0
         checkFrozen();
 356  0
         Object value = lookup(oldName);
 357  0
         unbind(oldName);
 358  0
         bind(newName, value);
 359  0
     }
 360  
 
 361  
     public void rename(String oldName, String newName) throws NamingException
 362  
     {
 363  0
         Object value = lookup(oldName);
 364  0
         unbind(oldName);
 365  0
         bind(newName, value);
 366  0
     }
 367  
 
 368  
     public void unbind(Name name) throws NamingException
 369  
     {
 370  0
         unbind(name.toString());
 371  0
     }
 372  
 
 373  
     public void unbind(String name) throws NamingException
 374  
     {
 375  0
         checkFrozen();
 376  0
         internalBind(name, null, true);
 377  0
     }
 378  
 
 379  0
     private abstract class LocalNamingEnumeration implements NamingEnumeration
 380  
     {
 381  
 
 382  0
         private Iterator i = bindings.entrySet().iterator();
 383  
 
 384  
         public boolean hasMore() throws NamingException
 385  
         {
 386  0
             return i.hasNext();
 387  
         }
 388  
 
 389  
         public boolean hasMoreElements()
 390  
         {
 391  0
             return i.hasNext();
 392  
         }
 393  
 
 394  
         protected Map.Entry getNext()
 395  
         {
 396  0
             return (Map.Entry) i.next();
 397  
         }
 398  
 
 399  
         public void close() throws NamingException
 400  
         {
 401  0
         }
 402  
     }
 403  
 
 404  0
     private class ListEnumeration extends DefaultContext.LocalNamingEnumeration
 405  
     {
 406  
 
 407  
         public Object next() throws NamingException
 408  
         {
 409  0
             return nextElement();
 410  
         }
 411  
 
 412  
         public Object nextElement()
 413  
         {
 414  0
             Map.Entry entry = getNext();
 415  0
             return new NameClassPair((String) entry.getKey(), entry.getValue().getClass().getName());
 416  
         }
 417  
     }
 418  
 
 419  0
     private class ListBindingEnumeration extends DefaultContext.LocalNamingEnumeration
 420  
     {
 421  
 
 422  
         public Object next() throws NamingException
 423  
         {
 424  0
             return nextElement();
 425  
         }
 426  
 
 427  
         public Object nextElement()
 428  
         {
 429  0
             Map.Entry entry = getNext();
 430  0
             return new Binding((String) entry.getKey(), entry.getValue());
 431  
         }
 432  
     }
 433  
 
 434  
     public Map getEntries()
 435  
     {
 436  0
         return new HashMap(bindings);
 437  
     }
 438  
 
 439  
     public void setEntries(Map entries) throws NamingException
 440  
     {
 441  0
         if (entries != null)
 442  
         {
 443  0
             for (Iterator iter = entries.entrySet().iterator(); iter.hasNext();)
 444  
             {
 445  0
                 Map.Entry entry = (Map.Entry) iter.next();
 446  0
                 String name = (String) entry.getKey();
 447  0
                 Object value = entry.getValue();
 448  0
                 internalBind(name, value);
 449  0
             }
 450  
         }
 451  0
     }
 452  
 
 453  
     public boolean isFreeze()
 454  
     {
 455  0
         return freeze;
 456  
     }
 457  
 
 458  
     public void setFreeze(boolean freeze)
 459  
     {
 460  0
         this.freeze = freeze;
 461  0
     }
 462  
 
 463  
     /**
 464  
      * internalBind is intended for use only during setup or possibly by suitably synchronized superclasses. It binds
 465  
      * every possible lookup into a map in each context. To do this, each context strips off one name segment and if
 466  
      * necessary creates a new context for it. Then it asks that context to bind the remaining name. It returns a map
 467  
      * containing all the bindings from the next context, plus the context it just created (if it in fact created it).
 468  
      * (the names are suitably extended by the segment originally lopped off).
 469  
      *
 470  
      * @param name
 471  
      * @param value
 472  
      * @return
 473  
      * @throws javax.naming.NamingException
 474  
      */
 475  
     protected Map internalBind(String name, Object value) throws NamingException
 476  
     {
 477  0
         return internalBind(name, value, false);
 478  
 
 479  
     }
 480  
 
 481  
     protected Map internalBind(String name, Object value, boolean allowRebind) throws NamingException
 482  
     {
 483  
 
 484  0
         if (name == null || name.length() == 0)
 485  
         {
 486  0
             throw new NamingException("Invalid Name " + name);
 487  
         }
 488  0
         if (frozen)
 489  
         {
 490  0
             throw new NamingException("Read only");
 491  
         }
 492  
 
 493  0
         Map newBindings = new HashMap();
 494  0
         int pos = name.indexOf('/');
 495  0
         if (pos == -1)
 496  
         {
 497  0
             Object oldValue = treeBindings.put(name, value);
 498  0
             if (!allowRebind && oldValue != null)
 499  
             {
 500  0
                 throw new NamingException("Something already bound at " + name);
 501  
             }
 502  0
             bindings.put(name, value);
 503  0
             newBindings.put(name, value);
 504  0
         }
 505  
         else
 506  
         {
 507  0
             String segment = name.substring(0, pos);
 508  
 
 509  0
             if (segment == null || segment.length() == 0)
 510  
             {
 511  0
                 throw new NamingException("Invalid segment " + segment);
 512  
             }
 513  0
             Object o = treeBindings.get(segment);
 514  0
             if (o == null)
 515  
             {
 516  0
                 o = newContext();
 517  0
                 treeBindings.put(segment, o);
 518  0
                 bindings.put(segment, o);
 519  0
                 newBindings.put(segment, o);
 520  
             }
 521  0
             else if (!(o instanceof DefaultContext))
 522  
             {
 523  0
                 throw new NamingException("Something already bound where a subcontext should go");
 524  
             }
 525  0
             DefaultContext defaultContext = (DefaultContext) o;
 526  0
             String remainder = name.substring(pos + 1);
 527  0
             Map subBindings = defaultContext.internalBind(remainder, value, allowRebind);
 528  0
             for (Iterator iterator = subBindings.entrySet().iterator(); iterator.hasNext();)
 529  
             {
 530  0
                 Map.Entry entry = (Map.Entry) iterator.next();
 531  0
                 String subName = segment + "/" + (String) entry.getKey();
 532  0
                 Object bound = entry.getValue();
 533  0
                 treeBindings.put(subName, bound);
 534  0
                 newBindings.put(subName, bound);
 535  0
             }
 536  
         }
 537  0
         return newBindings;
 538  
     }
 539  
 
 540  
     protected void checkFrozen() throws OperationNotSupportedException
 541  
     {
 542  0
         if (isFreeze())
 543  
         {
 544  0
             throw new OperationNotSupportedException("JNDI context is frozen!");
 545  
         }
 546  0
     }
 547  
 
 548  
     protected DefaultContext newContext()
 549  
     {
 550  0
         return new DefaultContext();
 551  
     }
 552  
 
 553  
 }