1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/*
2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2008 the original author or authors.
3bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License.
6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at
7bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
8bda3441225e0607b5ced8b538123fd7c7a417910chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software
11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and
14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License.
15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.fake.filesystem;
17bda3441225e0607b5ced8b538123fd7c7a417910chrismair
18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.util.Assert;
19bda3441225e0607b5ced8b538123fd7c7a417910chrismair
20bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport java.util.Date;
21bda3441225e0607b5ced8b538123fd7c7a417910chrismair
22bda3441225e0607b5ced8b538123fd7c7a417910chrismair/**
23bda3441225e0607b5ced8b538123fd7c7a417910chrismair * The abstract superclass for concrete file system entry classes representing files and directories.
24bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair
26bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$
27bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
28bda3441225e0607b5ced8b538123fd7c7a417910chrismairpublic abstract class AbstractFileSystemEntry implements FileSystemEntry {
29bda3441225e0607b5ced8b538123fd7c7a417910chrismair
30bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private String path;
31bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private boolean pathLocked = false;
32bda3441225e0607b5ced8b538123fd7c7a417910chrismair
33bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private Date lastModified;
34bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private String owner;
35bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private String group;
36bda3441225e0607b5ced8b538123fd7c7a417910chrismair
37bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public Date getLastModified() {
38bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return lastModified;
39bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
40bda3441225e0607b5ced8b538123fd7c7a417910chrismair
41bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void setLastModified(Date lastModified) {
42bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.lastModified = lastModified;
43bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
44bda3441225e0607b5ced8b538123fd7c7a417910chrismair
45bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public String getOwner() {
46bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return owner;
47bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
48bda3441225e0607b5ced8b538123fd7c7a417910chrismair
49bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void setOwner(String owner) {
50bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.owner = owner;
51bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
52bda3441225e0607b5ced8b538123fd7c7a417910chrismair
53bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public String getGroup() {
54bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return group;
55bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
56bda3441225e0607b5ced8b538123fd7c7a417910chrismair
57bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void setGroup(String group) {
58bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.group = group;
59bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
60bda3441225e0607b5ced8b538123fd7c7a417910chrismair
61bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public Permissions getPermissions() {
62bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return permissions;
63bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
64bda3441225e0607b5ced8b538123fd7c7a417910chrismair
65bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void setPermissions(Permissions permissions) {
66bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.permissions = permissions;
67bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
68bda3441225e0607b5ced8b538123fd7c7a417910chrismair
69bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private Permissions permissions;
70bda3441225e0607b5ced8b538123fd7c7a417910chrismair
71bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
72bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * Construct a new instance without setting its path
73bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
74bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public AbstractFileSystemEntry() {
75bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
76bda3441225e0607b5ced8b538123fd7c7a417910chrismair
77bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
78bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * Construct a new instance with the specified value for its path
79bda3441225e0607b5ced8b538123fd7c7a417910chrismair     *
80bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * @param path - the value for path
81bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
82bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public AbstractFileSystemEntry(String path) {
83bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.path = path;
84bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
85bda3441225e0607b5ced8b538123fd7c7a417910chrismair
86bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
87bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * @return the path for this entry
88bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
89bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public String getPath() {
90bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return path;
91bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
92bda3441225e0607b5ced8b538123fd7c7a417910chrismair
93bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
94bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * @return the file name or directory name (no path) for this entry
95bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
96bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public String getName() {
97bda3441225e0607b5ced8b538123fd7c7a417910chrismair        int separatorIndex1 = path.lastIndexOf('/');
98bda3441225e0607b5ced8b538123fd7c7a417910chrismair        int separatorIndex2 = path.lastIndexOf('\\');
99bda3441225e0607b5ced8b538123fd7c7a417910chrismair//        int separatorIndex = [separatorIndex1, separatorIndex2].max();
100bda3441225e0607b5ced8b538123fd7c7a417910chrismair        int separatorIndex = separatorIndex1 > separatorIndex2 ? separatorIndex1 : separatorIndex2;
101bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return (separatorIndex == -1) ? path : path.substring(separatorIndex + 1);
102bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
103bda3441225e0607b5ced8b538123fd7c7a417910chrismair
104bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
105bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * Set the path for this entry. Throw an exception if pathLocked is true.
106bda3441225e0607b5ced8b538123fd7c7a417910chrismair     *
107bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * @param path - the new path value
108bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
109bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void setPath(String path) {
110bda3441225e0607b5ced8b538123fd7c7a417910chrismair        Assert.isFalse(pathLocked, "path is locked");
111bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.path = path;
112bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
113bda3441225e0607b5ced8b538123fd7c7a417910chrismair
114bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void lockPath() {
115bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.pathLocked = true;
116bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
117bda3441225e0607b5ced8b538123fd7c7a417910chrismair
118bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void setPermissionsFromString(String permissionsString) {
119bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.permissions = new Permissions(permissionsString);
120bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
121bda3441225e0607b5ced8b538123fd7c7a417910chrismair
122bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
123bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * Abstract method -- must be implemented within concrete subclasses
124bda3441225e0607b5ced8b538123fd7c7a417910chrismair     *
125bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * @return true if this file system entry represents a directory
126bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
127bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public abstract boolean isDirectory();
128bda3441225e0607b5ced8b538123fd7c7a417910chrismair
129bda3441225e0607b5ced8b538123fd7c7a417910chrismair}
130