Coverage Report - org.apache.tiles.request.servlet.ServletApplicationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletApplicationContext
81%
22/27
58%
7/12
2.714
 
 1  
 /*
 2  
  * $Id: ServletApplicationContext.java 1297705 2012-03-06 20:44:30Z 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  
 package org.apache.tiles.request.servlet;
 22  
 
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collection;
 27  
 import java.util.Locale;
 28  
 import java.util.Map;
 29  
 
 30  
 import javax.servlet.ServletContext;
 31  
 
 32  
 import org.apache.tiles.request.ApplicationContext;
 33  
 import org.apache.tiles.request.ApplicationResource;
 34  
 import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
 35  
 import org.apache.tiles.request.collection.ScopeMap;
 36  
 import org.apache.tiles.request.locale.URLApplicationResource;
 37  
 import org.apache.tiles.request.servlet.extractor.ApplicationScopeExtractor;
 38  
 import org.apache.tiles.request.servlet.extractor.InitParameterExtractor;
 39  
 
 40  
 /**
 41  
  * Servlet-based implementation of the TilesApplicationContext interface.
 42  
  *
 43  
  * @version $Rev: 1297705 $ $Date: 2012-03-07 07:44:30 +1100 (Wed, 07 Mar 2012) $
 44  
  */
 45  
 public class ServletApplicationContext implements ApplicationContext {
 46  
 
 47  
     /**
 48  
      * The servlet context to use.
 49  
      */
 50  
     private ServletContext servletContext;
 51  
 
 52  
     /**
 53  
      * <p>The lazily instantiated <code>Map</code> of application scope
 54  
      * attributes.</p>
 55  
      */
 56  5
     private Map<String, Object> applicationScope = null;
 57  
 
 58  
     /**
 59  
      * <p>The lazily instantiated <code>Map</code> of context initialization
 60  
      * parameters.</p>
 61  
      */
 62  5
     private Map<String, String> initParam = null;
 63  
 
 64  
     /**
 65  
      * Creates a new instance of ServletTilesApplicationContext.
 66  
      *
 67  
      * @param servletContext The servlet context to use.
 68  
      */
 69  5
     public ServletApplicationContext(ServletContext servletContext) {
 70  5
         this.servletContext = servletContext;
 71  5
     }
 72  
 
 73  
     /** {@inheritDoc} */
 74  
     public Object getContext() {
 75  1
         return servletContext;
 76  
     }
 77  
 
 78  
     /** {@inheritDoc} */
 79  
     public Map<String, Object> getApplicationScope() {
 80  
 
 81  1
         if ((applicationScope == null) && (servletContext != null)) {
 82  1
             applicationScope = new ScopeMap(new ApplicationScopeExtractor(servletContext));
 83  
         }
 84  1
         return (applicationScope);
 85  
 
 86  
     }
 87  
 
 88  
     /** {@inheritDoc} */
 89  
     public Map<String, String> getInitParams() {
 90  
 
 91  1
         if ((initParam == null) && (servletContext != null)) {
 92  1
             initParam = new ReadOnlyEnumerationMap<String>(new InitParameterExtractor(servletContext));
 93  
         }
 94  1
         return (initParam);
 95  
 
 96  
     }
 97  
 
 98  
     /** {@inheritDoc} */
 99  
     public ApplicationResource getResource(String localePath) {
 100  
         try {
 101  3
             URL url = servletContext.getResource(localePath);
 102  3
             if (url != null) {
 103  2
                 return new URLApplicationResource(localePath, url);
 104  
             } else {
 105  1
                 return null;
 106  
             }
 107  0
         } catch (MalformedURLException e) {
 108  0
             return null;
 109  
         }
 110  
     }
 111  
 
 112  
     /** {@inheritDoc} */
 113  
     public ApplicationResource getResource(ApplicationResource base, Locale locale) {
 114  
         try {
 115  1
             URL url = servletContext.getResource(base.getLocalePath(locale));
 116  1
             if (url != null) {
 117  1
                 return new URLApplicationResource(base.getPath(), locale, url);
 118  
             } else {
 119  0
                 return null;
 120  
             }
 121  0
         } catch (MalformedURLException e) {
 122  0
             return null;
 123  
         }
 124  
     }
 125  
 
 126  
     /** {@inheritDoc} */
 127  
     public Collection<ApplicationResource> getResources(String path) {
 128  1
         ArrayList<ApplicationResource> resources = new ArrayList<ApplicationResource>();
 129  1
         resources.add(getResource(path));
 130  1
         return resources;
 131  
     }
 132  
 }