KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > spi > impl > ServletContextCatalogLocator


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/spi/impl/ServletContextCatalogLocator.java#4 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2005-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.spi.impl;
11
12 import mondrian.spi.CatalogLocator;
13
14 import javax.servlet.ServletContext JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17
18 /**
19  * Locates a catalog based upon a {@link ServletContext}.<p/>
20  *
21  * If the catalog URI is an absolute path, it refers to a resource inside our
22  * WAR file, so replace the URL.
23  *
24  * @author Gang Chen, jhyde
25  * @since December, 2005
26  * @version $Id: //open/mondrian/src/main/mondrian/spi/impl/ServletContextCatalogLocator.java#4 $
27  */

28 public class ServletContextCatalogLocator implements CatalogLocator {
29     private ServletContext JavaDoc servletContext;
30
31     public ServletContextCatalogLocator(ServletContext JavaDoc servletContext) {
32         this.servletContext = servletContext;
33     }
34
35     public String JavaDoc locate(String JavaDoc catalogPath) {
36         // If the catalog is an absolute path, it refers to a resource inside
37
// our WAR file, so replace the URL.
38
if (catalogPath != null && catalogPath.startsWith("/")) {
39             try {
40                 URL JavaDoc url = servletContext.getResource(catalogPath);
41                 if (url == null) {
42                     // The catalogPath does not exist, but construct a feasible
43
// URL so that the error message makes sense.
44
url = servletContext.getResource("/");
45                     url = new URL JavaDoc(url.getProtocol(), url.getHost(),
46                             url.getPort(),
47                             url.getFile() + catalogPath.substring(1));
48                 }
49                 catalogPath = url.toString();
50             } catch (MalformedURLException JavaDoc ignored) {
51             }
52         }
53         return catalogPath;
54     }
55 }
56
57 // End ServletContextCatalogLocator.java
58
Popular Tags