001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.extension;
016:
017: import java.io.IOException;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.HashSet;
023: import java.util.Set;
024: import javax.servlet.ServletContext;
025: import javax.xml.parsers.ParserConfigurationException;
026: import javax.xml.parsers.SAXParserFactory;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.araneaframework.core.AraneaRuntimeException;
030: import org.xml.sax.InputSource;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.XMLReader;
033:
034: /**
035: * Initializes the external resources of Aranea. External resources are static
036: * files that are required by different web components - images, javascripts, html
037: * files. All resources are listed in different XML configuration files.Consult the
038: * reference manual for configuring resources how-to.
039: *
040: * The order is to search all "conf/aranea-resources.xml" files and then the
041: * extensions' configuration files.
042: *
043: * @author "Toomas Römer" <toomas@webmedia.ee>
044: */
045: public class ExternalResourceInitializer {
046: private static final Log log = LogFactory
047: .getLog(ExternalResourceInitializer.class);
048:
049: /**
050: * Framework application main configuration file.
051: */
052: public static final String ARANEA_RESOURCES_FILE_NAME = "conf/aranea-resources.xml";
053:
054: // TODO: extensions configurable, right now explicit extension configuration file
055: public static final String EXTENSION_TINY_MCE = "conf/aranea-extension-tinymce.xml";
056:
057: public ExternalResource getResources(ServletContext context) {
058: try {
059: XMLReader xr = SAXParserFactory.newInstance()
060: .newSAXParser().getXMLReader();
061: ExternalResourceConfigurationHandler handler = new ExternalResourceConfigurationHandler();
062:
063: xr.setContentHandler(handler);
064: xr.setErrorHandler(handler);
065:
066: ClassLoader loader = Thread.currentThread()
067: .getContextClassLoader();
068:
069: Enumeration classPathResources = loader
070: .getResources(ARANEA_RESOURCES_FILE_NAME);
071: Enumeration contextPathResources = getContextResources(
072: context, ARANEA_RESOURCES_FILE_NAME);
073:
074: if (!(classPathResources.hasMoreElements() || contextPathResources
075: .hasMoreElements()))
076: log.warn("Aranea resource configuration file '"
077: + ARANEA_RESOURCES_FILE_NAME + "' not found.");
078: loadResources(classPathResources, xr);
079: loadResources(contextPathResources, xr);
080:
081: classPathResources = loader
082: .getResources(EXTENSION_TINY_MCE);
083: contextPathResources = getContextResources(context,
084: EXTENSION_TINY_MCE);
085: loadResources(classPathResources, xr);
086: loadResources(contextPathResources, xr);
087:
088: return handler.getResource();
089: } catch (ParserConfigurationException e) {
090: throw new AraneaRuntimeException(
091: "Problem while configuring SAX parser", e);
092: } catch (SAXException e) {
093: throw new AraneaRuntimeException(
094: "Problem while parsing resource configuration file",
095: e);
096: } catch (IOException e) {
097: throw new AraneaRuntimeException(
098: "Problem while reading configuration file", e);
099: }
100: }
101:
102: protected void loadResources(Enumeration resources, XMLReader xr)
103: throws IOException, SAXException {
104: while (resources.hasMoreElements()) {
105: URL fileURL = (URL) resources.nextElement();
106: log.debug("Adding resources from file'" + fileURL + "'");
107: xr.parse(new InputSource(fileURL.openStream()));
108: }
109: }
110:
111: protected Enumeration getContextResources(ServletContext ctx,
112: String fileName) throws MalformedURLException {
113: Set fileURLSet = new HashSet();
114: URL url;
115:
116: url = ctx.getResource("/META-INF/" + fileName);
117: if (url != null)
118: fileURLSet.add(url);
119: url = ctx.getResource("/WEB-INF/" + fileName);
120: if (url != null)
121: fileURLSet.add(url);
122:
123: return Collections.enumeration(fileURLSet);
124: }
125: }
|