This project has retired. For details please refer to its Attic page.
BaseLocaleUrlDefinitionDAO xref
View Javadoc

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      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      public BaseLocaleUrlDefinitionDAO(ApplicationContext applicationContext) {
91          this.applicationContext = applicationContext;
92          lastModifiedDates = new HashMap<String, Long>();
93      }
94  
95      public void setSources(List<ApplicationResource> sources) {
96          // filter out any sources that are already localized
97          ArrayList<ApplicationResource> defaultSources = new ArrayList<ApplicationResource>();
98          for(ApplicationResource source: sources) {
99              if(Locale.ROOT.equals(source.getLocale())) {
100                 defaultSources.add(source);
101             }
102         }
103         this.sources = defaultSources;
104     }
105 
106     public void setReader(DefinitionsReader reader) {
107         this.reader = reader;
108     }
109 
110     /** {@inheritDoc} */
111     public boolean refreshRequired() {
112         boolean status = false;
113 
114         Set<String> paths = lastModifiedDates.keySet();
115 
116         try {
117             for (String path : paths) {
118                 Long lastModifiedDate = lastModifiedDates.get(path);
119                 ApplicationResource resource = applicationContext.getResource(path);
120                 long newModDate = resource.getLastModified();
121                 if (newModDate != lastModifiedDate) {
122                     status = true;
123                     break;
124                 }
125             }
126         } catch (IOException e) {
127             log.warn("Exception while monitoring update times.", e);
128             return true;
129         }
130         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         Map<String, Definition> defsMap = null;
141 
142         InputStream stream = null;
143         try {
144             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             stream = resource.getInputStream();
150             defsMap = reader.read(stream);
151         } catch (FileNotFoundException e) {
152             // File not found. continue.
153             if (log.isDebugEnabled()) {
154                 log.debug("File " + resource.toString() + " not found, continue");
155             }
156         } catch (IOException e) {
157             throw new DefinitionsFactoryException(
158                     "I/O error processing configuration.", e);
159         } finally {
160             try {
161                 if (stream != null) {
162                     stream.close();
163                 }
164             } catch (IOException e) {
165                 throw new DefinitionsFactoryException(
166                         "I/O error closing " + resource.toString(), e);
167             }
168         }
169 
170         return defsMap;
171     }
172 }