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