Coverage Report - org.apache.tapestry.portlet.PortletApplicationSpecificationInitializer
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletApplicationSpecificationInitializer
0%
0/26
0%
0/6
1.714
 
 1  
 // Copyright 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.portlet;
 16  
 
 17  
 import javax.portlet.PortletConfig;
 18  
 
 19  
 import org.apache.hivemind.Resource;
 20  
 import org.apache.tapestry.parse.ISpecificationParser;
 21  
 import org.apache.tapestry.services.ApplicationGlobals;
 22  
 import org.apache.tapestry.spec.ApplicationSpecification;
 23  
 import org.apache.tapestry.spec.IApplicationSpecification;
 24  
 import org.apache.tapestry.web.WebContext;
 25  
 import org.apache.tapestry.web.WebContextResource;
 26  
 
 27  
 /**
 28  
  * Locates and reads the application specification for the portlet and stores it
 29  
  * into {@link org.apache.tapestry.services.ApplicationGlobals}.
 30  
  * <p>
 31  
  * TODO: Merge this code with
 32  
  * {@link org.apache.tapestry.services.impl.ApplicationSpecificationInitializer},
 33  
  * they're very similar. This would probably be an additional service that would
 34  
  * do the lookup based on the {@link org.apache.tapestry.web.WebActivator}&nbsp;and
 35  
  * the {@link org.apache.tapestry.web.WebContext}.
 36  
  * 
 37  
  * @author Howard M. Lewis Ship
 38  
  * @since 4.0
 39  
  * @see org.apache.tapestry.services.impl.ApplicationSpecificationInitializer
 40  
  */
 41  0
 public class PortletApplicationSpecificationInitializer implements
 42  
         PortletApplicationInitializer
 43  
 {
 44  
 
 45  
     private WebContext _context;
 46  
 
 47  
     private ApplicationGlobals _globals;
 48  
 
 49  
     private ISpecificationParser _parser;
 50  
 
 51  
     public void initialize(PortletConfig portletConfig)
 52  
     {
 53  0
         String name = portletConfig.getPortletName();
 54  
 
 55  0
         Resource resource = findApplicationSpecification(name);
 56  
 
 57  0
         IApplicationSpecification specification = resource == null ? constructStandinSpecification(name)
 58  
                 : _parser.parseApplicationSpecification(resource);
 59  
 
 60  0
         _globals.storeSpecification(specification);
 61  0
     }
 62  
 
 63  
     private Resource findApplicationSpecification(String name)
 64  
     {
 65  0
         String expectedName = name + ".application";
 66  
 
 67  0
         Resource webInfLocation = new WebContextResource(_context, "/WEB-INF/");
 68  0
         Resource webInfAppLocation = webInfLocation.getRelativeResource(name
 69  
                 + "/");
 70  
 
 71  0
         Resource result = check(webInfAppLocation, expectedName);
 72  0
         if (result != null) return result;
 73  
 
 74  0
         return check(webInfLocation, expectedName);
 75  
     }
 76  
 
 77  
     private Resource check(Resource resource, String name)
 78  
     {
 79  0
         Resource result = resource.getRelativeResource(name);
 80  
 
 81  0
         if (result.getResourceURL() != null) return result;
 82  
 
 83  0
         return null;
 84  
     }
 85  
 
 86  
     private IApplicationSpecification constructStandinSpecification(String name)
 87  
     {
 88  0
         ApplicationSpecification result = new ApplicationSpecification();
 89  
 
 90  
         // Pretend the file exists in the most common expected location.
 91  
 
 92  0
         Resource virtualLocation = new WebContextResource(_context, "/WEB-INF/"
 93  
                 + name + ".application");
 94  
 
 95  0
         result.setSpecificationLocation(virtualLocation);
 96  
 
 97  0
         result.setName(name);
 98  
 
 99  0
         return result;
 100  
     }
 101  
 
 102  
     public void setContext(WebContext context)
 103  
     {
 104  0
         _context = context;
 105  0
     }
 106  
 
 107  
     public void setGlobals(ApplicationGlobals globals)
 108  
     {
 109  0
         _globals = globals;
 110  0
     }
 111  
 
 112  
     public void setParser(ISpecificationParser parser)
 113  
     {
 114  0
         _parser = parser;
 115  0
     }
 116  
 }