001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.admin;
009:
010: import java.util.*;
011: import java.io.*;
012:
013: import org.apache.log4j.Logger;
014:
015: import dtw.webmail.model.*;
016:
017: /**
018: * Class that implements a file status wrapper.
019: *
020: * @author Dieter Wimberger
021: * @version 0.9.7 07/02/2003
022: */
023: public class FileStatus {
024:
025: //logging
026: private static Logger log = Logger.getLogger(FileStatus.class);
027:
028: //instance attributes
029: private BitSet m_Status;
030: private String m_Filename;
031: private boolean m_Ok;
032:
033: /**
034: * Constructs a new file status instance.
035: */
036: public FileStatus(String filename) throws JwmaException {
037: m_Status = new BitSet(4);
038: m_Filename = filename;
039: gatherStatus();
040: }//constructor
041:
042: /**
043: * Returns the filename of the file that is
044: * described through this <tt>FileStatus</tt>.
045: *
046: * @return the filename of the file described by this
047: * <tt>FileStatus</tt>.
048: */
049: public String getFilename() {
050: return m_Filename;
051: }//getFilename
052:
053: /**
054: * Tests if the file is writeable.
055: *
056: * @return true if writeable, false otherwise.
057: */
058: public boolean isWriteable() {
059: return m_Status.get(WRITEABLE);
060: }//isWriteable
061:
062: /**
063: * Tests if the file is a directory.
064: *
065: * @return true if it is a directory, false otherwise.
066: */
067: public boolean isDirectory() {
068: return m_Status.get(DIRECTORY);
069: }//isDirectory
070:
071: /**
072: * Tests if the file is readable.
073: *
074: * @return true if readable, false otherwise.
075: */
076: public boolean isReadable() {
077: return m_Status.get(READABLE);
078: }//isReadable
079:
080: /**
081: * Tests if the file exists.
082: *
083: * @return true if it exists, false otherwise.
084: */
085: public boolean isExisting() {
086: return m_Status.get(EXISTS);
087: }//isExisting
088:
089: /**
090: * Returns the IS state of the file described
091: * by this <tt>FileStatus</tt>.
092: *
093: * @return the is state as <tt>String</tt>.
094: */
095: public String getIsState() {
096: StringBuffer sbuf = new StringBuffer();
097: if (isExisting()) {
098: sbuf.append('[');
099: }
100: sbuf.append(((isDirectory()) ? "D" : "!D"));
101: sbuf.append(',');
102: sbuf.append(((isReadable()) ? "R" : "!R"));
103: sbuf.append(',');
104: sbuf.append(((isWriteable()) ? "W" : "!W"));
105: if (isExisting()) {
106: sbuf.append(']');
107: }
108: return sbuf.toString();
109: }//isState
110:
111: /**
112: * Tests if the file described by this
113: * <tt>FileStatus</tt> represents the given type.
114: * <p>
115: * The types are encoded through given constants.
116: *
117: * @return true if of the given type, false otherwise.
118: */
119: public boolean isType(int TYPE) {
120: switch (TYPE) {
121: case READABLE_FILE:
122: return (!m_Status.get(DIRECTORY) && m_Status.get(EXISTS)
123: && m_Status.get(READABLE) && !m_Status
124: .get(WRITEABLE));
125: case WRITEABLE_FILE:
126: return (!m_Status.get(DIRECTORY) && m_Status.get(EXISTS)
127: && m_Status.get(READABLE) && m_Status
128: .get(WRITEABLE));
129: case READABLE_DIRECTORY:
130: return (m_Status.get(DIRECTORY) && m_Status.get(EXISTS)
131: && m_Status.get(READABLE) && !m_Status
132: .get(WRITEABLE));
133: case WRITEABLE_DIRECTORY:
134: return (m_Status.get(DIRECTORY) && m_Status.get(EXISTS)
135: && m_Status.get(READABLE) && m_Status
136: .get(WRITEABLE));
137: default:
138: return false;
139: }
140: }//isType
141:
142: /**
143: * Gathers the status of the file that is
144: * refered to by the filename of this
145: * <tt>FileStatus</tt> instance.
146: *
147: * @throws JwmaException if file checking fails.
148: */
149: private void gatherStatus() throws JwmaException {
150:
151: File testf = new File(m_Filename);
152: try {
153: if (testf.isDirectory()) {
154: m_Status.set(DIRECTORY);
155: }
156: if (testf.exists()) {
157: m_Status.set(EXISTS);
158: }
159: if (testf.canRead()) {
160: m_Status.set(READABLE);
161: }
162: if (testf.canWrite()) {
163: m_Status.set(WRITEABLE);
164: }
165: } catch (Exception ex) {
166: JwmaException jex = new JwmaException(
167: "jwma.admin.filestatus").setException(ex);
168: }
169: }//gatherStatus
170:
171: //constants
172: private static final int DIRECTORY = 0;
173: private static final int EXISTS = 1;
174: private static final int READABLE = 2;
175: private static final int WRITEABLE = 3;
176:
177: /**
178: * Represents an existing and readable file.
179: * [Exists,!D,R,!W]
180: */
181: public static final int READABLE_FILE = 0;
182:
183: /**
184: * Represents an existing and writeable file.
185: * [Exists,!D,R,W]
186: */
187: public static final int WRITEABLE_FILE = 1;
188:
189: /**
190: * Represents an existing and readable directory.
191: * [Exists,D,R,!W]
192: */
193: public static final int READABLE_DIRECTORY = 2;
194:
195: /**
196: * Represents an existing and writeable directory.
197: * [Exists,D,R,W]
198: */
199: public static final int WRITEABLE_DIRECTORY = 3;
200:
201: public static final String[] SHOULD_STATE = { "[!D,R,!W]",
202: "[!D,R,W]", "[D,R,!W]", "[D,R,W]" };
203:
204: }//class FileStatus
|