001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.modules.fs;
018:
019: import java.io.File;
020:
021: import javax.naming.directory.Attribute;
022: import javax.naming.directory.BasicAttribute;
023: import javax.naming.directory.BasicAttributes;
024:
025: /**
026: * This specialized resource attribute implementation does some lazy
027: * reading (to speed up simple checks, like checking the last modified
028: * date).
029: */
030: public class FileAttributes extends BasicAttributes {
031: // -------------------------------------------------------- Constructor
032:
033: public FileAttributes(File file) {
034: this .file = file;
035: }
036:
037: // --------------------------------------------------- Member Variables
038:
039: protected File file;
040:
041: protected boolean accessed = false;
042:
043: // ----------------------------------------- ResourceAttributes Methods
044: public static String CONTENT_LENGTH = "contentLength";
045:
046: public Attribute get(String attrId) {
047: if (CONTENT_LENGTH.equalsIgnoreCase(attrId)) {
048: // XXX use our own att, with long support
049: return new BasicAttribute(CONTENT_LENGTH, new Long(
050: getContentLength()));
051: }
052: return (super .get(attrId));
053: }
054:
055: /**
056: * Is collection.
057: */
058: // public boolean isCollection() {
059: // if (!accessed) {
060: // collection = file.isDirectory();
061: // accessed = true;
062: // }
063: // return super.isCollection();
064: // }
065: // Those methods avoid using an Attribute and return the real value.
066: // There is no caching at this level - use the higher level caching.
067: /**
068: * Get content length.
069: *
070: * @return content length value
071: */
072: public long getContentLength() {
073: long contentLength = file.length();
074: return contentLength;
075: }
076:
077: /**
078: * Get creation time.
079: *
080: * @return creation time value
081: */
082: public long getCreation() {
083: long creation = file.lastModified();
084: return creation;
085: }
086:
087: /**
088: * Get last modified time.
089: *
090: * @return lastModified time value
091: */
092: public long getLastModified() {
093: long lastModified = file.lastModified();
094: return lastModified;
095: }
096:
097: /**
098: * Get resource type.
099: *
100: * @return String resource type
101: */
102: // public String getResourceType() {
103: // if (!accessed) {
104: // //collection = file.isDirectory();
105: // accessed = true;
106: // }
107: // return super.getResourceType();
108: // }
109: }
|