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.io.*;
011:
012: import org.apache.log4j.Logger;
013:
014: import dtw.webmail.util.*;
015: import dtw.webmail.model.*;
016: import dtw.webmail.JwmaKernel;
017:
018: /**
019: * Class that implements a wrapper for and a proxy to available
020: * status information.
021: * <p>
022: *
023: * @author Dieter Wimberger
024: * @version 0.9.7 07/02/2003
025: */
026: public class JwmaStatus {
027:
028: //class attributes
029: private static JwmaStatus c_Self;
030: //logging
031: private static Logger log = Logger.getLogger(JwmaStatus.class);
032:
033: //instance attributes and associations
034: //private JwmaSettings m_Settings;
035: private int m_KernelStatus;
036: private String m_KernelReport;
037: private FileStatus[] m_DirStatus;
038: private FileStatus[] m_FileStatus;
039:
040: /**
041: * Constructs a new <tt>JwmaStatus</tt> instance.
042: */
043: private JwmaStatus() throws JwmaException {
044: c_Self = this ;
045: //m_Settings = JwmaKernel.getReference().getSettings();
046: //prepareStatus();
047: }//JwmaStatus
048:
049: /**
050: * Returns the number of observed directories as
051: * <tt>int</tt>.
052: *
053: * @return the number of observed directories as <tt>int</tt>.
054: */
055: public int getDirectoryCount() {
056: if (m_DirStatus != null) {
057: return m_DirStatus.length;
058: }
059: return 0;
060: }//getDirectoryCount
061:
062: /**
063: * Tests if the given directory is in the prescribed state.
064: * <p>
065: * The states are encoded within the
066: * <tt>FileStatus</tt> class as constants, and prescribed
067: * for the observed directories in the <tt>DIRECTORY_TYPES</tt>
068: * constant.
069: *
070: * @param directory, the index of the directory as <tt>int</tt>.
071: *
072: * @return true if the given directory is in prescribed state,
073: * false otherwise.
074: *
075: * @see #DIRECTORY_TYPES
076: * @see dtw.webmail.admin.FileStatus#READABLE_DIRECTORY
077: * @see dtw.webmail.admin.FileStatus#WRITEABLE_DIRECTORY
078: */
079: public boolean isDirectoryOk(int directory) {
080: return m_DirStatus[directory]
081: .isType(DIRECTORY_TYPES[directory]);
082: }//isDirectoryOk
083:
084: /**
085: * Returns the resolve hint, if the directory is not
086: * in the prescribed state.
087: * <p>
088: * The method returns an empty string if the directory is
089: * in the prescribed state.
090: *
091: * @param directory, the index of the directory as <tt>int</tt>.
092: *
093: * @return the resolve hint as <tt>String</tt>.
094: */
095: public String getDirResolveHint(int directory) {
096: if (!isDirectoryOk(directory)) {
097: return m_DirStatus[directory].getIsState()
098: + " => "
099: + FileStatus.SHOULD_STATE[DIRECTORY_TYPES[directory]];
100: } else {
101: return "";
102: }
103: }//getDirectoryReport
104:
105: /**
106: * Returns the full path of the directory as <tt>String</tt>.
107: *
108: * @param directory, the index of the directory as <tt>int</tt>.
109: *
110: * @return the full path as <tt>String</tt>.
111: */
112: public String getDirectoryPath(int directory) {
113: return m_DirStatus[directory].getFilename();
114: }//getDirectoryPath
115:
116: /**
117: * Returns the number of observed files as
118: * <tt>int</tt>.
119: *
120: * @return the number of observed files as <tt>int</tt>.
121: */
122: public int getFileCount() {
123: if (m_FileStatus != null) {
124: return m_FileStatus.length;
125: }
126: return 0;
127: }//getFileCount
128:
129: /**
130: * Tests if the given file is in the prescribed state.
131: * <p>
132: * The states are encoded within the
133: * <tt>FileStatus</tt> class as constants, and prescribed
134: * for the observed directories in the <tt>FILE_TYPES</tt>
135: * constant.
136: *
137: * @param file, the index of the file as <tt>int</tt>.
138: *
139: * @return true if the given file is in prescribed state,
140: * false otherwise.
141: *
142: * @see #FILE_TYPES
143: * @see dtw.webmail.admin.FileStatus#READABLE_FILE
144: * @see dtw.webmail.admin.FileStatus#WRITEABLE_FILE
145: */
146: public boolean isFileOk(int file) {
147: //all errormessages are treated as 5
148: return m_FileStatus[file].isType(FILE_TYPES[((file > 3) ? 3
149: : file)]);
150: }//isFileOk
151:
152: /**
153: * Returns the resolve hint, if the file is not
154: * in the prescribed state.
155: * <p>
156: * The method returns an empty string if the directory is
157: * in the prescribed state.
158: *
159: * @param file, the index of the file as <tt>int</tt>.
160: *
161: * @return the resolve hint as <tt>String</tt>.
162: */
163: public String getFileResolveHint(int file) {
164: if (!isFileOk(file)) {
165: return m_FileStatus[file].getIsState()
166: + " => "
167: + FileStatus.SHOULD_STATE[FILE_TYPES[((file > 3) ? 3
168: : file)]];
169: } else {
170: return "";
171: }
172: }//getFileReport
173:
174: /**
175: * Returns the full path of the file as <tt>String</tt>.
176: *
177: * @param file, the index of the file as <tt>int</tt>.
178: *
179: * @return the full path as <tt>String</tt>.
180: */
181: public String getFilePath(int file) {
182: return m_FileStatus[file].getFilename();
183: }//getFilePath
184:
185: /**
186: * Tests if the kernel is up an running completely.
187: * If this test returns false, the <tt>getKernelStatus()</tt>
188: * method can be used to obtain status information.
189: *
190: * @return true if the kernel is up and running error free,
191: * false otherwise.
192: *
193: * @see #getKernelStatus()
194: */
195: public boolean isKernelOk() {
196: return (m_KernelStatus > 9);
197: }//isKernelOk
198:
199: /**
200: * Returns the status of the kernel as <tt>int</tt>.
201: * The information content of this number can be obtained from
202: * the documentation (Deployment-> Boot Process) or
203: * <a href="http://jwma.sourceforge.net/deployment/kernel.html" target="_top">
204: * online</a>.
205: *
206: * @return an <tt>int</tt> describing the state of the kernel.
207: */
208: public int getKernelStatus() {
209: return m_KernelStatus;
210: }//getKernelStatus
211:
212: /**
213: * Returns the site's post office hostaddress
214: * setting.
215: *
216: * @return local site's post office hostaddress
217: * as <tt>String</tt>.
218: */
219: public String getPostOfficeHost() {
220: return "";//JwmaKernel.getReference().getPostOfficeHost();
221: }//getPostOfficeHost
222:
223: /**
224: * Returns the site's MTA hostaddress
225: * setting.
226: *
227: * @return local site's post MTA hostaddress
228: * as <tt>String</tt>.
229: */
230: public String getMTAHost() {
231: return System.getProperty("mail.smtp.host");
232: }//getMTAHost
233:
234: /**
235: * Tests if the preset post office supports maildir style
236: * mixture of messages and subfolders.
237: *
238: * @return true if mixed mode store, false otherwise.
239: */
240: public boolean supportsMixedFolders() {
241: return false;
242: //return JwmaKernel.getReference().isPostOfficeStoreMixed();
243: }//supportsMixedFolders
244:
245: /**
246: * Returns an <tt>String</tt> representing the maximum
247: * size allowed for a message to be transported via jwma.
248: *
249: * @return maximum size of a message to be transported as
250: * <tt>String</tt>.
251: */
252: public String getMailTransportLimit() {
253: //int size = JwmaKernel.getReference()
254: // .getSettings().getMailTransportLimit();
255: /*
256: int steps = 0;
257: while (size > 1024) {
258: size = size / 1024;
259: steps++;
260: }
261: if (steps == 0) {
262: return size + " kB";
263: } else if (steps == 1) {
264: return size + " MB";
265: } else {
266: return "Too large.";
267: }
268: */
269: return "";
270: }//getTransportSizeLimit
271:
272: /**
273: * Returns a dump of the loaded settings file.
274: *
275: * @return the dump of the loaded settings file as <tt>String</tt>.
276: */
277: public String getSettingsDump() {
278: //return JwmaKernel.getReference()
279: // .getSettings().getDump();
280: return "";
281: }//getSettingsDump
282:
283: /**
284: * Prepares the status by gathering information.
285: */
286: private void prepareStatus() throws JwmaException {
287:
288: //Kernel status
289: m_KernelStatus = JwmaKernel.getReference().getKernelStatus();
290:
291: //Directories status
292: String[] paths = JwmaKernel.getReference().listDirectories();
293: m_DirStatus = new FileStatus[paths.length];
294:
295: for (int i = 0; i < paths.length; i++) {
296: m_DirStatus[i] = new FileStatus(paths[i]);
297: }
298:
299: //Files status
300: paths = JwmaKernel.getReference().listFiles();
301: m_FileStatus = new FileStatus[paths.length];
302:
303: for (int i = 0; i < paths.length; i++) {
304: m_FileStatus[i] = new FileStatus(paths[i]);
305: }
306: }//prepareStatus
307:
308: /**
309: * Creates the <tt>JwmaStatus</tt> singleton
310: * instance.
311: *
312: * @return the newly created singleton instance.
313: *
314: * @throws JwmaException if the status can not be prepared.
315: */
316: public static JwmaStatus createJwmaStatus() throws JwmaException {
317:
318: return new JwmaStatus();
319: }//createJwmaStatus
320:
321: /**
322: * Returns the reference to the singleton instance.
323: */
324: public static JwmaStatus getReference() {
325: return c_Self;
326: }//getReference
327:
328: /**
329: * Prescription of the SHOULD STATE of directories.
330: */
331: public static final int[] DIRECTORY_TYPES = {
332: FileStatus.WRITEABLE_DIRECTORY, //ROOT
333: FileStatus.WRITEABLE_DIRECTORY, //ETC
334: FileStatus.WRITEABLE_DIRECTORY, //LOG
335: FileStatus.WRITEABLE_DIRECTORY //DATA
336: };
337:
338: /**
339: * Prescription of the SHOULD STATE of files.
340: */
341: public static final int[] FILE_TYPES = { FileStatus.WRITEABLE_FILE, //SETTINGS
342: FileStatus.WRITEABLE_FILE, //SYSLOG
343: FileStatus.WRITEABLE_FILE, //DEBUGLOG
344: //FileStatus.READABLE_FILE, //PREF_MAPPING
345: //FileStatus.WRITEABLE_FILE, //PREF_TEMPLATE
346: FileStatus.READABLE_FILE //ERRORMESSAGES FILES
347: };
348:
349: }//JwmaStatus
|