Commit 2cfb1391 by Tuomas Riihimäki

Fix logging

1 parent b08d0afd
package fi.codecrew.moya.beans.moyamessage; package fi.codecrew.moya.beans.moyamessage;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.ejb.MessageDriven; import javax.ejb.MessageDriven;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageListener; import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.BotBean; import fi.codecrew.moya.beans.BotBean;
import fi.codecrew.moya.facade.EventFacade;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventMessage; import fi.codecrew.moya.utilities.moyamessage.MoyaEventMessage;
/** /**
...@@ -26,13 +25,22 @@ public class IrcBotMoyaEventTopicListener implements MessageListener { ...@@ -26,13 +25,22 @@ public class IrcBotMoyaEventTopicListener implements MessageListener {
@EJB @EJB
private BotBean botbean; private BotBean botbean;
@EJB
private EventFacade eventfacade;
/** /**
* @see MessageListener#onMessage(Message) * @see MessageListener#onMessage(Message)
*/ */
public void onMessage(Message message) { public void onMessage(Message message) {
try { try {
MoyaEventMessage msg = message.getBody(MoyaEventMessage.class); MoyaEventMessage msg = message.getBody(MoyaEventMessage.class);
botbean.getBot().say(toString("Got Message ", msg.getEventtype(), " msg: ", msg.getDescription())); String event = "unknown event";
if (msg.getEventId() != null) {
LanEvent e = eventfacade.find(msg.getEventId());
if (e != null)
event = e.getName();
}
botbean.getBot().say(toString(event, " ", msg.getEventtype(), " msg: ", msg.getDescription()));
logger.warn("Received moya event message for irc bot {}", message); logger.warn("Received moya event message for irc bot {}", message);
} catch (JMSException e) { } catch (JMSException e) {
logger.warn("Exception while getting jms message for IRCbot"); logger.warn("Exception while getting jms message for IRCbot");
......
...@@ -63,7 +63,7 @@ public class MoyaEventSender implements LoggingBeanLocal { ...@@ -63,7 +63,7 @@ public class MoyaEventSender implements LoggingBeanLocal {
MoyaEventMessage msg = new MoyaEventMessage(); MoyaEventMessage msg = new MoyaEventMessage();
msg.setEventtype(type); msg.setEventtype(type);
msg.setTime(Calendar.getInstance()); msg.setTime(Calendar.getInstance());
msg.setUserId(user.getId()); msg.setEventUserId(user.getId());
msg.setCurrentUserId(permbean.getCurrentUser().getUser().getId()); msg.setCurrentUserId(permbean.getCurrentUser().getUser().getId());
msg.setEventId(eventbean.getCurrentEvent().getId()); msg.setEventId(eventbean.getCurrentEvent().getId());
msg.setDescription(message); msg.setDescription(message);
......
...@@ -20,31 +20,26 @@ package fi.codecrew.moya.beans.moyamessage; ...@@ -20,31 +20,26 @@ package fi.codecrew.moya.beans.moyamessage;
import java.util.Calendar; import java.util.Calendar;
import javax.annotation.Resource;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.ejb.MessageDriven; import javax.ejb.MessageDriven;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageListener; import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.transaction.UserTransaction;
import org.slf4j.Logger; import org.slf4j.Logger;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.facade.LanEventFacade;
import fi.codecrew.moya.facade.LogEntryFacade; import fi.codecrew.moya.facade.LogEntryFacade;
import fi.codecrew.moya.facade.LogEntryTypeFacade; import fi.codecrew.moya.facade.LogEntryTypeFacade;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.LoggingBeanLocal;
import fi.codecrew.moya.beans.SecurityLogType;
import fi.codecrew.moya.model.EventUser; import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent; import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.LogEntry; import fi.codecrew.moya.model.LogEntry;
import fi.codecrew.moya.model.LogEntryType; import fi.codecrew.moya.model.LogEntryType;
import fi.codecrew.moya.model.User; import fi.codecrew.moya.model.User;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventMessage; import fi.codecrew.moya.utilities.moyamessage.MoyaEventMessage;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventType;
/** /**
* Session Bean implementation class SercurityBean * Session Bean implementation class SercurityBean
...@@ -55,13 +50,26 @@ public class MoyaEventTopicLoggingBean implements MessageListener { ...@@ -55,13 +50,26 @@ public class MoyaEventTopicLoggingBean implements MessageListener {
private static final boolean DEBUG = true; private static final boolean DEBUG = true;
private final Logger logger = org.slf4j.LoggerFactory.getLogger(MoyaEventTopicLoggingBean.class); private final Logger logger = org.slf4j.LoggerFactory.getLogger(MoyaEventTopicLoggingBean.class);
@EJB
private LanEventFacade eventfacade;
@Override
public void onMessage(Message message) { public void onMessage(Message message) {
if (message != null) { if (message != null) {
try { try {
MoyaEventMessage msg = message.getBody(MoyaEventMessage.class); MoyaEventMessage msg = message.getBody(MoyaEventMessage.class);
logger.warn("Got message at TopicLogging {}, {}", msg.getEventtype(), msg.getDescription()); logger.warn("Got message at TopicLogging {}, {}", msg.getEventtype(), msg.getDescription());
LanEvent event = null;
if (msg.getEventId() != null)
event = eventfacade.find(msg.getEventId());
User user = null;
if (msg.getEventUserId() != null) {
EventUser euser = eventUserfacade.find(msg.getEventUserId());
if (euser != null)
user = euser.getUser();
}
logMessage(msg.getEventtype(), event, user, msg.getDescription());
} catch (JMSException e) { } catch (JMSException e) {
logger.warn("Exception whie receiving Moya EventTopic", e); logger.warn("Exception whie receiving Moya EventTopic", e);
} }
...@@ -69,27 +77,34 @@ public class MoyaEventTopicLoggingBean implements MessageListener { ...@@ -69,27 +77,34 @@ public class MoyaEventTopicLoggingBean implements MessageListener {
} }
@EJB @EJB
private EventUserFacade eventUserfacade;
@EJB
private EventBeanLocal eventbean; private EventBeanLocal eventbean;
public LogEntry logMessage(SecurityLogType paramType, LanEvent event, User user, Object... description) { @EJB
private LogEntryFacade entryFacade;
@EJB
private LogEntryTypeFacade typeFacade;
private LogEntry logMessage(MoyaEventType moyaEventType, LanEvent event, User user, String desc) {
LogEntry entry = null; LogEntry entry = null;
if (event == null) { if (event == null) {
event = eventbean.getCurrentEvent(); event = eventbean.getCurrentEvent();
} }
try { try {
String desc = toString(description);
logger.warn("Sending logmsg {}", desc); logger.warn("Sending logmsg {}", desc);
//LogEntryType type = typeFacade.findOrCreate(paramType); LogEntryType type = typeFacade.findOrCreate(moyaEventType);
// entry = new LogEntry(Calendar.getInstance()); entry = new LogEntry(Calendar.getInstance());
// entry.setParentEvent(event); entry.setParentEvent(event);
// entry.setType(type); entry.setType(type);
// entry.setDescription(desc); entry.setDescription(desc);
// entry.setUser(user); entry.setUser(user);
// entryFacade.create(entry); entryFacade.create(entry);
String msg = "SECURITY DEBUG: Type: \"" + paramType.name() + // String msg = "SECURITY DEBUG: Type: \"" + moyaEventType.name() +
"\" user \"" + paramType.name() + // "\" user \"" + moyaEventType.name() +
"\", description \"" + ((user == null) ? "null" : user.getLogin()) + "\"" + desc; // "\", description \"" + ((user == null) ? "null" : user.getLogin()) + "\"" + desc;
// sender.sendMessage(msg); // sender.sendMessage(msg);
// utx.commit(); // utx.commit();
} catch (Exception e) { } catch (Exception e) {
...@@ -98,23 +113,12 @@ public class MoyaEventTopicLoggingBean implements MessageListener { ...@@ -98,23 +113,12 @@ public class MoyaEventTopicLoggingBean implements MessageListener {
return entry; return entry;
} }
public LogEntry logMessage(SecurityLogType paramType, EventUser user, Object... description) { // private static final String toString(Object... desc) {
LanEvent event = null; // StringBuilder msg = new StringBuilder();
User usr = null; // for (Object msgpart : desc) {
if (user != null) // msg.append(msgpart);
{ // }
event = user.getEvent(); // return msg.toString();
usr = user.getUser(); // }
}
return logMessage(paramType, event, usr, description);
}
private static final String toString(Object... desc) {
StringBuilder msg = new StringBuilder();
for (Object msgpart : desc) {
msg.append(msgpart);
}
return msg.toString();
}
} }
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.facade;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.LanEventDomain_;
import fi.codecrew.moya.model.LanEvent_;
import fi.codecrew.moya.model.LanEventDomain;
@Stateless
@LocalBean
public class LanEventFacade extends IntegerPkGenericFacade<LanEvent> {
public LanEventFacade() {
super(LanEvent.class);
}
}
...@@ -24,6 +24,7 @@ import javax.persistence.TypedQuery; ...@@ -24,6 +24,7 @@ import javax.persistence.TypedQuery;
import fi.codecrew.moya.beans.SecurityLogType; import fi.codecrew.moya.beans.SecurityLogType;
import fi.codecrew.moya.model.LogEntryType; import fi.codecrew.moya.model.LogEntryType;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventType;
@Stateless @Stateless
@LocalBean @LocalBean
...@@ -34,18 +35,18 @@ public class LogEntryTypeFacade extends IntegerPkGenericFacade<LogEntryType> { ...@@ -34,18 +35,18 @@ public class LogEntryTypeFacade extends IntegerPkGenericFacade<LogEntryType> {
super(LogEntryType.class); super(LogEntryType.class);
} }
public LogEntryType findOrCreate(SecurityLogType type) { public LogEntryType findOrCreate(MoyaEventType moyaEventType) {
// Fetch log entry type // Fetch log entry type
TypedQuery<LogEntryType> q = getEm().createNamedQuery("LogEntryType.findByName", LogEntryType.class); TypedQuery<LogEntryType> q = getEm().createNamedQuery("LogEntryType.findByName", LogEntryType.class);
q.setParameter("name", type.name()); q.setParameter("name", moyaEventType.name());
LogEntryType logEntryType = getSingleNullableResult(q); LogEntryType logEntryType = getSingleNullableResult(q);
// Might not exist yet // Might not exist yet
if (logEntryType == null) { if (logEntryType == null) {
logEntryType = new LogEntryType(); logEntryType = new LogEntryType();
logEntryType.setName(type.name()); logEntryType.setName(moyaEventType.name());
getEm().persist(logEntryType); getEm().persist(logEntryType);
} }
......
...@@ -11,7 +11,7 @@ public class MoyaEventMessage implements Serializable { ...@@ -11,7 +11,7 @@ public class MoyaEventMessage implements Serializable {
private Calendar time; private Calendar time;
private MoyaEventType eventtype; private MoyaEventType eventtype;
private String description; private String description;
private Integer userId; private Integer eventUserId;
private Integer eventId; private Integer eventId;
private Integer currentUserId; private Integer currentUserId;
...@@ -27,8 +27,8 @@ public class MoyaEventMessage implements Serializable { ...@@ -27,8 +27,8 @@ public class MoyaEventMessage implements Serializable {
this.description = description; this.description = description;
} }
public void setUserId(Integer userId) { public void setEventUserId(Integer eventUserId) {
this.userId = userId; this.eventUserId = eventUserId;
} }
public void setEventId(Integer eventId) { public void setEventId(Integer eventId) {
...@@ -47,8 +47,8 @@ public class MoyaEventMessage implements Serializable { ...@@ -47,8 +47,8 @@ public class MoyaEventMessage implements Serializable {
return description; return description;
} }
public Integer getUserId() { public Integer getEventUserId() {
return userId; return eventUserId;
} }
public Integer getEventId() { public Integer getEventId() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!