/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: EJBSinkEJB.java 2550 2007-10-22 14:00:54Z $
*
* $Log$
* Revision 1.7 2006/09/29 12:32:13 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.6 2005/04/22 15:10:49 drmlipp
* Merged changes from 1.3 branch up to 1.3p15.
*
* Revision 1.4.4.2 2005/04/13 16:14:06 drmlipp
* Optimized db access.
*
* Revision 1.5 2005/04/08 11:28:03 drmlipp
* Merged changes from 1.3 branch up to 1.3p6.
*
* Revision 1.4.4.1 2005/04/04 20:08:59 drmlipp
* Changed WLS transaction isolation.
*
* Revision 1.4 2004/09/17 14:37:07 drmlipp
* More properties for J2EESDK port.
*
* Revision 1.3 2004/09/10 12:44:28 drmlipp
* Enabled call by reference for weblogic by default.
*
* Revision 1.2 2004/08/19 13:24:48 drmlipp
* Fixed AVK errors and (many) warnings.
*
* Revision 1.1.1.6 2004/08/18 15:17:35 drmlipp
* Update to 1.2
*
* Revision 1.22 2004/06/23 15:06:38 lipp
* Started JOnAS port.
*
* Revision 1.21 2004/01/14 07:59:44 lipp
* Added transaction isolation attribute for WLS.
*
* Revision 1.20 2003/10/01 14:20:22 lipp
* Adjusted weblogic datasource name.
*
* Revision 1.19 2003/09/28 19:21:09 lipp
* More improvements concerning security handling.
*
* Revision 1.18 2003/09/11 13:25:49 lipp
* Improved naming scheme for symbolic role names.
*
* Revision 1.17 2003/09/03 15:24:17 lipp
* Fixed view type specification.
*
* Revision 1.16 2003/08/22 13:05:04 lipp
* Better data source name.
*
* Revision 1.15 2003/06/27 08:51:46 lipp
* Fixed copyright/license information.
*
* Revision 1.14 2003/06/04 13:15:50 lipp
* Optimized resource allocation/caching.
*
* Revision 1.13 2003/05/23 15:42:41 lipp
* Fixed deployment unit dependencies.
*
* Revision 1.12 2003/01/15 17:36:13 robert
* Workaround for NoSuchMethodError by LoggingEvent.getLevel
*
* Revision 1.11 2003/01/15 15:28:31 lipp
* Added permissions.
*
* Revision 1.10 2003/01/14 11:53:44 robert
* Rename column Timestamp of the table LogMessages to LogTime.
*
* Revision 1.9 2002/11/26 11:23:30 lipp
* Modified RemoteException comment.
*
* Revision 1.8 2002/11/15 15:15:37 montag
* Generation of EJBSink-Classes and -DD with xdoclet.
*
* Revision 1.7 2002/09/04 06:57:37 lipp
* Now using JBoss-3.0
*
* Revision 1.6 2002/01/11 09:30:59 robert
* rename table ErrorLog to LogMessages
*
* Revision 1.5 2002/01/10 10:19:09 robert
* resumes database connection on connection failed.
*
* Revision 1.4 2002/01/09 17:18:48 robert
* add NDC to the SQL Statement
*
* Revision 1.3 2002/01/09 15:44:54 robert
* modify the SQL Statement
*
* Revision 1.2 2002/01/09 09:59:13 robert
* javadoc
*
* Revision 1.1 2002/01/09 09:21:14 robert
* new EJB for EJBAppender
*
*
*/
package de.danet.an.util.log4j;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.spi.LoggingEvent;
import de.danet.an.util.JDBCUtil;
import de.danet.an.util.UniversalPrepStmt;
/**
* This session EJB provides the server side part of the
* {@link de.danet.an.util.log4j.EJBAppender}.
*
* @ejbHome <{de.danet.an.util.log4j.EJBSinkHome}>
* @ejbRemote <{de.danet.an.util.log4j.EJBSink}>
* @see de.danet.an.util.log4j
*
* @ejb.bean name="EJBSink" display-name="EJBSink"
* jndi-name="ejb/@@@_Utility-EJBs_EJBSinkEJB_JNDI_Name_@@@"
* type="Stateless" transaction-type="Container" view-type="remote"
* @jonas.bean ejb-name="EJBSink"
* @ejb.home remote-class="de.danet.an.util.log4j.EJBSinkHome"
* @ejb.interface remote-class="de.danet.an.util.log4j.EJBSink"
* @ejb.resource-ref res-ref-name="jdbc/Logging"
* res-type="javax.sql.DataSource" res-auth="Container"
* jndi-name="DefaultDS"
* @jonas.resource res-ref-name="jdbc/Logging" jndi-name="jdbc_1"
* @weblogic.enable-call-by-reference True
* @weblogic.resource-description res-ref-name="jdbc/Logging"
* jndi-name="DefaultDS"
* @weblogic.transaction-isolation TRANSACTION_READ_COMMITTED
*/
public class EJBSinkEJB implements SessionBean {
/** The SessionContext interface of the instance. */
private SessionContext ctx;
/**
* The data source of the database.
* @see javax.sql.DataSource
*/
private DataSource ds = null;
/** Database attributes */
private static final String DB_NAME = "java:comp/env/jdbc/Logging";
/**
* Set the session context.
* @param context session context
* @throws EJBException if error
*/
public void setSessionContext(SessionContext context)
throws EJBException {
ctx = context;
try {
ds = JDBCUtil.refreshDS(null, DB_NAME);
} catch (NamingException ne) {
throw new EJBException(ne);
}
}
/**
* Not called for stateless session beans.
* @see javax.ejb.SessionBean
*/
public void ejbActivate() throws EJBException {
}
/**
* Not called for stateless session beans.
* @see javax.ejb.SessionBean
*/
public void ejbPassivate() throws EJBException {
}
/**
* A container invokes this method before it ends the life of the session
* object. This happens as a result of a client's invoking a remove
* operation, or when a container decides to terminate the session object
* after a timeout.
* @see javax.ejb.SessionBean
*/
public void ejbRemove() {
ds = null;
ctx = null;
}
/**
* Create a new instance of the EJBSinkEJB.
*
* @throws CreateException if the EJB cannot be create.
*/
public void ejbCreate() throws CreateException {
}
/**
* Insert the event information into the database.
* @param event the event including the message to be logged.
* @ejb.interface-method
* @ejb.transaction
* type="RequiresNew"
*/
public void append(LoggingEvent event) {
Connection con = null;
PreparedStatement prepStmt = null;
try {
con = ds.getConnection();
prepStmt = new UniversalPrepStmt
(ds, con, "INSERT INTO LogMessages "
+ "(Prio, Message, Category, Thread, NDC, LogTime) "
+ "VALUES (?, ?, ?, ?, ?, ?)");
int offset = 1;
prepStmt.setString (offset++, "INFO");
//event.getLevel().toString());//'INFO'
prepStmt.setString (offset++, event.getRenderedMessage());
prepStmt.setString (offset++, event.categoryName);//'log4j.Basic'
prepStmt.setString (offset++, event.getThreadName());//'main'
prepStmt.setString (offset++, event.getNDC());//NDC
prepStmt.setTimestamp
(offset++, new java.sql.Timestamp(event.timeStamp));
prepStmt.executeUpdate();
} catch (SQLException se) {
throw new EJBException(se);
} finally {
try {
JDBCUtil.closeAll (null, prepStmt, con);
} catch (SQLException e) {
// errors in loggers, that's difficult, we don't want loops...
System.err.println
("Problem in EJBSinkEJB: " + e.getMessage ());
e.printStackTrace ();
}
}
}
/**
* Prepared the database connection and the PreparedStatement
* for the database access.
*/
private void prepareDatabaseConnection()
throws SQLException {
}
}