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.Database;
20  import lotus.domino.NotesException;
21  import lotus.domino.Session;
22  
23  import org.apache.log4j.helpers.LogLog;
24  import org.apache.log4j.spi.LoggingEvent;
25  
26  /***
27   * This appender is the basis for servlet based appenders.
28   * 
29   * @author Bernd G?tz
30   */
31  abstract public class DominoAppender extends AbstractAppender {
32  	
33  	/* (non-Javadoc)
34  	 * @see org.goetz.domino.log4j.AbstractAppender#initializeAppNameAndPath(org.apache.log4j.spi.LoggingEvent)
35  	 */
36  	protected void initialize(LoggingEvent event) 
37  		throws NotesException {
38  		doc.setApplicationPath(getApplicationPath());
39  	    String appName = doc.getApplicationName();
40  	    if ((appName == null) || (appName.length() == 0)) {
41  	    	// get the logger name from the event 
42  	    	appName = event.getLoggerName();
43  	    }
44  	    if (getThreshold() != null) {
45  	    	// add the threshold to the name:
46  	    	doc.setApplicationName(appName + "." + 
47  	    		getThreshold().toString());
48  	    } else {
49  	    	doc.setApplicationName(appName);
50  	    }
51  	}
52  	
53  	/* (non-Javadoc)
54       * @see org.goetz.domino.log4j.AbstractAppender#getDominoDatabase(lotus.domino.Session)
55       */
56      protected Database getDominoDatabase(Session session) 
57      	throws NotesException {
58      	
59  		Database db = null;
60  		String databaseName = doc.getDatabaseName();
61  		String serverName = doc.getServerName();
62  		if (databaseName == null) {
63  			throw new NotesException(0, "Database must be specified");
64  		}
65  		LogLog.debug("dbPath [" + databaseName + "]");
66  		LogLog.debug("Trying to get [" + databaseName + "] on "
67  				+ (serverName.equals("") ? "local" : serverName));
68  		db = session.getDatabase(serverName, databaseName);
69  		if (!db.isOpen()) {
70  			LogLog.debug("Log databaseName is closed. Trying to open it.");
71  			db.open();
72  		}
73  		return db;
74      }
75  
76      /***
77       * Returns the name for a specific appender type.
78       * 
79       * @return application path name
80       */
81      abstract String getApplicationPath();
82      
83  }