001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsFile.java,v $
003: * Date : $Date: 2008-02-27 12:05:38 $
004: * Version: $Revision: 1.30 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.file;
033:
034: import org.opencms.db.CmsResourceState;
035: import org.opencms.main.CmsException;
036: import org.opencms.util.CmsUUID;
037:
038: import java.io.Serializable;
039:
040: /**
041: * A file resource in the OpenCms VFS.<p>
042: *
043: * A file resource is a CmsResource that contains an additional byte
044: * array of binary data, which is the file content.<p>
045: *
046: * A file object is not allowed to have sub-resources.<p>
047: *
048: * @author Alexander Kandzior
049: * @author Michael Emmerich
050: *
051: * @version $Revision: 1.30 $
052: *
053: * @since 6.0.0
054: */
055: public class CmsFile extends CmsResource implements Cloneable,
056: Serializable, Comparable {
057:
058: /** Serial version UID required for safe serialization. */
059: private static final long serialVersionUID = -5201022482708455620L;
060:
061: /** The content of this file. */
062: private byte[] m_fileContent;
063:
064: /**
065: * Constructor, creates a new file Object from the given resource with
066: * an empty byte array as file content, if the resource does not
067: * implement a file.<p>
068: *
069: * @param resource the base resource object to create a file from
070: */
071: public CmsFile(CmsResource resource) {
072:
073: this (resource.getStructureId(), resource.getResourceId(),
074: resource.getRootPath(), resource.getTypeId(), resource
075: .getFlags(), resource.getProjectLastModified(),
076: resource.getState(), resource.getDateCreated(),
077: resource.getUserCreated(), resource
078: .getDateLastModified(), resource
079: .getUserLastModified(), resource
080: .getDateReleased(), resource.getDateExpired(),
081: resource.getSiblingCount(), resource.getLength(),
082: resource.getDateContent(), resource.getVersion(),
083: // if the resource already is a file, keep contents that might have been already read
084: resource instanceof CmsFile ? ((CmsFile) resource)
085: .getContents() : null);
086: }
087:
088: /**
089: * Constructor, creates a new file object.<p>
090: *
091: * @param structureId the id of this resources structure record
092: * @param resourceId the id of this resources resource record
093: * @param path the filename of this resource
094: * @param type the type of this resource
095: * @param flags the flags of this resource
096: * @param projectId the project id this resource was last modified in
097: * @param state the state of this resource
098: * @param dateCreated the creation date of this resource
099: * @param userCreated the id of the user who created this resource
100: * @param dateLastModified the date of the last modification of this resource
101: * @param userLastModified the id of the user who did the last modification of this resource
102: * @param dateReleased the release date of this resource
103: * @param dateExpired the expiration date of this resource
104: * @param linkCount the count of all siblings of this resource
105: * @param length the size of the file content of this resource
106: * @param dateContent the date of the last modification of the content of this resource
107: * @param version the version number of this resource
108: * @param content the binary content data of this file
109: */
110: public CmsFile(CmsUUID structureId, CmsUUID resourceId,
111: String path, int type, int flags, CmsUUID projectId,
112: CmsResourceState state, long dateCreated,
113: CmsUUID userCreated, long dateLastModified,
114: CmsUUID userLastModified, long dateReleased,
115: long dateExpired, int linkCount, int length,
116: long dateContent, int version, byte[] content) {
117:
118: super (structureId, resourceId, path, type, false, flags,
119: projectId, state, dateCreated, userCreated,
120: dateLastModified, userLastModified, dateReleased,
121: dateExpired, linkCount, length, dateContent, version);
122:
123: m_fileContent = content;
124: if (m_fileContent == null) {
125: m_fileContent = new byte[0];
126: }
127: }
128:
129: /**
130: * Utility method to upgrade a CmsResource to a CmsFile.<p>
131: *
132: * Sometimes a CmsResource might already be a (casted) CmsFile that
133: * also has the contents read. This method tries to optimize
134: * read access to the VFS by "upgrading" the CmsResource to a CmsFile
135: * first. If this fails, the CmsFile is read from the VFS.<p>
136: *
137: * @param resource the resource to upgrade
138: * @param cms permission context for accessing the VFS
139: *
140: * @return the upgraded (or read) file
141: *
142: * @throws CmsException if something goes wrong
143: *
144: * @deprecated use {@link CmsObject#readFile(CmsResource)} instead
145: */
146: public static CmsFile upgrade(CmsResource resource, CmsObject cms)
147: throws CmsException {
148:
149: // the logic here has been moved to the readFile(CmsResource) method in the CmsObject
150: return cms.readFile(resource);
151: }
152:
153: /**
154: * Returns a clone of this Objects instance.<p>
155: *
156: * @return a clone of this instance
157: */
158: public Object clone() {
159:
160: byte[] newContent = new byte[this .getContents().length];
161: System.arraycopy(getContents(), 0, newContent, 0,
162: getContents().length);
163:
164: CmsFile clone = new CmsFile(getStructureId(), getResourceId(),
165: getRootPath(), getTypeId(), getFlags(),
166: getProjectLastModified(), getState(), getDateCreated(),
167: getUserCreated(), getDateLastModified(),
168: getUserLastModified(), getDateReleased(),
169: getDateExpired(), getSiblingCount(), getLength(),
170: getDateContent(), getVersion(), newContent);
171:
172: if (isTouched()) {
173: clone.setDateLastModified(getDateLastModified());
174: }
175:
176: return clone;
177: }
178:
179: /**
180: * Returns the content of this file.<p>
181: *
182: * @return the content of this file
183: */
184: public byte[] getContents() {
185:
186: return m_fileContent;
187: }
188:
189: /**
190: * @see org.opencms.file.CmsResource#getLength()
191: */
192: public int getLength() {
193:
194: return m_length;
195: }
196:
197: /**
198: * @see org.opencms.file.CmsResource#isFile()
199: */
200: public boolean isFile() {
201:
202: return true;
203: }
204:
205: /**
206: * @see org.opencms.file.CmsResource#isFolder()
207: */
208: public boolean isFolder() {
209:
210: return false;
211: }
212:
213: /**
214: * Sets the contents of this file.<p>
215: *
216: * This will also set the date content, but only if the content is already set.<p>
217: *
218: * @param value the content of this file
219: */
220: public void setContents(byte[] value) {
221:
222: long dateContent = System.currentTimeMillis();
223: if ((m_fileContent == null) || (m_fileContent.length == 0)) {
224: dateContent = m_dateContent;
225: }
226: m_fileContent = new byte[value.length];
227: System.arraycopy(value, 0, m_fileContent, 0, value.length);
228:
229: if (m_fileContent.length > 0) {
230: m_length = m_fileContent.length;
231: } else {
232: m_length = 0;
233: }
234: m_dateContent = dateContent;
235: }
236: }
|