001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.asset;
015    
016    
017    /**
018     * Wrapper around cached asset resource.
019     * 
020     * @author jkuhnert
021     */
022    public class CachedAsset
023    {
024        
025        /**
026         * The raw data for this resource.
027         */
028        private byte[] _data;
029        
030        /**
031         * The gzipped version of the raw data.
032         */
033        private byte[] _gzipData;
034        
035        /**
036         * Path to the resource.
037         */
038        private String _path;
039        
040        /**
041         * The last known modification time of the data this cached object
042         * represents. Is used to invalidate cache entries.
043         */
044        private long _lastModified;
045        
046        /**
047         * Creates a new cachable asset entry. 
048         * 
049         * @param path
050         *          The path string of the resource.
051         * @param lastModified
052         *          The last known modification time of the data this cached object
053         *          represents. Is used to invalidate cache entries.
054         * @param data
055         *          The data representation to cache.
056         * @param gzipData
057         *          The optional gzip'ed data.
058         */
059        public CachedAsset(String path, long lastModified, byte[] data, byte[] gzipData)
060        {
061            _path = path;
062            _lastModified = lastModified;
063            _data = data;
064            _gzipData = gzipData;
065        }
066        
067        /**
068         * @return Returns the data.
069         */
070        public byte[] getData()
071        {
072            return _data;
073        }
074        
075        /**
076         * @param data The data to set.
077         */
078        public void setData(byte[] data)
079        {
080            _data = data;
081        }
082    
083        
084        /**
085         * @return Returns the gzipData.
086         */
087        public byte[] getGzipData()
088        {
089            return _gzipData;
090        }
091    
092        
093        /**
094         * @param gzipData The gzipData to set.
095         */
096        public void setGzipData(byte[] gzipData)
097        {
098            _gzipData = gzipData;
099        }
100    
101        
102        /**
103         * @return Returns the path.
104         */
105        public String getPath()
106        {
107            return _path;
108        }
109        
110        /**
111         * @return Returns the lastModified.
112         */
113        public long getLastModified()
114        {
115            return _lastModified;
116        }
117        
118        /**
119         * Clears the currently cached data and resets the last modified time.
120         * 
121         * @param lastModified The lastModified to set.
122         */
123        public void clear(long lastModified)
124        {
125            _lastModified = lastModified;
126            _data = null;
127            _gzipData = null;
128        }
129    
130        /**
131         * {@inheritDoc}
132         */
133        public int hashCode()
134        {
135            final int prime = 31;
136            int result = 1;
137            result = prime * result + ((_path == null) ? 0 : _path.hashCode());
138            return result;
139        }
140    
141        /**
142         * {@inheritDoc}
143         */
144        public boolean equals(Object obj)
145        {
146            if (this == obj) return true;
147            if (obj == null) return false;
148            if (getClass() != obj.getClass()) return false;
149            final CachedAsset other = (CachedAsset) obj;
150            if (_path == null) {
151                if (other._path != null) return false;
152            } else if (!_path.equals(other._path)) return false;
153            return true;
154        }
155        
156        /**
157         * {@inheritDoc}
158         */
159        public String toString()
160        {
161            String ret = "CachedAsset [path: " + _path;
162            
163            if (_data != null)
164                ret += ", data size(bytes): " + _data.length;
165            if (_gzipData != null)
166                ret += ", gzip data size(bytes): " + _gzipData.length;
167            
168            ret += ", lastModified(ms): " + _lastModified + "]";
169            
170            return ret;
171        }
172    }