    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/framework/util/WebappResourceResolver.java $
    002    // $Id: WebappResourceResolver.java 18195 2009-06-18 15:55:39Z mschneider $
    003    /*----------------------------------------------------------------------------
    004     This file is part of deegree, http://deegree.org/
    005     Copyright (C) 2001-2009 by:
    006       Department of Geography, University of Bonn
    007     and
    008       lat/lon GmbH
    009
    010     This library is free software; you can redistribute it and/or modify it under
    011     the terms of the GNU Lesser General Public License as published by the Free
    012     Software Foundation; either version 2.1 of the License, or (at your option)
    013     any later version.
    014     This library is distributed in the hope that it will be useful, but WITHOUT
    015     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    016     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    017     details.
    018     You should have received a copy of the GNU Lesser General Public License
    019     along with this library; if not, write to the Free Software Foundation, Inc.,
    020     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    021
    022     Contact information:
    023
    024     lat/lon GmbH
    025     Aennchenstr. 19, 53177 Bonn
    026     Germany
    027     http://lat-lon.de/
    028
    029     Department of Geography, University of Bonn
    030     Prof. Dr. Klaus Greve
    031     Postfach 1147, 53001 Bonn
    032     Germany
    033     http://www.geographie.uni-bonn.de/deegree/
    034
    035     e-mail: info@deegree.org
    036    ----------------------------------------------------------------------------*/
    037
    038    package org.deegree.framework.util;
    039
    040    import java.io.File;
    041    import java.net.MalformedURLException;
    042    import java.net.URI;
    043    import java.net.URL;
    044
    045    import javax.servlet.ServletContext;
    046
    047    import org.deegree.framework.log.ILogger;
    048
    049    /**
    050     * Utility class for resolving of references in webapp config files to {@link URL}s.
    051     *
    052     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
    053     * @author last edited by: $Author: mschneider $
    054     *
    055     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    056     */
    057    public class WebappResourceResolver {
    058
    059        /**
    060         * 'Heuristical' method to retrieve the {@link URL} for a file referenced from an init-param of
    061         * a webapp config file which may be:
    062         * <ul>
    063         * <li>a (absolute) <code>URL</code></li>
    064         * <li>a file location</li>
    065         * <li>a (relative) URL which in turn is resolved using <code>ServletContext.getRealPath</code>
    066         * </li>
    067         * </ul>
    068         *
    069         * @param location
    070         * @param context
    071         * @param log
    072         *            the log where errors are logged
    073         * @return the full (and whitespace-escaped) URL
    074         * @throws MalformedURLException
    075         */
    076        public static URL resolveFileLocation( String location, ServletContext context, ILogger log )
    077                                throws MalformedURLException {
    078            URL serviceConfigurationURL = null;
    079
    080            log.logDebug( "Resolving configuration file location: '" + location + "'..." );
    081            try {
    082                // construction of URI performs whitespace escaping
    083                serviceConfigurationURL = new URI( location ).toURL();
    084            } catch ( Exception e ) {
    085                log.logDebug( "No valid (absolute) URL. Trying context.getRealPath() now." );
    086                String realPath = context.getRealPath( location );
    087                if ( realPath == null ) {
    088                    log.logDebug( "No 'real path' available. Trying to parse as a file location now." );
    089                    serviceConfigurationURL = new File( location ).toURI().toURL();
    090                } else {
    091                    try {
    092                        // realPath may either be a URL or a File
    093                        serviceConfigurationURL = new URI( realPath ).toURL();
    094                    } catch ( Exception e2 ) {
    095                        log.logDebug( "'Real path' cannot be parsed as URL. " + "Trying to parse as a file location now." );
    096                        // construction of URI performs whitespace escaping
    097                        serviceConfigurationURL = new File( realPath ).toURI().toURL();
    098                        log.logDebug( "serviceConfigurationURL: " + serviceConfigurationURL );
    099                    }
    100                }
    101            }
    102            return serviceConfigurationURL;
    103        }
    104    }