View Javadoc

1   package net.krecan.m2_proxy.repository;
2   
3   import java.io.BufferedInputStream;
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   import java.io.PushbackInputStream;
7   import java.net.MalformedURLException;
8   import java.net.URL;
9   import java.net.URLConnection;
10  
11  import net.krecan.m2_proxy.Repository;
12  import net.krecan.m2_proxy.Resource;
13  import net.krecan.m2_proxy.resource.DefaultResource;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /***
19   * URL based repository. Obsolete, not used any more
20   * 
21   * @author Lukas Krecan
22   * 
23   */
24  public class UrlRepository implements Repository {
25  	private static final Log LOG = LogFactory.getLog(UrlRepository.class);
26  	
27  	private URL baseUrl;
28  
29  	/***
30  	 * Loads resource from file.
31  	 */
32  	public Resource loadResource(String resourceName)
33  			throws IOException {
34  		URL resourceUrl = getResourceUrl(resourceName);
35  		URLConnection connection = resourceUrl.openConnection();
36  		if (connection.getContentLength()>0)
37  		{
38  			PushbackInputStream in = null;
39  			
40  			LOG.debug("Loading resource "+resourceUrl);
41  			try
42  			{
43  			  in = new PushbackInputStream(new BufferedInputStream(connection.getInputStream()));
44  			  in.unread(in.read());
45  			}
46  			catch (IOException e)
47  			{
48  				if ((e instanceof FileNotFoundException) || (e.getCause() instanceof FileNotFoundException))
49  				{
50  					//it is not exceptional state, when file is not found
51  					return null;
52  				}
53  				else
54  				{
55  					throw e;
56  				}
57  			}
58  			return new DefaultResource(resourceUrl.toString(), connection.getLastModified(), connection.getContentLength(), in);
59  		}
60  		else
61  		{
62  			return null;
63  		}
64  		
65  	}
66  	/***
67  	 * Returns resource URL. Concats baseUrl and resourceName.
68  	 * @param resourceName
69  	 * @return
70  	 * @throws MalformedURLException
71  	 */
72  	protected URL getResourceUrl(String resourceName) throws MalformedURLException {
73  		URL resourceUrl = new URL(getBaseUrl(), resourceName);
74  		return resourceUrl;
75  	}
76  
77  	public URL getBaseUrl() {
78  		return baseUrl;
79  	}
80  
81  	/***
82  	 * Sets base url
83  	 * 
84  	 * @param baseUrl
85  	 */
86  	public void setBaseUrl(URL baseUrl) {
87  		this.baseUrl = baseUrl;
88  	}
89  
90  
91  }