LoggingBean.java 3.1 KB
package fi.insomnia.bortal.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.insomnia.bortal.facade.LogEntryFacade;
import fi.insomnia.bortal.facade.LogEntryTypeFacade;
import fi.insomnia.bortal.model.LogEntry;
import fi.insomnia.bortal.model.LogEntryType;
import fi.insomnia.bortal.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;

	@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, User user, String... description) {
		LogEntry entry = null;
		try {
			String desc = toString(description);
			utx.begin();
			LogEntryType type = typeFacade.findOrCreate(paramType);
			entry = new LogEntry(Calendar.getInstance());
			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();
		} catch (Exception e) {
			logger.warn("Exception at SecurityBean", e);
		}
		return entry;
	}

	private static final String toString(String... desc) {
		StringBuilder msg = new StringBuilder();
		for (String msgpart : desc) {
			msg.append(msgpart);
		}
		return msg.toString();
	}
}