Commit bff767f2 by Tuomas Riihimäki

Moya Event messaging via jms

1 parent 026af0e3
......@@ -34,6 +34,7 @@ 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.beans.moyamessage.MoyaEventSender;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.LogEntry;
......@@ -112,8 +113,7 @@ public class LoggingBean implements LoggingBeanLocal {
// }
@Override
public LogEntry logMessage(SecurityLogType paramType, LanEvent event, User user,
Object... description) {
public LogEntry logMessage(SecurityLogType paramType, LanEvent event, User user, Object... description) {
LogEntry entry = null;
if (event == null) {
event = eventbean.getCurrentEvent();
......@@ -132,7 +132,7 @@ public class LoggingBean implements LoggingBeanLocal {
String msg = "SECURITY DEBUG: Type: \"" + paramType.name() +
"\" user \"" + paramType.name() +
"\", description \"" + ((user == null) ? "null" : user.getLogin()) + "\"" + desc;
sender.sendMessage(msg);
//sender.sendMessage(msg);
// utx.commit();
} catch (Exception e) {
logger.warn("Exception at SecurityBean", e);
......@@ -144,8 +144,7 @@ public class LoggingBean implements LoggingBeanLocal {
public LogEntry logMessage(SecurityLogType paramType, EventUser user, Object... description) {
LanEvent event = null;
User usr = null;
if (user != null)
{
if (user != null) {
event = user.getEvent();
usr = user.getUser();
}
......
package fi.codecrew.moya.beans;
package fi.codecrew.moya.beans.moyamessage;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
......@@ -6,11 +6,14 @@ import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.BotBean;
/**
* Message-Driven Bean implementation class for: IrcBotTopicListener
*/
......@@ -26,15 +29,21 @@ public class IrcBotMoyaEventTopicListener implements MessageListener {
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
botbean.getBot().say("Got msg: " + tm.getText());
logger.info("Received moya event message for irc bot {}", message);
MoyaEventMessage msg = message.getBody(MoyaEventMessage.class);
botbean.getBot().say(toString("Got Message ", msg.getEventtype(), " msg: ", msg.getDescription()));
logger.warn("Received moya event message for irc bot {}", message);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.warn("Exception while getting jms message for IRCbot");
}
}
private static String toString(Object... string) {
StringBuilder sb = new StringBuilder();
for (Object s : string) {
sb.append(s);
}
return sb.toString();
}
}
package fi.codecrew.moya.beans.moyamessage;
import java.io.Serializable;
import java.util.Calendar;
public class MoyaEventMessage implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Calendar time;
private MoyaEventType eventtype;
private String description;
private Integer userId;
private Integer eventId;
private Integer currentUserId;
public void setTime(Calendar time) {
this.time = time;
}
public void setEventtype(MoyaEventType eventtype) {
this.eventtype = eventtype;
}
public void setDescription(String description) {
this.description = description;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public void setEventId(Integer eventId) {
this.eventId = eventId;
}
public Calendar getTime() {
return time;
}
public MoyaEventType getEventtype() {
return eventtype;
}
public String getDescription() {
return description;
}
public Integer getUserId() {
return userId;
}
public Integer getEventId() {
return eventId;
}
public Integer getCurrentUserId() {
return currentUserId;
}
public void setCurrentUserId(Integer currentUser) {
this.currentUserId = currentUser;
}
}
\ No newline at end of file
package fi.codecrew.moya.beans;
package fi.codecrew.moya.beans.moyamessage;
import java.util.Calendar;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.jms.JMSConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSDestinationDefinition;
import javax.jms.ObjectMessage;
import javax.jms.Topic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.model.EventUser;
@Stateless
@JMSDestinationDefinition(name = MoyaEventSender.MOYA_EVENT_SENDER_TOPIC,
destinationName = MoyaEventSender.MOYA_EVENT_TOPIC_DESTINATION,
description = "Events generated by moya to be logged or otherwise handler",
interfaceName = "javax.jms.Topic")
interfaceName = "javax.jms.Topic"
)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
//@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class MoyaEventSender {
static final String MOYA_EVENT_TOPIC_DESTINATION = "moyaEventTopic";
public static final String MOYA_EVENT_SENDER_TOPIC = "java:global/jms/moyaEventTopic";
@EJB
private EventBeanLocal eventbean;
@EJB
private PermissionBeanLocal permbean;
@Inject
JMSContext context;
......@@ -25,7 +46,23 @@ public class MoyaEventSender {
@Resource(mappedName = MOYA_EVENT_SENDER_TOPIC)
Topic topic;
public void sendMessage(String message) {
context.createProducer().send(topic, message);
private static final Logger logger = LoggerFactory.getLogger(MoyaEventSender.class);
public void sendMessage(MoyaEventMessage moyaMessage) {
ObjectMessage msg = context.createObjectMessage(moyaMessage);
context.createProducer().send(topic, msg);
}
public void sendMessage(MoyaEventType type, EventUser user, String message) {
MoyaEventMessage msg = new MoyaEventMessage();
msg.setEventtype(type);
msg.setTime(Calendar.getInstance());
msg.setUserId(user.getId());
msg.setCurrentUserId(permbean.getCurrentUser().getUser().getId());
msg.setEventId(eventbean.getCurrentEvent().getId());
msg.setDescription(message);
logger.info("Sending Moya message {} with description ", type, message);
sendMessage(msg);
}
}
......@@ -16,7 +16,7 @@
* future versions of the Software.
*
*/
package fi.codecrew.moya.beans;
package fi.codecrew.moya.beans.moyamessage;
import java.util.Calendar;
......@@ -26,6 +26,10 @@ import javax.ejb.MessageDriven;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.transaction.UserTransaction;
import org.slf4j.Logger;
......@@ -45,73 +49,28 @@ import fi.codecrew.moya.model.User;
* Session Bean implementation class SercurityBean
*/
@MessageDriven(mappedName = MoyaEventSender.MOYA_EVENT_SENDER_TOPIC)
public class MoyaEventTopicLoggingBean implements LoggingBeanLocal {
public class MoyaEventTopicLoggingBean implements MessageListener {
private static final boolean DEBUG = true;
private final Logger logger = org.slf4j.LoggerFactory
.getLogger(MoyaEventTopicLoggingBean.class);
// @EJB
// private LogEntryTypeFacade typeFacade;
// @EJB
// private LogEntryFacade entryFacade;
private final Logger logger = org.slf4j.LoggerFactory.getLogger(MoyaEventTopicLoggingBean.class);
@Override
public void onMessage(Message message) {
if (message != null) {
try {
MoyaEventMessage msg = message.getBody(MoyaEventMessage.class);
logger.warn("Got message at TopicLogging {}, {}", msg.getEventtype(), msg.getDescription());
} catch (JMSException e) {
logger.warn("Exception whie receiving Moya EventTopic", e);
}
}
}
@EJB
private EventBeanLocal eventbean;
// @Resource
// UserTransaction utx;
// @Override
// public LogEntry logPermissionDenied(User user, Exception exception) {
// LogEntry entry = null;
//
// entry = logMessage(SecurityLogType.permissionDenied, user,
// exception.getMessage());
// logger.debug(entry.toString(), exception);
//
// return entry;
//
// }
//
// public LogEntry logException(User user, Exception exception) {
//
// LogEntry entry = logMessage(SecurityLogType.unknownException, user,
// exception.getMessage());
// logger.debug(entry.toString(), exception);
// return entry;
// }
//
// public LogEntry logMessage(User user, String... description) {
//
// LogEntry entry = logMessage(SecurityLogType.genericMessage, user,
// toString(description));
//
// return entry;
// }
//
// private static final String toString(String... desc) {
// StringBuilder msg = new StringBuilder();
// for (String msgpart : desc) {
// msg.append(msgpart);
// }
// return msg.toString();
// }
//
// public LogEntry logMessage(String... description) {
// LogEntry entry = logMessage(SecurityLogType.genericMessage,
// toString(description));
// return entry;
//
// }
// public LogEntry logPermissionDenied(User currentuser, String... message)
// {
// return logMessage(SecurityLogType.permissionDenied, currentuser,
// toString(message));
// }
@Override
public LogEntry logMessage(SecurityLogType paramType, LanEvent event, User user,
Object... description) {
public LogEntry logMessage(SecurityLogType paramType, LanEvent event, User user, Object... description) {
LogEntry entry = null;
if (event == null) {
event = eventbean.getCurrentEvent();
......@@ -130,7 +89,7 @@ public class MoyaEventTopicLoggingBean implements LoggingBeanLocal {
String msg = "SECURITY DEBUG: Type: \"" + paramType.name() +
"\" user \"" + paramType.name() +
"\", description \"" + ((user == null) ? "null" : user.getLogin()) + "\"" + desc;
sender.sendMessage(msg);
// sender.sendMessage(msg);
// utx.commit();
} catch (Exception e) {
logger.warn("Exception at SecurityBean", e);
......@@ -138,7 +97,6 @@ public class MoyaEventTopicLoggingBean implements LoggingBeanLocal {
return entry;
}
@Override
public LogEntry logMessage(SecurityLogType paramType, EventUser user, Object... description) {
LanEvent event = null;
User usr = null;
......@@ -157,4 +115,5 @@ public class MoyaEventTopicLoggingBean implements LoggingBeanLocal {
}
return msg.toString();
}
}
package fi.codecrew.moya.beans.moyamessage;
public enum MoyaEventType {
LOGIN_FAILED(MoyaEventSource.USER),
USER_CREATED(MoyaEventSource.USER),
USER_INSUFFICIENT_PERMISSIONS(MoyaEventSource.USER),
BILL_CREATED(MoyaEventSource.SHOP),
BILL_PAID(MoyaEventSource.SHOP),
;
private final MoyaEventSource source;
private MoyaEventType(MoyaEventSource source) {
this.source = source;
}
public MoyaEventSource getSource() {
return source;
}
private static enum MoyaEventSource {
USER, EVENT, SHOP
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!