View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.goetz.domino.log4j;
18  
19  import java.util.Date;
20  import java.util.Vector;
21  
22  import org.apache.log4j.helpers.LogLog;
23  
24  /***
25   * This class provides a container for log documents in a Domino application. 
26   * 
27   * @author Bernd G?tz
28   */
29  public class LogDocument {
30  	
31      /***
32       * The number of LoggingEvents to log in a singel Domino document.
33       */
34      protected static final int DEFAULT_MAX_LINES = 1000;
35      
36      /***
37       * Default form name.
38       */
39      protected static final String DEFAULT_FORM_NAME = "frmEvents";
40      
41      /***
42       * The upper limit in bytes where to stop writing to a document.
43       */
44      protected static final int UPPER_SIZE_LIMIT = 1024*28;
45      
46      /***
47       * The maximum to write to a document is 32kB.
48       */
49      protected static final int MAX_DOCUMENT_SIZE = 1024*31;
50  
51      /***
52       * The Domino serverName to log events to, or "" for local.
53       */
54      private String serverName = "";
55  	
56      /***
57       * The databaseName path to log events to.
58       * If set to null, the current databaseName is used as log databaseName.
59       */
60      private String databaseName;
61      
62  	private String formName;
63  	
64  	private String applicationName;
65  	
66  	private String applicationPath;
67  	
68      /***
69       * The pattern converter layout including %u for the current agent user.
70       */
71      private  String message = null;
72      
73      /***
74       * True if the message contains the variable "%u".
75       */
76      private boolean msgContainsUserVariable = false;
77      
78  	private Date startTime;
79  	
80  	private Date finishTime;
81  	
82  	private Vector events = new Vector();
83  	
84  	private int currentSize = 0;
85  	
86  	/***
87  	 * Current number of lines.
88  	 */
89  	private int currentLines = 0;
90  	
91  	/***
92  	 * Current notes document id with which this document is connected to.
93  	 */
94  	private String currentDocId;
95  	
96  	/***
97  	 * The maximum number of LoggingEvents per document.
98  	 */
99  	private int maxLines = DEFAULT_MAX_LINES;
100 
101 	private boolean isFull = false; 
102 	
103 	private boolean dirty = false; 
104 	
105     public LogDocument() {
106 		super();
107 		startTime = new Date();
108 	}
109 	
110 	public LogDocument(String name, String path, String name2, String name3, 
111 		Date time) {
112 		super();
113 		applicationName = name;
114 		applicationPath = path;
115 		formName = name2;
116 		serverName = name3;
117 		startTime = time;
118 	}
119 
120 	/***
121 	 * Resets the current log document.
122 	 * 
123 	 * @param startTime
124 	 */
125 	public void reset(Date startTime) {
126 		LogLog.debug("Resetting log document instance");
127 		events.clear();
128 		currentSize = 0;
129 		currentLines = 0;
130 		isFull = false;
131 		dirty = false;
132 		this.startTime = startTime;
133 		this.finishTime = null;
134 	}
135 
136 	public String getApplicationName() {
137 		return applicationName;
138 	}
139 
140 	public String getApplicationPath() {
141 		return applicationPath;
142 	}
143 
144 	public Date getFinishTime() {
145 		return finishTime;
146 	}
147 
148 	public void setFinishTime(Date finishTime) {
149 		this.finishTime = finishTime;
150 	}
151 
152 	public String getFormName() {
153 		if (formName == null) {
154 			return DEFAULT_FORM_NAME;
155 		} else {
156 			return formName;
157 		}
158 	}
159 
160 	public String getServerName() {
161 		return serverName;
162 	}
163 
164 	public Date getStartTime() {
165 		return startTime;
166 	}
167 
168 	public Vector getEvents() {
169 		return events;
170 	}
171 	
172 	/***
173 	 * Adds a single line to the document.
174 	 * 
175 	 * @param s
176 	 */
177 	public void add(String s) {
178 		if ((currentSize + s.length()) >= MAX_DOCUMENT_SIZE) {
179 			int l = MAX_DOCUMENT_SIZE - currentSize - 1;
180 			LogLog.debug("Cutting log entry to " + l + " characters");
181 			s = "Document full, partial information: ".concat(s).
182 				substring(l);
183 		}
184 		events.add(s);
185 		currentSize += s.length();
186 		currentLines++;
187 		dirty = true;
188         isFull = (currentSize >= UPPER_SIZE_LIMIT) || 
189         	(currentLines >= maxLines);
190         if (isFull) {
191             setFinishTime(new Date());
192         }
193 	}
194 	
195 	public int getCurrentSize() {
196 		return currentSize;
197 	}
198 	
199 	public int getCurrentLines() {
200 		return currentLines;
201 	}
202 	
203 	public void setApplicationName(String applicationName) {
204 		this.applicationName = applicationName;
205 	}
206 
207 	public void setApplicationPath(String applicationPath) {
208 		this.applicationPath = applicationPath;
209 	}
210 
211 	public void setFormName(String formName) {
212 		this.formName = formName;
213 	}
214 
215 	public void setServerName(String serverName) {
216 		this.serverName = serverName;
217 	}
218 
219 	public void setStartTime(Date startTime) {
220 		this.startTime = startTime;
221 	}
222 
223 	public String getDatabaseName() {
224 		return databaseName;
225 	}
226 
227 	public void setDatabaseName(String databaseName) {
228 		this.databaseName = databaseName;
229 	}
230 
231 	public String getMessage() {
232 		return message;
233 	}
234 
235 	/***
236 	 * Sets the message string and the 
237 	 * 
238 	 * @param message
239 	 */
240 	public void setMessage(String message) {
241 		this.message = message;
242     	this.msgContainsUserVariable = (message.indexOf("%u") >= 0);
243 	}
244 	
245 	/***
246 	 * Returns true if the current message contains the variable "%u".
247 	 * 
248 	 * The current value is determined by calling the method <code>setMessage()
249 	 * </code>.
250 	 * 
251 	 * @return true if message contains the variable "%u" 
252 	 */
253 	public boolean messageContainsUserVariable() {
254 		return msgContainsUserVariable;
255 	}
256 
257 	public int getMaxLines() {
258 		return maxLines;
259 	}
260 
261 	public void setMaxLines(int lines) {
262 		this.maxLines = lines;
263 	}
264 
265 	public boolean isFull() {
266 		return isFull;
267 	}
268 
269 	public String getCurrentDocId() {
270 		return currentDocId;
271 	}
272 
273 	public void setCurrentDocId(String currentDocId) {
274 		this.currentDocId = currentDocId;
275 	}
276 
277 	public boolean isDirty() {
278 		return dirty;
279 	}
280 
281 }