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.internal.config;
21  
22  import org.apache.commons.collections4.CollectionUtils;
23  import org.apache.myfaces.tobago.config.TobagoConfig;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.xml.sax.SAXException;
27  
28  import javax.servlet.ServletContext;
29  import javax.servlet.ServletException;
30  import javax.xml.parsers.ParserConfigurationException;
31  import java.io.IOException;
32  import java.net.URISyntaxException;
33  import java.net.URL;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  public class TobagoConfigBuilder {
38  
39    private static final Logger LOG = LoggerFactory.getLogger(TobagoConfigBuilder.class);
40  
41    private static final String WEB_INF_TOBAGO_CONFIG_XML = "WEB-INF/tobago-config.xml";
42    private static final String META_INF_TOBAGO_CONFIG_XML = "META-INF/tobago-config.xml";
43  
44    private List<TobagoConfigFragment> configFragmentList;
45    private ServletContext servletContext;
46  
47    public TobagoConfigBuilder(final ServletContext servletContext) {
48      this.servletContext = servletContext;
49      this.configFragmentList = new ArrayList<TobagoConfigFragment>();
50    }
51  
52    public TobagoConfigBuilder(ServletContext servletContext, List<TobagoConfigFragment> configFragmentList) {
53      this(servletContext);
54      this.configFragmentList.addAll(configFragmentList);
55    }
56  
57    public static void init(final ServletContext servletContext) {
58      try {
59        final TobagoConfigBuilder builder = new TobagoConfigBuilder(servletContext);
60        builder.build();
61      } catch (final Throwable e) {
62        if (LOG.isErrorEnabled()) {
63          final String error = "Error while deploy process. Tobago can't be initialized! Application will not run!";
64          LOG.error(error, e);
65          throw new RuntimeException(error, e);
66        }
67      }
68    }
69  
70    public TobagoConfig build() throws URISyntaxException, SAXException,
71       ParserConfigurationException, ServletException, IOException {
72      final TobagoConfigImpl tobagoConfig = initializeConfigFromFiles();
73      // prepare themes
74      tobagoConfig.resolveThemes();
75      tobagoConfig.initDefaultValidatorInfo();
76      tobagoConfig.lock();
77  
78      servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, tobagoConfig);
79      return tobagoConfig;
80    }
81  
82    protected TobagoConfigImpl initializeConfigFromFiles()
83       throws ServletException, IOException, SAXException, ParserConfigurationException, URISyntaxException {
84      configFromClasspath();
85      configFromWebInf();
86      final TobagoConfigSorter sorter = new TobagoConfigSorter(configFragmentList);
87      sorter.sort();
88      return sorter.merge();
89    }
90  
91    private void configFromWebInf()
92        throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
93  
94      final URL url = servletContext.getResource("/" + WEB_INF_TOBAGO_CONFIG_XML);
95      if (url != null) {
96        configFragmentList.add(new TobagoConfigParser().parse(url));
97      }
98    }
99  
100   private void configFromClasspath() throws ServletException {
101 
102     try {
103       if (LOG.isInfoEnabled()) {
104         LOG.info("Searching for '" + META_INF_TOBAGO_CONFIG_XML + "'");
105       }
106       final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
107       final List<URL> urls = new ArrayList<URL>();
108       CollectionUtils.addAll(urls, classLoader.getResources(META_INF_TOBAGO_CONFIG_XML));
109 
110       for (final URL themeUrl : urls) {
111         try {
112           final TobagoConfigFragment fragment = new TobagoConfigParser().parse(themeUrl);
113           fragment.setUrl(themeUrl);
114           configFragmentList.add(fragment);
115 
116           // tomcat uses jar
117           // weblogic uses zip
118           // IBM WebSphere uses wsjar
119           final String protocol = themeUrl.getProtocol();
120           if (!"jar".equals(protocol) && !"zip".equals(protocol) && !"wsjar".equals(protocol)) {
121             LOG.warn("Unknown protocol '" + themeUrl + "'");
122           }
123         } catch (final Exception e) {
124           throw new Exception(e.getClass().getName() + " on themeUrl: " + themeUrl, e);
125         }
126       }
127     } catch (final Exception e) {
128       final String msg = "while loading ";
129       LOG.error(msg, e);
130       throw new ServletException(msg, e);
131     }
132   }
133 }