001 package hirondelle.web4jtools.logview.directories; 002 003 import hirondelle.web4j.model.ModelCtorException; 004 import hirondelle.web4j.security.SafeText; 005 import hirondelle.web4j.model.Check; 006 import hirondelle.web4j.model.ModelUtil; 007 import hirondelle.web4j.model.Validator; 008 import java.io.*; 009 import static hirondelle.web4j.util.Consts.FAILS; 010 import static hirondelle.web4j.util.Consts.FILE_SEPARATOR; 011 import hirondelle.web4j.util.Util; 012 013 /** 014 * Basic information on logging directories. 015 * 016 * <P>This tool can examine both your applictation logs, and the logs of your 017 * server (such as Tomcat). 018 */ 019 public final class LogInfo { 020 021 /** 022 * Full constructor. 023 * 024 * @param aProjectName simple name or alias for the project, displayed as a reminder (required). 025 * @param aAppLogDir directory containing application log files (required); existing directory, ending in a separator; 026 * any files having no size (such as <tt>.lck</tt> files) will be ignored, while all other files in this directory will be considered log files. 027 * @param aServerLogDir directory containing your server's log files (required); existing directory, ending in a separator. 028 * @param aServerLogStartsWith server log files of interest have file names starting with this text (for example, <tt>'localhost'</tt>); 029 * for Tomcat, several log files are generated, but it is almost always the case that only one kind of log file has items of 030 * interest. 031 */ 032 public LogInfo(SafeText aProjectName, SafeText aAppLogDir, SafeText aServerLogDir, SafeText aServerLogStartsWith) throws ModelCtorException { 033 fProjecName = aProjectName; 034 fAppLogDir = aAppLogDir; 035 fServerLogDir = aServerLogDir; 036 fServerLogStartsWith = aServerLogStartsWith; 037 validateState(); 038 } 039 040 public SafeText getAppLoggingDirectory() { return fAppLogDir; } 041 public SafeText getProjectName() { return fProjecName; } 042 public SafeText getServerLoggingDirectory() { return fServerLogDir; } 043 public SafeText getServerLogFileStartsWith() { return fServerLogStartsWith; } 044 045 /** Intended for debugging only. */ 046 @Override public String toString() { 047 return ModelUtil.toStringFor(this); 048 } 049 050 @Override public boolean equals( Object aThat ) { 051 Boolean result = ModelUtil.quickEquals(this, aThat); 052 if ( result == null ){ 053 LogInfo that = (LogInfo) aThat; 054 result = ModelUtil.equalsFor(this.getSignificantFields(), that.getSignificantFields()); 055 } 056 return result; 057 } 058 059 @Override public int hashCode() { 060 if ( fHashCode == 0 ) { 061 fHashCode = ModelUtil.hashCodeFor(getSignificantFields()); 062 } 063 return fHashCode; 064 } 065 066 // PRIVATE // 067 private SafeText fProjecName; 068 private SafeText fAppLogDir; 069 private SafeText fServerLogDir; 070 private SafeText fServerLogStartsWith; 071 private int fHashCode; 072 073 private void validateState() throws ModelCtorException { 074 ModelCtorException ex = new ModelCtorException(); 075 if( FAILS == Check.required(fProjecName) ) { 076 ex.add("Project Name is required."); 077 } 078 if ( FAILS == Check.required(fAppLogDir, validDirectory())) { 079 ex.add("Application Logging Directory is required: an existing, read-able directory, ending with a " + Util.quote(FILE_SEPARATOR) + "."); 080 } 081 if ( FAILS == Check.required(fServerLogDir, validDirectory())) { 082 ex.add("Tomcat Logging Directory is required: an existing, read-able directory, ending with a " + Util.quote(FILE_SEPARATOR) + "."); 083 } 084 if ( FAILS == Check.required(fServerLogStartsWith)) { 085 ex.add("Tomcat Log File Starts With is required."); 086 } 087 if ( ex.isNotEmpty() ) throw ex; 088 } 089 090 private Validator validDirectory() { 091 return new Validator() { 092 public boolean isValid(Object aObject) { 093 SafeText text = (SafeText)aObject; 094 File file = new File(text.getRawString()); 095 return file.isDirectory() && file.exists() && file.canRead() && file.isAbsolute() && endsWithSeparator(text); 096 } 097 }; 098 } 099 100 private boolean endsWithSeparator(SafeText aDir){ 101 return aDir.getRawString().endsWith(FILE_SEPARATOR); 102 } 103 104 private Object[] getSignificantFields(){ 105 return new Object[] {fProjecName, fAppLogDir, fServerLogDir, fServerLogStartsWith}; 106 } 107 }