Commit a7205d14 by Juho Juopperi

Merge branch 'ircbot' into 'master'

Initial IRCbot implementation

Initial creation of irc bot for moya Also changes logging over JMS topic.
All logging should be moved to provided jms functions so all intrested parties (logging and irc bot) can handle them accordingly

Also update persistence.xml version from 2.0 to 2.1.
NOTICE! THIS MIGHT NEED MANUAL UPDATE OF ECLIPSE
project properties -> project facets -> jpa from 2.0 to 2.1

See merge request !130
2 parents aca146b6 522d6c56
#!/bin/bash
for i in `find code -type d -mindepth 1 -maxdepth 1 -name "Moya*"`; do cd $i ; mvn versions:display-dependency-updates; cd ../.. ; done
for i in `find code -type d -mindepth 1 -maxdepth 1 -name "moya*"`; do
cd $i ; mvn versions:display-dependency-updates; cd ../.. ; done
package fi.codecrew.moya.beans;
import javax.ejb.Local;
import fi.iudex.utils.irc.IrcBot;
@Local
public interface BotBeanLocal {
void add(IrcBot bot);
}
package fi.codecrew.moya.beans;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Stateless;
import fi.iudex.utils.irc.IrcBot;
/**
* Session Bean implementation class BotBean
*/
@Singleton
@LocalBean
public class BotBean implements BotBeanLocal {
private IrcBot bot;
/**
* Default constructor.
*/
public BotBean() {
// TODO Auto-generated constructor stub
}
@Override
public void add(IrcBot bot) {
this.setBot(bot);
}
public IrcBot getBot() {
return bot;
}
public void setBot(IrcBot bot) {
this.bot = bot;
}
}
package fi.codecrew.moya.beans;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
@Startup
public class JmxNotificationTestBean implements NotificationListener {
@PostConstruct
public void startup() {
// JMXConnector jcf;
// try {
// jcf = JMXConnectorFactory.connect(new JMXServiceURL(null, null, 8686));
// MBeanServerConnection mbsc = jcf.getMBeanServerConnection();
// } catch (IOException e) {
// logger.warn("Error creating jmx connection", e);
// }
}
private static final Logger logger = LoggerFactory.getLogger(JmxNotificationTestBean.class);
@Override
public void handleNotification(Notification notification, Object handback) {
logger.info("Handling jmx Notification, src: {}, type {}, userdata: {}", new Object[] { notification.getSource(), notification.getType(), notification.getUserData() });
}
}
......@@ -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;
......@@ -44,22 +45,24 @@ import fi.codecrew.moya.model.User;
* Session Bean implementation class SercurityBean
*/
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
//@TransactionManagement(TransactionManagementType.BEAN)
public class LoggingBean implements LoggingBeanLocal {
private static final boolean DEBUG = true;
private final Logger logger = org.slf4j.LoggerFactory
.getLogger(LoggingBean.class);
@EJB
private LogEntryTypeFacade typeFacade;
@EJB
private LogEntryFacade entryFacade;
// @EJB
// private LogEntryTypeFacade typeFacade;
// @EJB
// private LogEntryFacade entryFacade;
@EJB
private EventBeanLocal eventbean;
@EJB
private MoyaEventSender sender;
@Resource
UserTransaction utx;
// @Resource
// UserTransaction utx;
// @Override
// public LogEntry logPermissionDenied(User user, Exception exception) {
......@@ -110,31 +113,27 @@ 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)
{
if (event == null) {
event = eventbean.getCurrentEvent();
}
try {
String desc = toString(description);
utx.begin();
LogEntryType type = typeFacade.findOrCreate(paramType);
entry = new LogEntry(Calendar.getInstance());
entry.setParentEvent(event);
entry.setType(type);
entry.setDescription(desc);
entry.setUser(user);
logger.warn("Sending logmsg {}", desc);
//LogEntryType type = typeFacade.findOrCreate(paramType);
// entry = new LogEntry(Calendar.getInstance());
// entry.setParentEvent(event);
// entry.setType(type);
// entry.setDescription(desc);
// entry.setUser(user);
entryFacade.create(entry);
if (DEBUG) {
logger.debug(
"SECURITY DEBUG: Type: \"{}\" user \"{}\", description \"{}\"",
new String[] { paramType.name(),
(user == null) ? "null" : user.getLogin(), desc });
}
utx.commit();
// entryFacade.create(entry);
String msg = "SECURITY DEBUG: Type: \"" + paramType.name() +
"\" user \"" + paramType.name() +
"\", description \"" + ((user == null) ? "null" : user.getLogin()) + "\"" + desc;
//sender.sendMessage(msg);
// utx.commit();
} catch (Exception e) {
logger.warn("Exception at SecurityBean", e);
}
......@@ -145,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.moyamessage;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
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
*/
@MessageDriven(mappedName = MoyaEventSender.MOYA_EVENT_SENDER_TOPIC)
public class IrcBotMoyaEventTopicListener implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(IrcBotMoyaEventTopicListener.class);
@EJB
private BotBean botbean;
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
try {
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) {
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.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.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"
)
@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;
@Resource(mappedName = MOYA_EVENT_SENDER_TOPIC)
Topic topic;
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);
}
}
/*
* 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.beans.moyamessage;
import java.util.Calendar;
import javax.annotation.Resource;
import javax.ejb.EJB;
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;
import fi.codecrew.moya.facade.LogEntryFacade;
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.LanEvent;
import fi.codecrew.moya.model.LogEntry;
import fi.codecrew.moya.model.LogEntryType;
import fi.codecrew.moya.model.User;
/**
* Session Bean implementation class SercurityBean
*/
@MessageDriven(mappedName = MoyaEventSender.MOYA_EVENT_SENDER_TOPIC)
public class MoyaEventTopicLoggingBean implements MessageListener {
private static final boolean DEBUG = true;
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;
public LogEntry logMessage(SecurityLogType paramType, LanEvent event, User user, Object... description) {
LogEntry entry = null;
if (event == null) {
event = eventbean.getCurrentEvent();
}
try {
String desc = toString(description);
logger.warn("Sending logmsg {}", desc);
//LogEntryType type = typeFacade.findOrCreate(paramType);
// entry = new LogEntry(Calendar.getInstance());
// entry.setParentEvent(event);
// entry.setType(type);
// entry.setDescription(desc);
// entry.setUser(user);
// entryFacade.create(entry);
String msg = "SECURITY DEBUG: Type: \"" + paramType.name() +
"\" user \"" + paramType.name() +
"\", description \"" + ((user == null) ? "null" : user.getLogin()) + "\"" + desc;
// sender.sendMessage(msg);
// utx.commit();
} catch (Exception e) {
logger.warn("Exception at SecurityBean", e);
}
return entry;
}
public LogEntry logMessage(SecurityLogType paramType, EventUser user, Object... description) {
LanEvent event = null;
User usr = null;
if (user != null)
{
event = user.getEvent();
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();
}
}
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
}
}
eclipse.preferences.version=1
entitygen.DEFAULT_PACKAGE=model
org.eclipse.jpt.core.platform=eclipselink2_4
org.eclipse.jpt.core.platform=eclipselink2_5
org.eclipse.jpt.jpa.core.discoverAnnotatedClasses=true
org.eclipse.jpt.jpa.core.metamodelSourceFolderName=target/generated-sources/annotations
/*
* 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.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity()
@Table(name = "irc_channels",
uniqueConstraints = @UniqueConstraint(columnNames = {
IrcChannel.COLUMN_EVENT_ID,
IrcChannel.COLUMN_CHANNEL,
}))
public class IrcChannel extends GenericEntity {
protected static final String COLUMN_CHANNEL = "channel";
protected static final String COLUMN_EVENT_ID = "event_id";
private static final long serialVersionUID = 5347009472418354347L;
public static enum IrcNetwork {
IRCNET("irc.nebula.fi"), QUAKENET("irc.fi.quakenet.org");
private final String server;
private IrcNetwork(String server) {
this.server = server;
}
public String getServer() {
return server;
}
}
@JoinColumn(name = COLUMN_EVENT_ID)
private LanEvent event;
@Column(nullable = false, name = COLUMN_CHANNEL)
private String channel;
@Enumerated(EnumType.STRING)
private IrcNetwork network;
public LanEvent getEvent() {
return event;
}
public void setEvent(LanEvent event) {
this.event = event;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public IrcNetwork getNetwork() {
return network;
}
public void setNetwork(IrcNetwork network) {
this.network = network;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MoyaDb">
<jta-data-source>jdbc/moyaDb</jta-data-source>
<properties>
......
......@@ -63,7 +63,11 @@
<version>${javaee-api-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>fi.iudex</groupId>
<artifactId>utils-standalone</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
......@@ -112,6 +116,11 @@
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>fi.iudex</groupId>
<artifactId>jerklib</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>net.matlux</groupId>
<artifactId>jvm-breakglass</artifactId>
<version>0.0.7</version>
......
/*
* 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.
*
*/
/*
* 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.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.ejb.Startup;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.BotBeanLocal;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.PlaceBeanLocal;
import fi.iudex.utils.irc.IrcBot;
import fi.iudex.utils.irc.IrcBotListener;
/**
* Servlet implementation class SshServlet
*/
@WebServlet(urlPatterns = "/irc", loadOnStartup = 10)
public class IrcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(IrcServlet.class);
@EJB
private EventBeanLocal eventbean;
@EJB
private PermissionBeanLocal permBean;
private final ArrayList<IrcBot> bots = new ArrayList<>();
@EJB
private BotBeanLocal botbean;
/**
* @see HttpServlet#HttpServlet()
*/
public IrcServlet() {
super();
// TODO Auto-generated constructor stub
}
@EJB
private PlaceBeanLocal placebean;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
IrcBot bot = new IrcBot("irc.cc.tut.fi", "#moya-debug", "moya-bot");
botbean.add(bot);
bots.add(bot);
bot.start();
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
logger.info("Destroying irc servlet");
for (IrcBot b : bots) {
b.stop();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String msg = request.getParameter("msg");
if (msg != null && !msg.isEmpty())
for (IrcBot b : bots) {
b.say(msg);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!