Coverage Report - org.apache.tapestry.resolver.ComponentResourceResolverImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentResourceResolverImpl
0%
0/66
0%
0/48
4.556
 
 1  
 package org.apache.tapestry.resolver;
 2  
 
 3  
 import org.apache.hivemind.Resource;
 4  
 import org.apache.hivemind.util.ContextResource;
 5  
 import org.apache.tapestry.IComponent;
 6  
 import org.apache.tapestry.IRequestCycle;
 7  
 import org.apache.tapestry.TapestryUtils;
 8  
 import org.apache.tapestry.asset.AssetFactory;
 9  
 import org.apache.tapestry.web.WebContextResource;
 10  
 
 11  
 import java.util.Locale;
 12  
 
 13  
 /**
 14  
  * Implementation of {@link IComponentResourceResolver}.
 15  
  */
 16  0
 public class ComponentResourceResolverImpl implements IComponentResourceResolver {
 17  
 
 18  
     /**
 19  
      * Used to find resources under the context WEB-INF/ directory.
 20  
      */
 21  
     private static final String WEB_INF = "WEB-INF/";
 22  
 
 23  
     private AssetFactory _classpathAssetFactory;
 24  
     private AssetFactory _contextAssetFactory;
 25  
 
 26  
     /** Application id of the current tapestry app - as in applicationId.application */
 27  
     private String _applicationId;
 28  
     
 29  
     /** Root web context directory */
 30  
     private Resource _contextRoot;
 31  
     /** The location of the WEB-INF context directory */
 32  
     private Resource _webInfLocation;
 33  
     /** The location of the application within the WEB-INF/ folder */
 34  
     private Resource _webInfAppLocation;
 35  
 
 36  
     /**
 37  
      * Called by hivemind automatically.
 38  
      */
 39  
     public void initializeService()
 40  
     {
 41  0
         _webInfLocation = _contextRoot.getRelativeResource(WEB_INF);
 42  
         
 43  0
         _webInfAppLocation = _webInfLocation.getRelativeResource(_applicationId + "/");
 44  0
     }
 45  
 
 46  
     public Resource findComponentResource(IComponent component, IRequestCycle cycle, String path, String extension, Locale locale)
 47  
     {
 48  0
         Resource base = component.getSpecification().getSpecificationLocation();
 49  0
         String baseName = path == null ? extractBaseName(base) : path;
 50  0
         Resource resource = null;
 51  
 
 52  
         // have to do explicit check for context resource first
 53  
         // as it might be a classpath based spec and then we need to manually figure out
 54  
         // the best location to start from as context paths always get resolved first
 55  
         // before classpath resources
 56  
         
 57  0
         if (WebContextResource.class.isInstance(base) || ContextResource.class.isInstance(base))
 58  
         {
 59  0
             resource = base.getRelativeResource(baseName + extension);
 60  
 
 61  0
             if (resource != null)
 62  0
                 return localizeResource(resource, locale);
 63  
         }
 64  
 
 65  0
         resource = findComponentClassResource(component, cycle, baseName, extension, locale);
 66  
 
 67  
         // In some cases the generic classpath resource path is fine - such as bundled component properties
 68  
 
 69  0
         if (resource == null)
 70  
         {    
 71  0
             resource = base.getRelativeResource(baseName + extension);
 72  
             
 73  0
             if (resource != null)
 74  0
                 resource = localizeResource(resource, locale);
 75  
         }
 76  
 
 77  0
         return resource;
 78  
     }
 79  
 
 80  
     String extractBaseName(Resource baseResourceLocation)
 81  
     {
 82  0
         String fileName = baseResourceLocation.getName();
 83  0
         int dotx = fileName.lastIndexOf('.');
 84  
 
 85  0
         return dotx > -1 ? fileName.substring(0, dotx) : fileName;
 86  
     }
 87  
 
 88  
     Resource localizeResource(Resource resource, Locale locale)
 89  
     {
 90  0
         if (locale == null)
 91  0
             return resource;
 92  
 
 93  0
         Resource localized = resource.getLocalization(locale);
 94  0
         if (localized != null && localized.getResourceURL() != null)
 95  0
             return localized;
 96  
 
 97  0
         return resource;
 98  
     }
 99  
 
 100  
     Resource findComponentClassResource(IComponent component, IRequestCycle cycle, String baseName, String extension, Locale locale)
 101  
     {
 102  0
         Resource base = component.getSpecification().getSpecificationLocation();
 103  0
         String componentPackages = component.getNamespace().getPropertyValue("org.apache.tapestry.component-class-packages");
 104  
 
 105  
         // this relies on finding things from the component class name
 106  
 
 107  0
         if (componentPackages == null)
 108  0
             return null;
 109  
 
 110  0
         String className = component.getSpecification().getComponentClassName();
 111  0
         if (className == null)
 112  0
             return null;
 113  
 
 114  0
         String[] packages = TapestryUtils.split(componentPackages);
 115  0
         for (int i=0; i < packages.length; i++)
 116  
         {
 117  
             // find matching package in class
 118  0
             int index = className.lastIndexOf(packages[i]);
 119  0
             if (index < 0)
 120  0
                 continue;
 121  
 
 122  
             // First try context
 123  
 
 124  0
             String templateName = className.substring((index + packages[i].length()) + 1, className.length()).replaceAll("\\.", "/");
 125  0
             templateName =  templateName + extension;
 126  
 
 127  0
             if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfAppLocation, templateName, locale))
 128  
             {
 129  0
                 return _contextAssetFactory.createAsset(_webInfAppLocation, component.getSpecification(),  templateName, locale, component.getLocation()).getResourceLocation();
 130  0
             } else if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfLocation, templateName, locale))
 131  
             {
 132  0
                 return _contextAssetFactory.createAsset(_webInfLocation, component.getSpecification(), templateName, locale, component.getLocation()).getResourceLocation();
 133  
             }
 134  
 
 135  
             // else classpath
 136  
 
 137  0
             String resourceName = baseName + extension;
 138  
 
 139  0
             if (_classpathAssetFactory.assetExists(component.getSpecification(), base, resourceName, locale))
 140  
             {
 141  0
                 return _classpathAssetFactory.createAsset(base, component.getSpecification(), resourceName, locale, component.getLocation()).getResourceLocation();
 142  
             }
 143  
 
 144  
             // if all else fails try package name context paths
 145  
 
 146  0
             String[] packageSegments = packages[i].split("\\.");
 147  
             
 148  0
             if (packageSegments != null && packageSegments.length > 0)
 149  
             {
 150  
                 // start with last segment and slowly build the path up with all of them
 151  0
                 String packagePath = "";
 152  
 
 153  0
                 for (int s=packageSegments.length - 1; s > -1; s--)
 154  
                 {
 155  0
                     packagePath += packageSegments[s] + "/";
 156  
 
 157  0
                     String templatePath = packagePath + templateName;
 158  
                     
 159  0
                     if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfAppLocation, templatePath, locale))
 160  
                     {
 161  0
                         return _contextAssetFactory.createAsset(_webInfAppLocation, component.getSpecification(),  templatePath, locale, component.getLocation()).getResourceLocation();
 162  0
                     } else if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfLocation, templatePath, locale))
 163  
                     {
 164  0
                         return _contextAssetFactory.createAsset(_webInfLocation, component.getSpecification(), templatePath, locale, component.getLocation()).getResourceLocation();
 165  
                     }
 166  
                 }
 167  
             }
 168  
         }
 169  
 
 170  0
         return null;
 171  
     }
 172  
 
 173  
     public void setContextRoot(Resource contextRoot)
 174  
     {
 175  0
         _contextRoot = contextRoot;
 176  0
     }
 177  
 
 178  
     public void setClasspathAssetFactory(AssetFactory classpathAssetFactory)
 179  
     {
 180  0
         _classpathAssetFactory = classpathAssetFactory;
 181  0
     }
 182  
 
 183  
     public void setContextAssetFactory(AssetFactory contextAssetFactory)
 184  
     {
 185  0
         _contextAssetFactory = contextAssetFactory;
 186  0
     }
 187  
 
 188  
     public void setApplicationId(String applicationId)
 189  
     {
 190  0
         _applicationId = applicationId;
 191  0
     }
 192  
 }