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.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
39
40
41 protected void initialize(LoggingEvent event)
42 throws NotesException {
43
44
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
51 appName = event.getLoggerName();
52 }
53 if (getThreshold() != null) {
54
55 doc.setApplicationName(appName + "." +
56 getThreshold().toString());
57 } else {
58 doc.setApplicationName(appName);
59 }
60 }
61
62
63
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
76
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
103
104
105 protected void initAppend() throws NotesException {
106
107 LogLog.debug("initAppend");
108 }
109
110
111
112
113 protected void releaseAppend() {
114
115 LogLog.debug("releaseAppend");
116 if (session != null) {
117 try {
118 LogLog.debug("recycle session");
119 session.recycle();
120 }
121 catch (NotesException e) {
122
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 }