1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
70
71
72 protected Session retrieveSession() throws NotesException {
73
74 if ((session != null) && (!session.isValid())) {
75
76 LogLog.debug("Session is not valid anymore, resetting it");
77 session = null;
78 }
79
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
92
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
109
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
130
131
132 protected void initialize(LoggingEvent event)
133 throws NotesException {
134 super.initialize(event);
135 }
136
137
138
139
140 protected void initAppend() throws NotesException {
141 }
142
143
144
145
146 protected void releaseAppend() {
147 }
148
149
150
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 }