Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: de/danet/an/util/log4j/EJBSinkEJB.java


1   /*
2    * This file is part of the WfMCore/WfMOpen project.
3    * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
4    * All rights reserved.
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   * 
20   * $Id: EJBSinkEJB.java,v 1.18 2003/09/11 13:25:49 lipp Exp $
21   *
22   * $Log: EJBSinkEJB.java,v $
23   * Revision 1.18  2003/09/11 13:25:49  lipp
24   * Improved naming scheme for symbolic role names.
25   *
26   * Revision 1.17  2003/09/03 15:24:17  lipp
27   * Fixed view type specification.
28   *
29   * Revision 1.16  2003/08/22 13:05:04  lipp
30   * Better data source name.
31   *
32   * Revision 1.15  2003/06/27 08:51:46  lipp
33   * Fixed copyright/license information.
34   *
35   * Revision 1.14  2003/06/04 13:15:50  lipp
36   * Optimized resource allocation/caching.
37   *
38   * Revision 1.13  2003/05/23 15:42:41  lipp
39   * Fixed deployment unit dependencies.
40   *
41   * Revision 1.12  2003/01/15 17:36:13  robert
42   * Workaround for NoSuchMethodError by LoggingEvent.getLevel
43   *
44   * Revision 1.11  2003/01/15 15:28:31  lipp
45   * Added permissions.
46   *
47   * Revision 1.10  2003/01/14 11:53:44  robert
48   * Rename column Timestamp of the table LogMessages to LogTime.
49   *
50   * Revision 1.9  2002/11/26 11:23:30  lipp
51   * Modified RemoteException comment.
52   *
53   * Revision 1.8  2002/11/15 15:15:37  montag
54   * Generation of EJBSink-Classes and -DD with xdoclet.
55   *
56   * Revision 1.7  2002/09/04 06:57:37  lipp
57   * Now using JBoss-3.0
58   *
59   * Revision 1.6  2002/01/11 09:30:59  robert
60   * rename table ErrorLog to LogMessages
61   *
62   * Revision 1.5  2002/01/10 10:19:09  robert
63   * resumes database connection on connection failed.
64   *
65   * Revision 1.4  2002/01/09 17:18:48  robert
66   * add NDC to the SQL Statement
67   *
68   * Revision 1.3  2002/01/09 15:44:54  robert
69   * modify the SQL Statement
70   *
71   * Revision 1.2  2002/01/09 09:59:13  robert
72   * javadoc
73   *
74   * Revision 1.1  2002/01/09 09:21:14  robert
75   * new EJB for EJBAppender
76   *
77   *
78   */
79  package de.danet.an.util.log4j;
80  
81  import java.rmi.RemoteException;
82  import java.sql.Connection;
83  import java.sql.PreparedStatement;
84  import java.sql.SQLException;
85  
86  import javax.ejb.CreateException;
87  import javax.ejb.EJBException;
88  import javax.ejb.SessionBean;
89  import javax.ejb.SessionContext;
90  import javax.naming.NamingException;
91  import javax.sql.DataSource;
92  
93  import org.apache.log4j.spi.LoggingEvent;
94  
95  import de.danet.an.util.JDBCUtil;
96  import de.danet.an.util.UniversalPrepStmt;
97  
98  /**
99   * This session EJB provides the server side part of the
100  * {@link de.danet.an.util.log4j.EJBAppender}.
101  *
102  * @ejbHome <{de.danet.an.util.log4j.EJBSinkHome}>
103  * @ejbRemote <{de.danet.an.util.log4j.EJBSink}>
104  * @see de.danet.an.util.log4j
105  * 
106  * @ejb.bean name="EJBSink" display-name="EJBSink"
107  * jndi-name="@@@_Utility-EJBs_EJBSinkEJB_JNDI_Name_@@@"
108  * type="Stateless" transaction-type="Container" view-type="remote"
109  * @ejb.home remote-class="de.danet.an.util.log4j.EJBSinkHome"
110  * @ejb.interface remote-class="de.danet.an.util.log4j.EJBSink"
111  * @ejb.resource-ref res-ref-name="jdbc/Logging" 
112  * res-type="javax.sql.DataSource" res-auth="Container"
113  * @weblogic.resource-description res-ref-name="jdbc/Logging"
114  * jndi-name="WfMCoreDS"
115  * @ejb.permission role-name="@@@_Utility-EJBs_EJBSinkEJB_User_@@@"
116  */
117 public class EJBSinkEJB implements SessionBean {
118 
119     /** The SessionContext interface of the instance. */
120     private SessionContext ctx;
121 
122     /**
123      * The data source of the database.
124      * @see javax.sql.DataSource
125      */
126     private DataSource ds = null;
127 
128     /** Database attributes */
129     private static final String DB_NAME = "java:comp/env/jdbc/Logging";
130 
131     /**
132      * Set the session context.
133      * @param context session context
134      * @throws EJBException if error
135      */
136     public void setSessionContext(SessionContext context) 
137   throws EJBException {
138         ctx = context;
139   try {
140       ds = JDBCUtil.refreshDS(null, DB_NAME);
141   } catch (NamingException ne) {
142       throw new EJBException(ne);
143   }
144     }
145     
146     /**
147      * Not called for stateless session beans.
148      * @see javax.ejb.SessionBean
149      */
150     public void ejbActivate() throws EJBException {
151     }
152 
153     /**
154      * Not called for stateless session beans.
155      * @see javax.ejb.SessionBean
156      */
157     public void ejbPassivate() throws EJBException {
158     }
159 
160     /**
161      * A container invokes this method before it ends the life of the session 
162      * object. This happens as a result of a client's invoking a remove 
163      * operation, or when a container decides to terminate the session object 
164      * after a timeout.
165      * @see javax.ejb.SessionBean
166      */
167     public void ejbRemove() {
168   ds = null;
169   ctx = null;
170     }
171 
172     /**
173      * Create a new instance of the EJBSinkEJB.
174      * 
175      * @throws CreateException if the EJB cannot be create.
176      */
177     public void ejbCreate() throws CreateException {
178 
179     }
180     
181     /**
182      * Insert the event information into the database.
183      * @param event the event including the message to be logged.
184      * @throws RemoteException if a system-level error occurs.
185      * @ejb.interface-method
186      * @ejb.transaction
187      * type="RequiresNew"
188      */
189     public void append(LoggingEvent event) throws RemoteException {
190   Connection con = null;
191   PreparedStatement prepStmt = null;
192   try {
193       con = ds.getConnection();
194       prepStmt = new UniversalPrepStmt 
195     (con, "INSERT INTO LogMessages " 
196      + "(Prio, Message, Category, Thread, NDC, LogTime) "
197      + "VALUES (?, ?, ?, ?, ?, ?)");
198       int offset = 1;
199       prepStmt.setString (offset++, "INFO");
200       //event.getLevel().toString());//'INFO'
201       prepStmt.setString (offset++, event.getRenderedMessage());
202       prepStmt.setString (offset++, event.categoryName);//'log4j.Basic'
203       prepStmt.setString (offset++, event.getThreadName());//'main'
204       prepStmt.setString (offset++, event.getNDC());//NDC
205       prepStmt.setTimestamp
206     (offset++, new java.sql.Timestamp(event.timeStamp));
207       prepStmt.executeUpdate();
208   } catch (SQLException se) {
209       throw new EJBException(se);
210   } finally {
211       try {
212     JDBCUtil.closeAll (null, prepStmt, con);
213       } catch (SQLException e) {
214     // errors in loggers, that's difficult, we don't want loops...
215     System.err.println 
216         ("Problem in EJBSinkEJB: " + e.getMessage ());
217     e.printStackTrace ();
218       }
219   }
220     }
221 
222     /**
223      * Prepared the database connection and the PreparedStatement
224      * for the database access.
225      */
226     private void prepareDatabaseConnection()
227   throws SQLException {
228     }
229 
230 }