View Javadoc

1   package net.krecan.m2_proxy.resource;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   
7   import net.krecan.m2_proxy.utils.ResourceUtils;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  /***
12   * Default resource implementantion. Wraps input stream.
13   * @author Lukas Krecan
14   *
15   */
16  
17  public class DefaultResource extends AbstractResource implements ResourceWithInputStream{
18  	long length = -1;
19  	private InputStream inputStream;
20  	private long lastModified;
21  
22  	
23  	private static final Log LOG = LogFactory.getLog(DefaultResource.class);
24  
25  
26  	public DefaultResource(String logicalName, long lastModified, long length, InputStream inputStream) {
27  		super(logicalName);
28  		this.lastModified = lastModified;
29  		this.inputStream = inputStream;
30  		this.length = length;
31  	}
32  
33  	public long getLength() {
34  		return length;
35  	}
36  
37  	public void copyToStream(OutputStream outputStream) throws IOException {
38  		LOG.debug("Copying resurce to the output stream: "+getLogicalName());
39  		ResourceUtils.copy(inputStream, outputStream);
40  	}
41  
42  	public InputStream getInputStream() {
43  		return inputStream;
44  	}
45  
46  	protected void setInputStream(InputStream inputStream) {
47  		this.inputStream = inputStream;
48  	}
49  
50  	protected void setLength(long length) {
51  		this.length = length;
52  	}
53  
54  	public void release(){
55  		try {
56  			LOG.debug("Releasing resource "+getLogicalName());
57  			if (inputStream!=null) inputStream.close();
58  			setReleased(true);
59  		} catch (IOException e) {
60  			LOG.error("Error when closing input stream",e);
61  		}
62  	}
63  
64  	public long getLastModified() {
65  		return lastModified;
66  	}
67  
68  	public void setLastModified(long lastModificationDate) {
69  		this.lastModified = lastModificationDate;
70  	}
71  
72  	
73  	public String toString() {
74  		return getClass().getName()+"logicalName="+getLogicalName()+"; lastModified="+lastModified+"; length="+length;
75  	}
76  	public boolean equals(Object obj) {
77  		if (!(obj instanceof DefaultResource)) return false;
78  		DefaultResource second = (DefaultResource)obj;
79  		
80  		return getLogicalName().equals(second.getLogicalName()) && lastModified==second.getLastModified() && length==second.getLength();
81  	}
82  	
83  	public int hashCode() {
84  		long hashCode = 17+getLogicalName().hashCode();
85  		hashCode = hashCode*37+lastModified;
86  		hashCode = hashCode*31+length;
87  		return (int) hashCode;
88  	}
89  }