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 lotus.domino.Agent;
20  import lotus.domino.AgentBase;
21  import lotus.domino.AgentContext;
22  import lotus.domino.Database;
23  import lotus.domino.NotesException;
24  import lotus.domino.Session;
25  
26  import org.apache.log4j.helpers.LogLog;
27  import org.apache.log4j.spi.LoggingEvent;
28  
29  /***
30   * This appender can be used in Domino Agents.
31   * 
32   * @author Bernd G?tz
33   */
34  public class AgentAppender extends AbstractAppender {
35  	
36  	private Session session = null;
37  	
38  	/* (non-Javadoc)
39  	 * @see org.goetz.domino.log4j.AbstractAppender#initializeAppNameAndPath(org.apache.log4j.spi.LoggingEvent)
40  	 */
41  	protected void initialize(LoggingEvent event) 
42  		throws NotesException {
43  
44          // set application information:
45  	    Agent agent = retrieveSession().getAgentContext().getCurrentAgent();
46  	    doc.setApplicationPath(agent.getParent().getFilePath());
47  	    agent.recycle();
48  	    String appName = doc.getApplicationName();
49  	    if ((appName == null) || (appName.length() == 0)) {
50  	    	// get the logger name from the event 
51  	    	appName = event.getLoggerName();
52  	    }
53  	    if (getThreshold() != null) {
54  	    	// add the threshold to the name:
55  	    	doc.setApplicationName(appName + "." + 
56  	    		getThreshold().toString());
57  	    } else {
58  	    	doc.setApplicationName(appName);
59  	    }
60  	}
61  
62  	/* (non-Javadoc)
63  	 * @see org.goetz.domino.log4j.AbstractAppender#getSession()
64  	 */
65  	protected Session retrieveSession() throws NotesException {
66  		if (session == null) {
67  			LogLog.debug("Call AgentBase.getAgentSession()");
68  			session = AgentBase.getAgentSession();
69  		} else {
70  			LogLog.debug("Returning cached session object");
71  		}
72  		return session;
73  	}
74  	
75  	/* (non-Javadoc)
76       * @see org.goetz.domino.log4j.AbstractAppender#getDominoDatabase(lotus.domino.Session)
77       */
78      protected Database getDominoDatabase(Session session) 
79      	throws NotesException {
80      	
81      	LogLog.debug("Return the Domino database");
82      	
83  		Database db = null;
84  		
85  		String databaseName = doc.getDatabaseName();
86  		String serverName = doc.getServerName();
87  
88          if (databaseName == null) {
89          	LogLog.debug("Trying to get current databaseName...");
90              AgentContext agentContext = session.getAgentContext();
91              db = agentContext.getCurrentDatabase();
92          } else {
93      		db = session.getDatabase(serverName, databaseName);
94          }
95  		if (!db.isOpen()) {
96  			LogLog.debug("Log databaseName is closed. Trying to open it.");
97  			db.open();
98  		}
99          return db;
100     }
101 
102     /* (non-Javadoc)
103      * @see org.goetz.domino.log4j.AbstractAppender#initAppend()
104      */
105     protected void initAppend() throws NotesException {
106     	// nothing to initialize the append
107     	LogLog.debug("initAppend");
108     }
109     
110     /* (non-Javadoc)
111      * @see org.goetz.domino.log4j.AbstractAppender#releaseAppend()
112      */
113     protected void releaseAppend() {
114     	// we must set the member variable session to null: 
115     	LogLog.debug("releaseAppend");
116     	if (session != null) {
117     		try {
118     			LogLog.debug("recycle session");
119         		session.recycle();
120     		}
121     		catch (NotesException e) {
122     			// TODO: recycle failed, what needs to be done here?
123     			LogLog.debug("got exception: " + e.getMessage());
124     		}
125     	};
126     	session = null;
127     }
128     
129     public String retrieveUserName() throws NotesException {
130     	return retrieveSession().getAgentContext().getEffectiveUserName();
131     }
132     
133 }