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.NotesFactory;
22  import lotus.domino.Session;
23  
24  import org.apache.log4j.MDC;
25  import org.apache.log4j.helpers.LogLog;
26  import org.apache.log4j.spi.LoggingEvent;
27  
28  /***
29   * This appender can be used outside of the Domino JVM, e.g. in a Tomcat 
30   * or in a Websphere Application Server container.
31   * 
32   * @author Bernd G?tz
33   */
34  public class RemoteAppender extends DominoAppender {
35  	
36  	/***
37  	 * Cached session object per appender.
38  	 */
39  	private Session session = null;
40  	
41  	/***
42  	 * User name to connect to Domino server via IIOP.
43  	 */
44  	private String connectUserName = "";
45  	
46  	/***
47  	 * User password to connect to Domino server via IIOP.
48  	 */
49  	private String connectPassword = "";
50  	
51      /***
52       * Sets the user name.
53       *
54       * @param connectUserName the user name to connect to the server.
55       */
56      public void setConnectUserName(String userName) {
57          this.connectUserName = userName;
58      }
59  
60      /***
61       * Sets the user password.
62       *
63       * @param connectPassword the user password to connect to the server.
64       */
65      public void setConnectPassword(String userPassword) {
66      	this.connectPassword = userPassword;
67      }
68  
69  	/* (non-Javadoc)
70  	 * @see org.goetz.domino.log4j.AbstractAppender#getSession()
71  	 */
72  	protected Session retrieveSession() throws NotesException {
73  		
74  		if ((session != null) && (!session.isValid())) {
75  			// reset the session object
76  			LogLog.debug("Session is not valid anymore, resetting it");
77  			session = null;
78  		}
79  		// logging:
80  		if (session == null) {
81  			LogLog.debug("Create a session using server name '" +
82  					doc.getServerName() + "'and technical user id '" +
83  					connectUserName + "'");
84  			if ((connectPassword != null) && (connectPassword.length() > 0)) {
85  				LogLog.debug("Password is not null or empty");
86  			} else {
87  				LogLog.debug("Password is null or empty");
88  			}
89  			session = NotesFactory.createSession(doc.getServerName(), 
90  				connectUserName, connectPassword);
91  			// TODO: for local connects using the domino client, does a 
92  			// call to createSessionWithFullAccess(.) works also?
93  		} else {
94  			LogLog.debug("Returning cached session object");
95  		}
96  		return session;
97  	}
98  	
99      /***
100      * Returns the string "Servlet"
101      * 
102      * @return application path name
103      */
104     protected String getApplicationPath() {
105     	return "Remote";
106     }
107 
108 	/* (non-Javadoc)
109 	 * @see org.goetz.domino.log4j.AbstractAppender#getDominoDatabase(lotus.domino.Session)
110 	 */
111 	protected Database getDominoDatabase(Session session) 
112 		throws NotesException {
113 		
114 		Database db = null;
115 		String databaseName = doc.getDatabaseName();
116 		if (databaseName == null) {
117 			throw new NotesException(0, "Database must be specified");
118 		}
119 		LogLog.debug("dbPath [" + databaseName + "]");
120 		LogLog.debug("Trying to get [" + databaseName + "] via IIOP");
121 		db = session.getDatabase(null, databaseName);
122 		if (!db.isOpen()) {
123 			LogLog.debug("Log databaseName is closed. Trying to open it.");
124 			db.open();
125 		}
126 		return db;
127 	}
128 
129 	/* (non-Javadoc)
130 	 * @see org.goetz.domino.log4j.AbstractAppender#initializeAppNameAndPath(org.apache.log4j.spi.LoggingEvent)
131 	 */
132 	protected void initialize(LoggingEvent event) 
133 		throws NotesException {
134 		super.initialize(event);
135 	}
136 	
137 	/* (non-Javadoc)
138 	 * @see org.goetz.domino.log4j.AbstractAppender#initAppend()
139 	 */
140 	protected void initAppend() throws NotesException {
141 	}
142 
143     /* (non-Javadoc)
144      * @see org.goetz.domino.log4j.AbstractAppender#releaseAppend()
145      */
146     protected void releaseAppend() {
147     }
148     
149     /* (non-Javadoc)
150      * @see org.goetz.domino.log4j.AbstractAppender#retrieveUserName()
151      */
152     public String retrieveUserName() {
153     	String id = (String)MDC.get("id");
154     	if (id == null) {
155     		return "anonymous";
156     	} else {
157     		return id;
158     	}
159     }
160 
161 }