Coverage Report - org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseLocaleUrlDefinitionDAO
80%
37/46
83%
10/12
3.6
 
 1  
 /*
 2  
  * $Id: BaseLocaleUrlDefinitionDAO.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  
 
 22  
 package org.apache.tiles.definition.dao;
 23  
 
 24  
 import java.io.FileNotFoundException;
 25  
 import java.io.IOException;
 26  
 import java.io.InputStream;
 27  
 import java.util.ArrayList;
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.Locale;
 31  
 import java.util.Map;
 32  
 import java.util.Set;
 33  
 
 34  
 import org.apache.tiles.Definition;
 35  
 import org.apache.tiles.definition.DefinitionsFactoryException;
 36  
 import org.apache.tiles.definition.DefinitionsReader;
 37  
 import org.apache.tiles.definition.RefreshMonitor;
 38  
 import org.apache.tiles.request.ApplicationContext;
 39  
 import org.apache.tiles.request.ApplicationResource;
 40  
 import org.slf4j.Logger;
 41  
 import org.slf4j.LoggerFactory;
 42  
 
 43  
 /**
 44  
  * Base abstract class for a DAO that is based on URLs and locale as a
 45  
  * customization key.
 46  
  *
 47  
  * @version $Rev: 1297705 $ $Date: 2012-03-07 07:44:30 +1100 (Wed, 07 Mar 2012) $
 48  
  * @since 2.1.0
 49  
  */
 50  
 public abstract class BaseLocaleUrlDefinitionDAO implements
 51  
         DefinitionDAO<Locale>, RefreshMonitor {
 52  
 
 53  
     /**
 54  
      * The logging object.
 55  
      */
 56  29
     private final Logger log = LoggerFactory
 57  
             .getLogger(BaseLocaleUrlDefinitionDAO.class);
 58  
 
 59  
     /**
 60  
      * Contains the URL objects identifying where configuration data is found.
 61  
      *
 62  
      * @since 2.1.0
 63  
      */
 64  
     protected List<ApplicationResource> sources;
 65  
 
 66  
     /**
 67  
      * Contains the dates that the URL sources were last modified.
 68  
      *
 69  
      * @since 2.1.0
 70  
      */
 71  
     protected Map<String, Long> lastModifiedDates;
 72  
 
 73  
     /**
 74  
      * Reader used to get definitions from the sources.
 75  
      *
 76  
      * @since 2.1.0
 77  
      */
 78  
     protected DefinitionsReader reader;
 79  
 
 80  
     /**
 81  
      * ApplicationContext to locate the source files.
 82  
      * 
 83  
      * @since 3.0.0
 84  
      */
 85  
     protected ApplicationContext applicationContext;
 86  
     
 87  
     /**
 88  
      * Constructor.
 89  
      */
 90  29
     public BaseLocaleUrlDefinitionDAO(ApplicationContext applicationContext) {
 91  29
         this.applicationContext = applicationContext;
 92  29
         lastModifiedDates = new HashMap<String, Long>();
 93  29
     }
 94  
 
 95  
     public void setSources(List<ApplicationResource> sources) {
 96  
         // filter out any sources that are already localized
 97  27
         ArrayList<ApplicationResource> defaultSources = new ArrayList<ApplicationResource>();
 98  27
         for(ApplicationResource source: sources) {
 99  51
             if(Locale.ROOT.equals(source.getLocale())) {
 100  51
                 defaultSources.add(source);
 101  
             }
 102  51
         }
 103  27
         this.sources = defaultSources;
 104  27
     }
 105  
 
 106  
     public void setReader(DefinitionsReader reader) {
 107  28
         this.reader = reader;
 108  28
     }
 109  
 
 110  
     /** {@inheritDoc} */
 111  
     public boolean refreshRequired() {
 112  2
         boolean status = false;
 113  
 
 114  2
         Set<String> paths = lastModifiedDates.keySet();
 115  
 
 116  
         try {
 117  2
             for (String path : paths) {
 118  2
                 Long lastModifiedDate = lastModifiedDates.get(path);
 119  2
                 ApplicationResource resource = applicationContext.getResource(path);
 120  2
                 long newModDate = resource.getLastModified();
 121  2
                 if (newModDate != lastModifiedDate) {
 122  1
                     status = true;
 123  1
                     break;
 124  
                 }
 125  1
             }
 126  0
         } catch (IOException e) {
 127  0
             log.warn("Exception while monitoring update times.", e);
 128  0
             return true;
 129  2
         }
 130  2
         return status;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Loads definitions from an URL without loading from "parent" URLs.
 135  
      *
 136  
      * @param resource The URL to read.
 137  
      * @return The definition map that has been read.
 138  
      */
 139  
     protected Map<String, Definition> loadDefinitionsFromResource(ApplicationResource resource) {
 140  177
         Map<String, Definition> defsMap = null;
 141  
 
 142  177
         InputStream stream = null;
 143  
         try {
 144  177
             lastModifiedDates.put(resource.getLocalePath(), resource
 145  
                     .getLastModified());
 146  
 
 147  
             // Definition must be collected, starting from the base
 148  
             // source up to the last localized file.
 149  177
             stream = resource.getInputStream();
 150  176
             defsMap = reader.read(stream);
 151  1
         } catch (FileNotFoundException e) {
 152  
             // File not found. continue.
 153  1
             if (log.isDebugEnabled()) {
 154  0
                 log.debug("File " + resource.toString() + " not found, continue");
 155  
             }
 156  0
         } catch (IOException e) {
 157  0
             throw new DefinitionsFactoryException(
 158  
                     "I/O error processing configuration.", e);
 159  
         } finally {
 160  0
             try {
 161  177
                 if (stream != null) {
 162  176
                     stream.close();
 163  
                 }
 164  0
             } catch (IOException e) {
 165  0
                 throw new DefinitionsFactoryException(
 166  
                         "I/O error closing " + resource.toString(), e);
 167  177
             }
 168  
         }
 169  
 
 170  177
         return defsMap;
 171  
     }
 172  
 }