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.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(final InputStream stream) throws IOException {
81        props = new Properties();
82        props.loadFromXML(stream);
83      }
84  
85      @Override
86      protected Object handleGetObject(final String key) {
87        return props.getProperty(key);
88      }
89  
90      @Override
91      public Enumeration<String> getKeys() {
92        final Set<String> handleKeys = props.stringPropertyNames();
93        return Collections.enumeration(handleKeys);
94      }
95  
96      public static class XMLResourceBundleControl extends Control {
97  
98        @Override
99        public List<String> getFormats(final String baseName) {
100         return Collections.singletonList(XML);
101       }
102 
103       @Override
104       public ResourceBundle newBundle(
105           final String baseName, final Locale locale, final String format, final ClassLoader loader,
106           final boolean reload)
107           throws IllegalAccessException, InstantiationException,
108           IOException {
109 
110         if ((baseName == null) || (locale == null) || (format == null) || (loader == null)) {
111           throw new NullPointerException();
112         }
113         ResourceBundle bundle = null;
114         if (!format.equals(XML)) {
115           return null;
116         }
117 
118         final String bundleName = toBundleName(baseName, locale);
119         final String resourceName = toResourceName(bundleName, format);
120         final URL url = loader.getResource(resourceName);
121         if (url == null) {
122           return null;
123         }
124         final URLConnection connection = url.openConnection();
125         if (connection == null) {
126           return null;
127         }
128         if (reload) {
129           connection.setUseCaches(false);
130         }
131         try (final BufferedInputStream bis = new BufferedInputStream(connection.getInputStream())) {
132           bundle = new XmlTobagoBundle(bis);
133         }
134 
135         return bundle;
136       }
137     }
138   }
139 }