LoggingBean.java 4.6 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;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
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.beans.moyamessage.MoyaEventSender;
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
 */
@Stateless
//@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 EventBeanLocal eventbean;
	@EJB
	private MoyaEventSender sender;

	//	@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) {
		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;
	}

	@Override
	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();
	}
}