Coverage Report - org.apache.tapestry5.internal.transform.AbstractIncludeAssetWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractIncludeAssetWorker
100%
17/17
100%
2/2
0
AbstractIncludeAssetWorker$1
100%
7/7
100%
2/2
0
 
 1  
 // Copyright 2007, 2008, 2009 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.tapestry5.internal.transform;
 16  
 
 17  
 import org.apache.tapestry5.Asset;
 18  
 import org.apache.tapestry5.ComponentResources;
 19  
 import org.apache.tapestry5.annotations.SetupRender;
 20  
 import org.apache.tapestry5.internal.services.ComponentResourcesOperation;
 21  
 import org.apache.tapestry5.ioc.Resource;
 22  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 23  
 import org.apache.tapestry5.ioc.services.SymbolSource;
 24  
 import org.apache.tapestry5.model.MutableComponentModel;
 25  
 import org.apache.tapestry5.services.AssetSource;
 26  
 import org.apache.tapestry5.services.ClassTransformation;
 27  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 28  
 import org.apache.tapestry5.services.TransformConstants;
 29  
 
 30  
 import java.util.List;
 31  
 import java.util.Locale;
 32  
 
 33  
 /**
 34  
  * Base class for workers that automatically inlcude assets in the page (via methods on {@link
 35  
  * org.apache.tapestry5.RenderSupport}).
 36  
  */
 37  2074
 public abstract class AbstractIncludeAssetWorker implements ComponentClassTransformWorker
 38  
 {
 39  
     private final AssetSource assetSource;
 40  
 
 41  
     private final SymbolSource symbolSource;
 42  
 
 43  
     public AbstractIncludeAssetWorker(AssetSource assetSource, SymbolSource symbolSource)
 44  116
     {
 45  116
         this.assetSource = assetSource;
 46  116
         this.symbolSource = symbolSource;
 47  116
     }
 48  
 
 49  
     /**
 50  
      * Expands symbols in the path, then adds an operation into the setup render phase of the component. Ultimately,
 51  
      * {@link #handleAsset(org.apache.tapestry5.Asset)} will be invoked for each asset (dervied from assetPaths).
 52  
      *
 53  
      * @param transformation transformation process for component
 54  
      * @param model          component model for component
 55  
      * @param assetPaths     raw paths to be converted to assets
 56  
      */
 57  
     protected final void addOperationForAssetPaths(ClassTransformation transformation,
 58  
                                                    MutableComponentModel model, String[] assetPaths)
 59  
     {
 60  20
         final Resource baseResource = model.getBaseResource();
 61  20
         final List<String> paths = CollectionFactory.newList();
 62  
 
 63  48
         for (String value : assetPaths)
 64  
         {
 65  28
             String expanded = symbolSource.expandSymbols(value);
 66  
 
 67  28
             paths.add(expanded);
 68  
         }
 69  
 
 70  20
         ComponentResourcesOperation op = new ComponentResourcesOperation()
 71  
         {
 72  
             // Remember that ONE instances of this op will be injected into EVERY instance
 73  
             // of the component ... that means that we can't do any aggresive caching
 74  
             // inside the operation (the operation must be threadsafe).
 75  
 
 76  20
             public void perform(ComponentResources resources)
 77  
             {
 78  1110
                 Locale locale = resources.getLocale();
 79  
 
 80  1110
                 for (String assetPath : paths)
 81  
                 {
 82  2074
                     Asset asset = assetSource.getAsset(baseResource, assetPath, locale);
 83  
 
 84  2074
                     handleAsset(asset);
 85  2074
                 }
 86  1110
             }
 87  
         };
 88  
 
 89  20
         String opFieldName = transformation.addInjectedField(ComponentResourcesOperation.class, "operation", op);
 90  
 
 91  20
         String resourcesName = transformation.getResourcesFieldName();
 92  
 
 93  20
         String body = String.format("%s.perform(%s);", opFieldName, resourcesName);
 94  
 
 95  
         // This is what I like about this approach; the injected body is tiny.  The downside is that
 96  
         // the object that gets injected is hard to test, hard enough that we'll just concentrate on
 97  
         // the integration test, thank you.
 98  
 
 99  20
         transformation.extendMethod(TransformConstants.SETUP_RENDER_SIGNATURE, body);
 100  
 
 101  20
         model.addRenderPhase(SetupRender.class);
 102  20
     }
 103  
 
 104  
     /**
 105  
      * Invoked, from the component's setup render phase, for each asset. This method must be threadsafe.  Most
 106  
      * implementation pass the asset to a particular method of {@link org.apache.tapestry5.RenderSupport}.
 107  
      *
 108  
      * @param asset to be processed
 109  
      */
 110  
     protected abstract void handleAsset(Asset asset);
 111  
 }