Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 399   Methods: 27
NCLOC: 236   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
Namespace.java 66.7% 65% 70.4% 66.3%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.engine;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.Collections;
 19   
 import java.util.HashMap;
 20   
 import java.util.HashSet;
 21   
 import java.util.List;
 22   
 import java.util.Map;
 23   
 import java.util.Set;
 24   
 
 25   
 import org.apache.hivemind.util.ClasspathResource;
 26   
 import org.apache.hivemind.ApplicationRuntimeException;
 27   
 import org.apache.hivemind.ClassResolver;
 28   
 import org.apache.hivemind.Location;
 29   
 import org.apache.hivemind.Resource;
 30   
 import org.apache.tapestry.INamespace;
 31   
 import org.apache.tapestry.Tapestry;
 32   
 import org.apache.tapestry.spec.IComponentSpecification;
 33   
 import org.apache.tapestry.spec.ILibrarySpecification;
 34   
 
 35   
 /**
 36   
  * Implementation of {@link org.apache.tapestry.INamespace}that works with a
 37   
  * {@link ISpecificationSource}to obtain page and component specifications as needed.
 38   
  * 
 39   
  * @author Howard Lewis Ship
 40   
  * @since 2.2
 41   
  */
 42   
 
 43   
 public class Namespace implements INamespace
 44   
 {
 45   
     private ILibrarySpecification _specification;
 46   
 
 47   
     private ISpecificationSource _specificationSource;
 48   
 
 49   
     private String _id;
 50   
 
 51   
     private String _extendedId;
 52   
 
 53   
     private INamespace _parent;
 54   
 
 55   
     private boolean _frameworkNamespace;
 56   
 
 57   
     private boolean _applicationNamespace;
 58   
 
 59   
     /** @since 3.1 */
 60   
 
 61   
     private ClassResolver _resolver;
 62   
 
 63   
     /**
 64   
      * Map of {@link org.apache.tapestry.spec.ComponentSpecification}keyed on page name. The map is
 65   
      * synchronized because different threads may try to update it simultaneously (due to dynamic
 66   
      * page discovery in the application namespace).
 67   
      */
 68   
 
 69   
     private Map _pages = Collections.synchronizedMap(new HashMap());
 70   
 
 71   
     /**
 72   
      * Map of {@link org.apache.tapestry.spec.ComponentSpecification}keyed on component alias.
 73   
      */
 74   
 
 75   
     private Map _components = Collections.synchronizedMap(new HashMap());
 76   
 
 77   
     /**
 78   
      * Map, keyed on id, of {@link INamespace}.
 79   
      */
 80   
 
 81   
     private Map _children = Collections.synchronizedMap(new HashMap());
 82   
 
 83  143
     public Namespace(String id, INamespace parent, ILibrarySpecification specification,
 84   
             ISpecificationSource specificationSource, ClassResolver resolver)
 85   
     {
 86  143
         _id = id;
 87  143
         _parent = parent;
 88  143
         _specification = specification;
 89  143
         _specificationSource = specificationSource;
 90  143
         _resolver = resolver;
 91   
 
 92  143
         _applicationNamespace = (_id == null);
 93  143
         _frameworkNamespace = FRAMEWORK_NAMESPACE.equals(_id);
 94   
     }
 95   
 
 96  0
     public String toString()
 97   
     {
 98  0
         StringBuffer buffer = new StringBuffer("Namespace@");
 99  0
         buffer.append(Integer.toHexString(hashCode()));
 100  0
         buffer.append('[');
 101   
 
 102  0
         if (_applicationNamespace)
 103  0
             buffer.append("<application>");
 104   
         else
 105  0
             buffer.append(getExtendedId());
 106   
 
 107  0
         buffer.append(']');
 108   
 
 109  0
         return buffer.toString();
 110   
     }
 111   
 
 112  172
     public String getId()
 113   
     {
 114  172
         return _id;
 115   
     }
 116   
 
 117  291
     public String getExtendedId()
 118   
     {
 119  291
         if (_applicationNamespace)
 120  196
             return null;
 121   
 
 122  95
         if (_extendedId == null)
 123  43
             _extendedId = buildExtendedId();
 124   
 
 125  95
         return _extendedId;
 126   
     }
 127   
 
 128  0
     public INamespace getParentNamespace()
 129   
     {
 130  0
         return _parent;
 131   
     }
 132   
 
 133  183
     public INamespace getChildNamespace(String id)
 134   
     {
 135  183
         String firstId = id;
 136  183
         String nextIds = null;
 137   
 
 138   
         // Split the id into first and next if it is a dot separated sequence
 139  183
         int index = id.indexOf('.');
 140  183
         if (index >= 0)
 141   
         {
 142  0
             firstId = id.substring(0, index);
 143  0
             nextIds = id.substring(index + 1);
 144   
         }
 145   
 
 146   
         // Get the first namespace
 147  183
         INamespace result = (INamespace) _children.get(firstId);
 148   
 
 149  183
         if (result == null)
 150   
         {
 151  23
             result = createNamespace(firstId);
 152   
 
 153  23
             _children.put(firstId, result);
 154   
         }
 155   
 
 156   
         // If the id is a dot separated sequence, recurse to find
 157   
         // the needed namespace
 158  183
         if (result != null && nextIds != null)
 159  0
             result = result.getChildNamespace(nextIds);
 160   
 
 161  183
         return result;
 162   
     }
 163   
 
 164  0
     public List getChildIds()
 165   
     {
 166  0
         return _specification.getLibraryIds();
 167   
     }
 168   
 
 169  93
     public IComponentSpecification getPageSpecification(String name)
 170   
     {
 171  93
         IComponentSpecification result = (IComponentSpecification) _pages.get(name);
 172   
 
 173  93
         if (result == null)
 174   
         {
 175  59
             result = locatePageSpecification(name);
 176   
 
 177  59
             _pages.put(name, result);
 178   
         }
 179   
 
 180  93
         return result;
 181   
     }
 182   
 
 183  0
     public List getPageNames()
 184   
     {
 185  0
         Set names = new HashSet();
 186   
 
 187  0
         names.addAll(_pages.keySet());
 188  0
         names.addAll(_specification.getPageNames());
 189   
 
 190  0
         List result = new ArrayList(names);
 191   
 
 192  0
         Collections.sort(result);
 193   
 
 194  0
         return result;
 195   
     }
 196   
 
 197  1636
     public IComponentSpecification getComponentSpecification(String alias)
 198   
     {
 199  1636
         IComponentSpecification result = (IComponentSpecification) _components.get(alias);
 200   
 
 201  1636
         if (result == null)
 202   
         {
 203  376
             result = locateComponentSpecification(alias);
 204  376
             _components.put(alias, result);
 205   
         }
 206   
 
 207  1636
         return result;
 208   
     }
 209   
 
 210  0
     public String getServiceClassName(String name)
 211   
     {
 212  0
         return _specification.getServiceClassName(name);
 213   
     }
 214   
 
 215  0
     public List getServiceNames()
 216   
     {
 217  0
         return _specification.getServiceNames();
 218   
     }
 219   
 
 220  95
     public ILibrarySpecification getSpecification()
 221   
     {
 222  95
         return _specification;
 223   
     }
 224   
 
 225  43
     private String buildExtendedId()
 226   
     {
 227  43
         if (_parent == null)
 228  26
             return _id;
 229   
 
 230  17
         String parentId = _parent.getExtendedId();
 231   
 
 232   
         // If immediate child of application namespace
 233   
 
 234  17
         if (parentId == null)
 235  17
             return _id;
 236   
 
 237  0
         return parentId + "." + _id;
 238   
     }
 239   
 
 240   
     /**
 241   
      * Returns a string identifying the namespace, for use in error messages. I.e., "Application
 242   
      * namespace" or "namespace 'foo'".
 243   
      */
 244   
 
 245  3
     public String getNamespaceId()
 246   
     {
 247  3
         if (_frameworkNamespace)
 248  0
             return Tapestry.getMessage("Namespace.framework-namespace");
 249   
 
 250  3
         if (_applicationNamespace)
 251  3
             return Tapestry.getMessage("Namespace.application-namespace");
 252   
 
 253  0
         return Tapestry.format("Namespace.nested-namespace", getExtendedId());
 254   
     }
 255   
 
 256   
     /**
 257   
      * Gets the specification from the specification source.
 258   
      * 
 259   
      * @throws ApplicationRuntimeException
 260   
      *             if the named page is not defined.
 261   
      */
 262   
 
 263  59
     private IComponentSpecification locatePageSpecification(String name)
 264   
     {
 265  59
         String path = _specification.getPageSpecificationPath(name);
 266   
 
 267  59
         if (path == null)
 268  0
             throw new ApplicationRuntimeException(Tapestry.format(
 269   
                     "Namespace.no-such-page",
 270   
                     name,
 271   
                     getNamespaceId()));
 272   
 
 273  59
         Resource location = getSpecificationLocation().getRelativeResource(path);
 274   
 
 275  59
         return _specificationSource.getPageSpecification(location);
 276   
     }
 277   
 
 278  376
     private IComponentSpecification locateComponentSpecification(String type)
 279   
     {
 280  376
         String path = _specification.getComponentSpecificationPath(type);
 281   
 
 282  376
         if (path == null)
 283  0
             throw new ApplicationRuntimeException(Tapestry.format(
 284   
                     "Namespace.no-such-alias",
 285   
                     type,
 286   
                     getNamespaceId()));
 287   
 
 288  376
         Resource location = getSpecificationLocation().getRelativeResource(path);
 289   
 
 290  376
         return _specificationSource.getComponentSpecification(location);
 291   
     }
 292   
 
 293  23
     private INamespace createNamespace(String id)
 294   
     {
 295  23
         String path = _specification.getLibrarySpecificationPath(id);
 296   
 
 297  23
         if (path == null)
 298  0
             throw new ApplicationRuntimeException(Tapestry.format(
 299   
                     "Namespace.library-id-not-found",
 300   
                     id,
 301   
                     getNamespaceId()));
 302   
 
 303  23
         Resource location = getSpecificationLocation().getRelativeResource(path);
 304   
 
 305   
         // Ok, an absolute path to a library for an application whose specification
 306   
         // is in the context root is problematic, cause getRelativeLocation()
 307   
         // will still be looking in the context. Handle this case with the
 308   
         // following little kludge:
 309   
 
 310  23
         if (location.getResourceURL() == null && path.startsWith("/"))
 311  1
             location = new ClasspathResource(_resolver, path);
 312   
 
 313  23
         ILibrarySpecification ls = _specificationSource.getLibrarySpecification(location);
 314   
 
 315  23
         return new Namespace(id, this, ls, _specificationSource, _resolver);
 316   
     }
 317   
 
 318  228
     public boolean containsPage(String name)
 319   
     {
 320  228
         return _pages.containsKey(name) || (_specification.getPageSpecificationPath(name) != null);
 321   
     }
 322   
 
 323   
     /** @since 2.3 * */
 324   
 
 325  179
     public String constructQualifiedName(String pageName)
 326   
     {
 327  179
         String prefix = getExtendedId();
 328   
 
 329  179
         if (prefix == null)
 330  127
             return pageName;
 331   
 
 332  52
         return prefix + SEPARATOR + pageName;
 333   
     }
 334   
 
 335   
     /** @since 3.0 * */
 336   
 
 337  1790
     public Resource getSpecificationLocation()
 338   
     {
 339  1790
         return _specification.getSpecificationLocation();
 340   
     }
 341   
 
 342   
     /** @since 3.0 * */
 343   
 
 344  965
     public boolean isApplicationNamespace()
 345   
     {
 346  965
         return _applicationNamespace;
 347   
     }
 348   
 
 349   
     /** @since 3.0 * */
 350   
 
 351  84
     public synchronized void installPageSpecification(String pageName,
 352   
             IComponentSpecification specification)
 353   
     {
 354  84
         _pages.put(pageName, specification);
 355   
     }
 356   
 
 357   
     /** @since 3.0 * */
 358   
 
 359  13
     public synchronized void installComponentSpecification(String type,
 360   
             IComponentSpecification specification)
 361   
     {
 362  13
         _components.put(type, specification);
 363   
     }
 364   
 
 365   
     /** @since 3.0 * */
 366   
 
 367  2478
     public boolean containsComponentType(String type)
 368   
     {
 369  2478
         return _components.containsKey(type)
 370   
                 || (_specification.getComponentSpecificationPath(type) != null);
 371   
     }
 372   
 
 373   
     /** @since 3.0 * */
 374   
 
 375  0
     public List getComponentTypes()
 376   
     {
 377  0
         Set types = new HashSet();
 378   
 
 379  0
         types.addAll(_components.keySet());
 380  0
         types.addAll(_specification.getComponentTypes());
 381   
 
 382  0
         List result = new ArrayList(types);
 383   
 
 384  0
         Collections.sort(result);
 385   
 
 386  0
         return result;
 387   
     }
 388   
 
 389   
     /** @since 3.0 * */
 390   
 
 391  0
     public Location getLocation()
 392   
     {
 393  0
         if (_specification == null)
 394  0
             return null;
 395   
 
 396  0
         return _specification.getLocation();
 397   
     }
 398   
 
 399   
 }