001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: EJBSinkEJB.java,v 1.8 2006/10/07 20:41:34 mlipp Exp $
021: *
022: * $Log: EJBSinkEJB.java,v $
023: * Revision 1.8 2006/10/07 20:41:34 mlipp
024: * Merged J2EE 1.4 adaptions from test branch.
025: *
026: * Revision 1.7 2006/09/29 12:32:13 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.6 2005/04/22 15:10:49 drmlipp
030: * Merged changes from 1.3 branch up to 1.3p15.
031: *
032: * Revision 1.4.4.2 2005/04/13 16:14:06 drmlipp
033: * Optimized db access.
034: *
035: * Revision 1.5 2005/04/08 11:28:03 drmlipp
036: * Merged changes from 1.3 branch up to 1.3p6.
037: *
038: * Revision 1.4.4.1 2005/04/04 20:08:59 drmlipp
039: * Changed WLS transaction isolation.
040: *
041: * Revision 1.4 2004/09/17 14:37:07 drmlipp
042: * More properties for J2EESDK port.
043: *
044: * Revision 1.3 2004/09/10 12:44:28 drmlipp
045: * Enabled call by reference for weblogic by default.
046: *
047: * Revision 1.2 2004/08/19 13:24:48 drmlipp
048: * Fixed AVK errors and (many) warnings.
049: *
050: * Revision 1.1.1.6 2004/08/18 15:17:35 drmlipp
051: * Update to 1.2
052: *
053: * Revision 1.22 2004/06/23 15:06:38 lipp
054: * Started JOnAS port.
055: *
056: * Revision 1.21 2004/01/14 07:59:44 lipp
057: * Added transaction isolation attribute for WLS.
058: *
059: * Revision 1.20 2003/10/01 14:20:22 lipp
060: * Adjusted weblogic datasource name.
061: *
062: * Revision 1.19 2003/09/28 19:21:09 lipp
063: * More improvements concerning security handling.
064: *
065: * Revision 1.18 2003/09/11 13:25:49 lipp
066: * Improved naming scheme for symbolic role names.
067: *
068: * Revision 1.17 2003/09/03 15:24:17 lipp
069: * Fixed view type specification.
070: *
071: * Revision 1.16 2003/08/22 13:05:04 lipp
072: * Better data source name.
073: *
074: * Revision 1.15 2003/06/27 08:51:46 lipp
075: * Fixed copyright/license information.
076: *
077: * Revision 1.14 2003/06/04 13:15:50 lipp
078: * Optimized resource allocation/caching.
079: *
080: * Revision 1.13 2003/05/23 15:42:41 lipp
081: * Fixed deployment unit dependencies.
082: *
083: * Revision 1.12 2003/01/15 17:36:13 robert
084: * Workaround for NoSuchMethodError by LoggingEvent.getLevel
085: *
086: * Revision 1.11 2003/01/15 15:28:31 lipp
087: * Added permissions.
088: *
089: * Revision 1.10 2003/01/14 11:53:44 robert
090: * Rename column Timestamp of the table LogMessages to LogTime.
091: *
092: * Revision 1.9 2002/11/26 11:23:30 lipp
093: * Modified RemoteException comment.
094: *
095: * Revision 1.8 2002/11/15 15:15:37 montag
096: * Generation of EJBSink-Classes and -DD with xdoclet.
097: *
098: * Revision 1.7 2002/09/04 06:57:37 lipp
099: * Now using JBoss-3.0
100: *
101: * Revision 1.6 2002/01/11 09:30:59 robert
102: * rename table ErrorLog to LogMessages
103: *
104: * Revision 1.5 2002/01/10 10:19:09 robert
105: * resumes database connection on connection failed.
106: *
107: * Revision 1.4 2002/01/09 17:18:48 robert
108: * add NDC to the SQL Statement
109: *
110: * Revision 1.3 2002/01/09 15:44:54 robert
111: * modify the SQL Statement
112: *
113: * Revision 1.2 2002/01/09 09:59:13 robert
114: * javadoc
115: *
116: * Revision 1.1 2002/01/09 09:21:14 robert
117: * new EJB for EJBAppender
118: *
119: *
120: */
121: package de.danet.an.util.log4j;
122:
123: import java.sql.Connection;
124: import java.sql.PreparedStatement;
125: import java.sql.SQLException;
126:
127: import javax.ejb.CreateException;
128: import javax.ejb.EJBException;
129: import javax.ejb.SessionBean;
130: import javax.ejb.SessionContext;
131: import javax.naming.NamingException;
132: import javax.sql.DataSource;
133:
134: import org.apache.log4j.spi.LoggingEvent;
135:
136: import de.danet.an.util.JDBCUtil;
137: import de.danet.an.util.UniversalPrepStmt;
138:
139: /**
140: * This session EJB provides the server side part of the
141: * {@link de.danet.an.util.log4j.EJBAppender}.
142: *
143: * @ejbHome <{de.danet.an.util.log4j.EJBSinkHome}>
144: * @ejbRemote <{de.danet.an.util.log4j.EJBSink}>
145: * @see de.danet.an.util.log4j
146: *
147: * @ejb.bean name="EJBSink" display-name="EJBSink"
148: * jndi-name="ejb/@@@_Utility-EJBs_EJBSinkEJB_JNDI_Name_@@@"
149: * type="Stateless" transaction-type="Container" view-type="remote"
150: * @jonas.bean ejb-name="EJBSink"
151: * @ejb.home remote-class="de.danet.an.util.log4j.EJBSinkHome"
152: * @ejb.interface remote-class="de.danet.an.util.log4j.EJBSink"
153: * @ejb.resource-ref res-ref-name="jdbc/Logging"
154: * res-type="javax.sql.DataSource" res-auth="Container"
155: * jndi-name="DefaultDS"
156: * @jonas.resource res-ref-name="jdbc/Logging" jndi-name="jdbc_1"
157: * @weblogic.enable-call-by-reference True
158: * @weblogic.resource-description res-ref-name="jdbc/Logging"
159: * jndi-name="DefaultDS"
160: * @weblogic.transaction-isolation TRANSACTION_READ_COMMITTED
161: */
162: public class EJBSinkEJB implements SessionBean {
163:
164: /** The SessionContext interface of the instance. */
165: private SessionContext ctx;
166:
167: /**
168: * The data source of the database.
169: * @see javax.sql.DataSource
170: */
171: private DataSource ds = null;
172:
173: /** Database attributes */
174: private static final String DB_NAME = "java:comp/env/jdbc/Logging";
175:
176: /**
177: * Set the session context.
178: * @param context session context
179: * @throws EJBException if error
180: */
181: public void setSessionContext(SessionContext context)
182: throws EJBException {
183: ctx = context;
184: try {
185: ds = JDBCUtil.refreshDS(null, DB_NAME);
186: } catch (NamingException ne) {
187: throw new EJBException(ne);
188: }
189: }
190:
191: /**
192: * Not called for stateless session beans.
193: * @see javax.ejb.SessionBean
194: */
195: public void ejbActivate() throws EJBException {
196: }
197:
198: /**
199: * Not called for stateless session beans.
200: * @see javax.ejb.SessionBean
201: */
202: public void ejbPassivate() throws EJBException {
203: }
204:
205: /**
206: * A container invokes this method before it ends the life of the session
207: * object. This happens as a result of a client's invoking a remove
208: * operation, or when a container decides to terminate the session object
209: * after a timeout.
210: * @see javax.ejb.SessionBean
211: */
212: public void ejbRemove() {
213: ds = null;
214: ctx = null;
215: }
216:
217: /**
218: * Create a new instance of the EJBSinkEJB.
219: *
220: * @throws CreateException if the EJB cannot be create.
221: */
222: public void ejbCreate() throws CreateException {
223:
224: }
225:
226: /**
227: * Insert the event information into the database.
228: * @param event the event including the message to be logged.
229: * @ejb.interface-method
230: * @ejb.transaction
231: * type="RequiresNew"
232: */
233: public void append(LoggingEvent event) {
234: Connection con = null;
235: PreparedStatement prepStmt = null;
236: try {
237: con = ds.getConnection();
238: prepStmt = new UniversalPrepStmt(
239: ds,
240: con,
241: "INSERT INTO LogMessages "
242: + "(Prio, Message, Category, Thread, NDC, LogTime) "
243: + "VALUES (?, ?, ?, ?, ?, ?)");
244: int offset = 1;
245: prepStmt.setString(offset++, "INFO");
246: //event.getLevel().toString());//'INFO'
247: prepStmt.setString(offset++, event.getRenderedMessage());
248: prepStmt.setString(offset++, event.categoryName);//'log4j.Basic'
249: prepStmt.setString(offset++, event.getThreadName());//'main'
250: prepStmt.setString(offset++, event.getNDC());//NDC
251: prepStmt.setTimestamp(offset++, new java.sql.Timestamp(
252: event.timeStamp));
253: prepStmt.executeUpdate();
254: } catch (SQLException se) {
255: throw new EJBException(se);
256: } finally {
257: try {
258: JDBCUtil.closeAll(null, prepStmt, con);
259: } catch (SQLException e) {
260: // errors in loggers, that's difficult, we don't want loops...
261: System.err.println("Problem in EJBSinkEJB: "
262: + e.getMessage());
263: e.printStackTrace();
264: }
265: }
266: }
267:
268: /**
269: * Prepared the database connection and the PreparedStatement
270: * for the database access.
271: */
272: private void prepareDatabaseConnection() throws SQLException {
273: }
274:
275: }
|