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 org.apache.log4j.helpers.LogLog;
20  
21  import lotus.domino.NotesException;
22  import lotus.domino.NotesFactory;
23  import lotus.domino.NotesThread;
24  import lotus.domino.Session;
25  
26  /***
27   * This appender can be used for servlets in the Domino servlet container.
28   * 
29   * @author Bernd G?tz
30   */
31  public class ServletAppender extends DominoAppender {
32  	
33  	/***
34  	 * Cached session object per appender.
35  	 */
36  	private Session session = null;
37  	
38  	/* (non-Javadoc)
39  	 * @see org.goetz.domino.log4j.AbstractAppender#getSession()
40  	 */
41  	protected Session retrieveSession() throws NotesException {
42  		if ((session != null) && (!session.isValid())) {
43  			// reset the session object
44  			LogLog.debug("Session is not valid anymore, resetting it");
45  			session = null;
46  		}
47  		// logging:
48  		if (session == null) {
49  			LogLog.debug("Call NotesFactory.createSession()...");
50  			session = NotesFactory.createSession();
51  		} else {
52  			LogLog.debug("Returning cached session object");
53  		}
54  		return session;
55  	}
56  
57      /* (non-Javadoc)
58       * @see org.goetz.domino.log4j.DominoAppender#getApplicationPath()
59       */
60      protected String getApplicationPath() {
61      	return "DominoServlet";
62      }
63      
64  	/* (non-Javadoc)
65  	 * @see org.goetz.domino.log4j.AbstractAppender#initAppend()
66  	 */
67  	protected void initAppend() throws NotesException {
68  		NotesThread.sinitThread();
69  	}
70  
71      /* (non-Javadoc)
72       * @see org.goetz.domino.log4j.AbstractAppender#releaseAppend()
73       */
74      protected void releaseAppend() {
75      	// dont call stermThread because this would invalidate the session 
76      	// object.
77      }
78      
79      /* (non-Javadoc)
80       * @see org.goetz.domino.log4j.AbstractAppender#retrieveUserName()
81       */
82      public String retrieveUserName() throws NotesException {
83      	return retrieveSession().getUserName();
84      }
85      
86      /* (non-Javadoc)
87       * @see org.goetz.domino.log4j.AbstractAppender#close()
88       */
89      public void close() {
90      	super.close();
91      	if ((session != null)) {
92      		if (session.isValid()) {
93      			LogLog.debug("Session is still valid");
94      		} else {
95      			LogLog.debug("Session is not valid");
96      		}
97      		try {
98      			LogLog.debug("Recycling session now");
99      			session.recycle();
100     		}
101     		catch (NotesException e) {
102     			LogLog.debug("Got Notes exception: " + e.getMessage());
103     			// do nothing
104     		}
105     	} else {
106    			LogLog.debug("Session is null");
107     	}
108     }
109     
110 }