MoyaEventTopicLoggingBean.java 3.62 KB
/*
 * 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();
	}

}