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