View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.io;
18  
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
22  import org.springframework.core.io.ContextResource;
23  import org.springframework.core.io.UrlResource;
24  import org.springframework.osgi.io.internal.OsgiResourceUtils;
25  
26  /**
27   * Extension to {@link UrlResource} that adds support for
28   * {@link ContextResource}. This resource is used by the
29   * {@link OsgiBundleResourcePatternResolver} with the URLs returned by the OSGi
30   * API.
31   * 
32   * @author Costin Leau
33   */
34  class UrlContextResource extends UrlResource implements ContextResource {
35  
36  	private final String pathWithinContext;
37  
38  
39  	/**
40  	 * Constructs a new <code>UrlContextResource</code> instance.
41  	 * 
42  	 * @param path
43  	 * @throws MalformedURLException
44  	 */
45  	public UrlContextResource(String path) throws MalformedURLException {
46  		super(path);
47  		pathWithinContext = checkPath(path);
48  	}
49  
50  	private String checkPath(String path) {
51  		return (path.startsWith(OsgiResourceUtils.FOLDER_DELIMITER) ? path : OsgiResourceUtils.FOLDER_DELIMITER + path);
52  	}
53  
54  	/**
55  	 * Constructs a new <code>UrlContextResource</code> instance.
56  	 * 
57  	 * @param url
58  	 */
59  	public UrlContextResource(URL url, String path) {
60  		super(url);
61  		this.pathWithinContext = checkPath(path);
62  	}
63  
64  	public String getPathWithinContext() {
65  		return pathWithinContext;
66  	}
67  }