Coverage Report - org.apache.tiles.request.portlet.PortletApplicationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletApplicationContext
83%
26/31
58%
7/12
2.333
 
 1  
 /*
 2  
  * $Id: PortletApplicationContext.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.portlet;
 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.portlet.PortletContext;
 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.portlet.extractor.ApplicationScopeExtractor;
 38  
 import org.apache.tiles.request.portlet.extractor.InitParameterExtractor;
 39  
 
 40  
 /**
 41  
  * Portlet-based TilesApplicationContext implementation.
 42  
  *
 43  
  * @version $Rev: 1297705 $ $Date: 2012-03-07 07:44:30 +1100 (Wed, 07 Mar 2012) $
 44  
  */
 45  
 public class PortletApplicationContext implements ApplicationContext {
 46  
 
 47  
     /**
 48  
      * <p>The lazily instantiated <code>Map</code> of application scope
 49  
      * attributes.</p>
 50  
      */
 51  6
     private Map<String, Object> applicationScope = null;
 52  
 
 53  
     /**
 54  
      * <p>The <code>PortletContext</code> for this web application.</p>
 55  
      */
 56  6
     protected PortletContext context = null;
 57  
 
 58  
     /**
 59  
      * <p>The lazily instantiated <code>Map</code> of context initialization
 60  
      * parameters.</p>
 61  
      */
 62  6
     private Map<String, String> initParam = null;
 63  
 
 64  
     /**
 65  
      * Creates a new instance of PortletTilesApplicationContext.
 66  
      *
 67  
      * @param context The portlet context to use.
 68  
      */
 69  6
     public PortletApplicationContext(PortletContext context) {
 70  6
         initialize(context);
 71  6
     }
 72  
 
 73  
     /** {@inheritDoc} */
 74  
     public Object getContext() {
 75  1
         return context;
 76  
     }
 77  
 
 78  
     /**
 79  
      * <p>Initialize (or reinitialize) this {@link PortletApplicationContext} instance
 80  
      * for the specified Portlet API objects.</p>
 81  
      *
 82  
      * @param context The <code>PortletContext</code> for this web application
 83  
      */
 84  
     public void initialize(PortletContext context) {
 85  
 
 86  
         // Save the specified Portlet API object references
 87  6
         this.context = context;
 88  
 
 89  6
     }
 90  
 
 91  
     /**
 92  
      * <p>Return the {@link PortletContext} for this context.</p>
 93  
      *
 94  
      * @return The original portlet context.
 95  
      */
 96  
     public PortletContext getPortletContext() {
 97  1
         return (this.context);
 98  
     }
 99  
 
 100  
     /** {@inheritDoc} */
 101  
     public Map<String, Object> getApplicationScope() {
 102  1
         if ((applicationScope == null) && (context != null)) {
 103  1
             applicationScope = new ScopeMap(new ApplicationScopeExtractor(context));
 104  
         }
 105  1
         return (applicationScope);
 106  
 
 107  
     }
 108  
 
 109  
     /** {@inheritDoc} */
 110  
     public Map<String, String> getInitParams() {
 111  1
         if ((initParam == null) && (context != null)) {
 112  1
             initParam = new ReadOnlyEnumerationMap<String>(new InitParameterExtractor(context));
 113  
         }
 114  1
         return (initParam);
 115  
 
 116  
     }
 117  
 
 118  
     /** {@inheritDoc} */
 119  
     public ApplicationResource getResource(String localePath) {
 120  
         try {
 121  3
             URL url = context.getResource(localePath);
 122  3
             if (url != null) {
 123  2
                 return new URLApplicationResource(localePath, url);
 124  
             } else {
 125  1
                 return null;
 126  
             }
 127  0
         } catch (MalformedURLException e) {
 128  0
             return null;
 129  
         }
 130  
     }
 131  
 
 132  
     /** {@inheritDoc} */
 133  
     public ApplicationResource getResource(ApplicationResource base, Locale locale) {
 134  
         try {
 135  1
             URL url = context.getResource(base.getLocalePath(locale));
 136  1
             if (url != null) {
 137  1
                 return new URLApplicationResource(base.getPath(), locale, url);
 138  
             } else {
 139  0
                 return null;
 140  
             }
 141  0
         } catch (MalformedURLException e) {
 142  0
             return null;
 143  
         }
 144  
     }
 145  
 
 146  
     /** {@inheritDoc} */
 147  
     public Collection<ApplicationResource> getResources(String path) {
 148  1
         ArrayList<ApplicationResource> resources = new ArrayList<ApplicationResource>();
 149  1
         resources.add(getResource(path));
 150  1
         return resources;
 151  
     }
 152  
 }