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.collections.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 final 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> list;
45  
46    private TobagoConfigBuilder(final ServletContext servletContext)
47        throws ServletException, IOException, SAXException, ParserConfigurationException, URISyntaxException {
48      list = new ArrayList<TobagoConfigFragment>();
49      configFromClasspath();
50      configFromWebInf(servletContext);
51      final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
52      sorter.sort();
53      final TobagoConfigImpl tobagoConfig = sorter.merge();
54  
55      tobagoConfig.initProjectState(servletContext);
56      // prepare themes
57      tobagoConfig.resolveThemes();
58  
59      tobagoConfig.initDefaultValidatorInfo();
60  
61      tobagoConfig.lock();
62  
63      servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, tobagoConfig);
64    }
65  
66    public static void init(final ServletContext servletContext) {
67      try {
68        final TobagoConfigBuilder builder = new TobagoConfigBuilder(servletContext);
69      } catch (final Throwable e) {
70        if (LOG.isErrorEnabled()) {
71          final String error = "Error while deploy process. Tobago can't be initialized! Application will not run!";
72          LOG.error(error, e);
73          throw new RuntimeException(error, e);
74        }
75      }
76    }
77  
78    private void configFromWebInf(final ServletContext servletContext)
79        throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
80  
81      final URL url = servletContext.getResource("/" + WEB_INF_TOBAGO_CONFIG_XML);
82      if (url != null) {
83        list.add(new TobagoConfigParser().parse(url));
84      }
85    }
86  
87    private void configFromClasspath() throws ServletException {
88  
89      try {
90        if (LOG.isInfoEnabled()) {
91          LOG.info("Searching for '" + META_INF_TOBAGO_CONFIG_XML + "'");
92        }
93        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
94        final List<URL> urls = new ArrayList<URL>();
95        CollectionUtils.addAll(urls, classLoader.getResources(META_INF_TOBAGO_CONFIG_XML));
96  
97        for (final URL themeUrl : urls) {
98          try {
99            final TobagoConfigFragment fragment = new TobagoConfigParser().parse(themeUrl);
100           fragment.setUrl(themeUrl);
101           list.add(fragment);
102 
103           // tomcat uses jar
104           // weblogic uses zip
105           // IBM WebSphere uses wsjar
106           final String protocol = themeUrl.getProtocol();
107           if (!"jar".equals(protocol) && !"zip".equals(protocol) && !"wsjar".equals(protocol)) {
108             LOG.warn("Unknown protocol '" + themeUrl + "'");
109           }
110         } catch (final Exception e) {
111           throw new Exception(e.getClass().getName() + " on themeUrl: " + themeUrl, e);
112         }
113       }
114     } catch (final Exception e) {
115       final String msg = "while loading ";
116       LOG.error(msg, e);
117       throw new ServletException(msg, e);
118     }
119   }
120 }