001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.core.ejb;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceEvent;
037:
038: import org.libresource.kernel.KernelConstants;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import java.sql.Connection;
042: import java.sql.PreparedStatement;
043: import java.sql.ResultSet;
044: import java.sql.Timestamp;
045:
046: import javax.ejb.EJBException;
047: import javax.ejb.MessageDrivenBean;
048: import javax.ejb.MessageDrivenContext;
049:
050: import javax.jms.Message;
051: import javax.jms.MessageListener;
052:
053: import javax.naming.InitialContext;
054:
055: import javax.sql.DataSource;
056:
057: /**
058: * LibreSource
059: *
060: * @author <a href="mailto:bort@loria.fr">Guillaume Bort </a>- <a
061: * href="http://www.inria.fr">INRIA Lorraine </a>
062: *
063: * @ejb.bean name="EventLoggerBean" transaction-type="Container"
064: * acknowledge-mode="Auto-acknowledge"
065: * destination-type="javax.jms.Topic"
066: * subscription-durability="NonDurable"
067: *
068: * @jonas.bean ejb-name="EventLoggerBean"
069: *
070: * @jonas.message-driven-destination jndi-name="libresource.events"
071: *
072: * @jboss.destination-jndi-name name="topic/libresource.events"
073: *
074: * @ejb.transaction type="NotSupported"
075: */
076: public class EventLoggerBean implements MessageDrivenBean,
077: MessageListener {
078: private transient MessageDrivenContext mdbContext;
079:
080: public void onMessage(Message message) {
081: Connection conn = null;
082: PreparedStatement stmt = null;
083:
084: try {
085: KernelService kernelService = (KernelService) Libresource
086: .getService(KernelConstants.SERVICE);
087: LibresourceEvent event = LibresourceEvent
088: .toLibresourceEvent(message);
089: conn = getConnection();
090: stmt = conn
091: .prepareStatement("INSERT INTO events_log (date_,from_,type_,service_,resource_,by_,args_) VALUES (?,?,?,?,?,?,?);");
092: stmt.setTimestamp(1, new Timestamp(System
093: .currentTimeMillis()));
094: stmt.setString(2, event.getFromResource().getPath());
095: stmt.setString(3, event.getEventType());
096: stmt.setString(4, event.getServiceName());
097: stmt.setString(5, event.getResourceType());
098: stmt.setString(6, event.getThrowedBy().getPath());
099: stmt.setString(7, event.getArgs());
100: stmt.executeUpdate();
101: } catch (Exception e) {
102: //mdbContext.setRollbackOnly();
103: } finally {
104: try {
105: if (stmt != null) {
106: stmt.close();
107: }
108: } catch (Exception e) {
109: }
110:
111: try {
112: if (conn != null) {
113: conn.close();
114: }
115: } catch (Exception e) {
116: }
117: }
118: }
119:
120: // standard call back methods
121: public void setMessageDrivenContext(MessageDrivenContext ctx) {
122: mdbContext = ctx;
123: }
124:
125: public void ejbRemove() throws EJBException {
126: }
127:
128: public void ejbCreate() throws EJBException {
129: }
130:
131: private Connection getConnection() throws Exception {
132: Connection con = Libresource.getDatasource().getConnection();
133: ResultSet rs = con.getMetaData().getTables(null, null,
134: "events_log", null);
135:
136: if (!rs.next()) {
137: con
138: .createStatement()
139: .executeUpdate(
140: "CREATE TABLE events_log (date_ TIMESTAMP, from_ VARCHAR, type_ VARCHAR, service_ VARCHAR, resource_ VARCHAR, by_ VARCHAR, args_ VARCHAR);");
141: }
142:
143: rs.close();
144:
145: return con;
146: }
147: }
|