View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.context;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.io.BufferedInputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.net.URL;
29  import java.net.URLConnection;
30  import java.util.Collections;
31  import java.util.Enumeration;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.Properties;
35  import java.util.ResourceBundle;
36  import java.util.Set;
37  
38  /**
39   * This class works like the Java resource bundle mechanism for a named resource bundle
40   * and adds the functionality of the tobago themes and also supports XML properties files.
41   * This class should be extended and used in the faces-config.xml.
42   *
43   * @since 1.5.0
44   */
45  public class TobagoBundle extends ResourceBundle {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(TobagoBundle.class);
48  
49    private String bundleName;
50    private ResourceBundle bundle;
51  
52    public TobagoBundle(final String bundleName) {
53      this.bundleName = bundleName;
54      this.bundle = ResourceBundle.getBundle(bundleName, new XmlTobagoBundle.XMLResourceBundleControl());
55    }
56  
57    @Override
58    protected Object handleGetObject(final String key) {
59      if (LOG.isDebugEnabled()) {
60        LOG.debug("Searching for '{}' in bundle '{}'", key, bundleName);
61      }
62      return bundle.getObject(key);
63    }
64  
65    @Override
66    public Enumeration<String> getKeys() {
67      return Collections.enumeration(Collections.<String>emptyList());
68    }
69  
70    public String getBundleName() {
71      return bundleName;
72    }
73  
74    public static class XmlTobagoBundle extends ResourceBundle {
75  
76      private static final String XML = "xml";
77  
78      private Properties props;
79  
80      private XmlTobagoBundle(InputStream stream) throws IOException {
81        props = new Properties();
82        props.loadFromXML(stream);
83      }
84  
85      protected Object handleGetObject(String key) {
86        return props.getProperty(key);
87      }
88  
89      public Enumeration<String> getKeys() {
90        Set<String> handleKeys = props.stringPropertyNames();
91        return Collections.enumeration(handleKeys);
92      }
93  
94      public static class XMLResourceBundleControl extends Control {
95  
96        public List<String> getFormats(String baseName) {
97          return Collections.singletonList(XML);
98        }
99  
100       public ResourceBundle newBundle(
101           String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
102           throws IllegalAccessException, InstantiationException,
103           IOException {
104 
105         if ((baseName == null) || (locale == null) || (format == null) || (loader == null)) {
106           throw new NullPointerException();
107         }
108         ResourceBundle bundle = null;
109         if (!format.equals(XML)) {
110           return null;
111         }
112 
113         String bundleName = toBundleName(baseName, locale);
114         String resourceName = toResourceName(bundleName, format);
115         URL url = loader.getResource(resourceName);
116         if (url == null) {
117           return null;
118         }
119         URLConnection connection = url.openConnection();
120         if (connection == null) {
121           return null;
122         }
123         if (reload) {
124           connection.setUseCaches(false);
125         }
126         InputStream stream = connection.getInputStream();
127         if (stream == null) {
128           return null;
129         }
130         BufferedInputStream bis = new BufferedInputStream(stream);
131         bundle = new XmlTobagoBundle(bis);
132         bis.close();
133 
134         return bundle;
135       }
136     }
137   }
138 }