Coverage Report - org.apache.tiles.request.portlet.wildcard.WildcardPortletApplicationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
WildcardPortletApplicationContext
67%
19/28
42%
6/14
3.2
 
 1  
 /*
 2  
  * $Id: WildcardPortletApplicationContext.java 1306435 2012-03-28 15:39:11Z nlebas $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 package org.apache.tiles.request.portlet.wildcard;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.net.URL;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 import java.util.Collections;
 29  
 import java.util.Locale;
 30  
 
 31  
 import javax.portlet.PortletContext;
 32  
 
 33  
 import org.apache.tiles.request.ApplicationResource;
 34  
 import org.apache.tiles.request.locale.URLApplicationResource;
 35  
 import org.apache.tiles.request.portlet.PortletApplicationContext;
 36  
 import org.springframework.core.io.Resource;
 37  
 import org.springframework.core.io.support.ResourcePatternResolver;
 38  
 import org.springframework.web.portlet.context.PortletContextResourcePatternResolver;
 39  
 
 40  
 /**
 41  
  * Portlet-based implementation of the TilesApplicationContext interface that
 42  
  * can resolve resources even using wildcards.
 43  
  *
 44  
  * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
 45  
  */
 46  
 public class WildcardPortletApplicationContext extends PortletApplicationContext {
 47  
 
 48  
     /**
 49  
      * The pattern resolver.
 50  
      */
 51  
     protected ResourcePatternResolver resolver;
 52  
 
 53  
     /**
 54  
      * Constructor.
 55  
      *
 56  
      * @param portletContext The portlet context.
 57  
      */
 58  
     public WildcardPortletApplicationContext(PortletContext portletContext) {
 59  1
         super(portletContext);
 60  1
     }
 61  
 
 62  
     /** {@inheritDoc} */
 63  
     @Override
 64  
     public void initialize(PortletContext context) {
 65  1
         super.initialize(context);
 66  
 
 67  1
         resolver = new PortletContextResourcePatternResolver(context);
 68  1
     }
 69  
 
 70  
     /** {@inheritDoc} */
 71  
     @Override
 72  
     public ApplicationResource getResource(String localePath) {
 73  2
         ApplicationResource retValue = null;
 74  2
         Collection<ApplicationResource> resourceSet = getResources(localePath);
 75  2
         if (resourceSet != null && !resourceSet.isEmpty()) {
 76  2
             retValue = resourceSet.iterator().next();
 77  
         }
 78  2
         return retValue;
 79  
     }
 80  
 
 81  
     /** {@inheritDoc} */
 82  
     @Override
 83  
     public ApplicationResource getResource(ApplicationResource base, Locale locale) {
 84  0
         ApplicationResource retValue = null;
 85  0
         Collection<ApplicationResource> resourceSet = getResources(base.getLocalePath(locale));
 86  0
         if (resourceSet != null && !resourceSet.isEmpty()) {
 87  0
             retValue = resourceSet.iterator().next();
 88  
         }
 89  0
         return retValue;
 90  
     }
 91  
 
 92  
     /** {@inheritDoc} */
 93  
     @Override
 94  
     public Collection<ApplicationResource> getResources(String path) {
 95  
         Resource[] resources;
 96  
         try {
 97  4
             resources = resolver.getResources(path);
 98  0
         } catch (IOException e) {
 99  0
             return Collections.<ApplicationResource> emptyList();
 100  4
         }
 101  4
         Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>();
 102  4
         if (resources != null && resources.length > 0) {
 103  10
             for (int i = 0; i < resources.length; i++) {
 104  
                 URL url;
 105  
                 try {
 106  6
                     url = resources[i].getURL();
 107  6
                     resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
 108  0
                 } catch (IOException e) {
 109  
                     // shouldn't happen with the kind of resources we're using
 110  0
                     throw new IllegalArgumentException("no URL for " + resources[i].toString(), e);
 111  6
                 }
 112  
             }
 113  
         }
 114  4
         return resourceList;
 115  
     }
 116  
 }