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