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 package info.magnolia.audit;
35
36 import info.magnolia.objectfactory.Components;
37
38 import java.util.ArrayList;
39 import java.util.Iterator;
40 import java.util.List;
41
42 import org.apache.commons.lang.StringUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47
48
49
50
51 public class AuditLoggingManager {
52
53 private List<LogConfiguration> logConfigurations = new ArrayList<LogConfiguration>();
54
55 private String defaultSeparator = ", ";
56
57 private static Logger applog = LoggerFactory.getLogger(AuditLoggingManager.class);
58
59 public static AuditLoggingManager getInstance() {
60 try {
61 return Components.getComponent(AuditLoggingManager.class);
62 } catch (Exception e) {
63
64 applog.info("Class AuditLoggingManager not defined");
65 return null;
66 }
67 }
68
69 public void addLogConfigurations(LogConfiguration action) {
70 this.logConfigurations.add(action);
71 }
72
73 public List<LogConfiguration> getLogConfigurations() {
74 return logConfigurations;
75 }
76
77 public void setLogConfigurations(List<LogConfiguration> logConfigurations) {
78 this.logConfigurations = logConfigurations;
79 }
80
81 public LogConfiguration getLogConfiguration(String action) {
82 Iterator<LogConfiguration> iterator = this.logConfigurations.iterator();
83 while (iterator.hasNext()) {
84 final LogConfiguration trail = iterator.next();
85 if (StringUtils.equals(trail.getName(), action)) {
86 return trail;
87 }
88 }
89 return null;
90 }
91
92
93
94
95 public boolean isAuditLoggingActive() {
96 Iterator<LogConfiguration> iterator = this.logConfigurations.iterator();
97 while (iterator.hasNext()) {
98 final LogConfiguration trail = iterator.next();
99 if (trail.isActive()) {
100 return true;
101 }
102 }
103 return false;
104 }
105
106 public void log(String action, String[] data) {
107 StringBuilder message = new StringBuilder();
108 LogConfiguration trail = this.getLogConfiguration(action);
109 if (trail == null) {
110 applog.trace("Can't get log configuration");
111 } else {
112 String separator = defaultSeparator;
113 if (!StringUtils.isEmpty(trail.getSeparator())) {
114 separator = trail.getSeparator();
115 }
116 message.append(separator).append(action);
117 if (trail.isActive()) {
118 for (int i = 0; i < data.length; i++) {
119 if (StringUtils.isNotEmpty(data[i])) {
120 message.append(separator).append(data[i]);
121 }
122
123 }
124 org.apache.log4j.Logger.getLogger(trail.getLogName()).log(LoggingLevel.AUDIT_TRAIL, message.toString());
125 }
126 }
127 }
128
129 public String getDefaultSeparator() {
130 return defaultSeparator;
131 }
132
133 public void setDefaultSeparator(String defaultSeparator) {
134 this.defaultSeparator = defaultSeparator;
135 }
136 }