SecurityBean.java
3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 SecurityBean implements SecurityBeanLocal {
private static final boolean DEBUG = true;
private final Logger logger = org.slf4j.LoggerFactory.getLogger(SecurityBean.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));
// }
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();
}
}