-
Notifications
You must be signed in to change notification settings - Fork 188
/
SqlAuditServiceFactory.java
99 lines (83 loc) · 4.06 KB
/
SqlAuditServiceFactory.java
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
/*
* Copyright (c) 2010-2013 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.repo.sql;
import com.evolveum.midpoint.audit.api.AuditService;
import com.evolveum.midpoint.audit.api.AuditServiceFactory;
import com.evolveum.midpoint.audit.api.AuditServiceFactoryException;
import com.evolveum.midpoint.repo.api.RepositoryServiceFactoryException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import java.util.List;
import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.tree.ImmutableNode;
import org.apache.commons.lang.StringUtils;
/**
* @author lazyman
*/
public class SqlAuditServiceFactory implements AuditServiceFactory {
private static final Trace LOGGER = TraceManager.getTrace(SqlAuditServiceFactory.class);
private static final String CONF_AUDIT_SERVICE_COLUMNS = "customColumn";
private static final String CONF_AUDIT_SERVICE_COLUMN_NAME = "columnName";
private static final String CONF_AUDIT_SERVICE_EVENT_RECORD_PROPERTY_NAME = "eventRecordPropertyName";
private SqlRepositoryFactory repositoryFactory;
private SqlAuditServiceImpl auditService;
public SqlRepositoryFactory getRepositoryFactory() {
return repositoryFactory;
}
public void setRepositoryFactory(SqlRepositoryFactory repositoryFactory) {
this.repositoryFactory = repositoryFactory;
}
@Override
public synchronized void destroy() throws AuditServiceFactoryException {
LOGGER.info("Destroying Sql audit service factory.");
try {
repositoryFactory.destroy();
} catch (RepositoryServiceFactoryException ex) {
throw new AuditServiceFactoryException(ex.getMessage(), ex);
}
LOGGER.info("Sql audit service factory destroy complete.");
}
@Override
public synchronized void init(Configuration config) throws AuditServiceFactoryException {
LOGGER.info("Initializing Sql audit service factory.");
try {
repositoryFactory.init(config);
auditService = new SqlAuditServiceImpl(repositoryFactory);
List<HierarchicalConfiguration<ImmutableNode>> subConfigColumns = ((BaseHierarchicalConfiguration)config).childConfigurationsAt(CONF_AUDIT_SERVICE_COLUMNS);
for(Configuration subConfigColumn : subConfigColumns) {
String columnName = getStringFromConfig(subConfigColumn, CONF_AUDIT_SERVICE_COLUMN_NAME);
String eventRecordPropertyName = getStringFromConfig(subConfigColumn, CONF_AUDIT_SERVICE_EVENT_RECORD_PROPERTY_NAME);
auditService.getCustomColumn().put(eventRecordPropertyName, columnName);
}
} catch (RepositoryServiceFactoryException ex) {
throw new AuditServiceFactoryException(ex.getMessage(), ex);
}
LOGGER.info("Sql audit service factory initialization complete.");
}
private String getStringFromConfig(Configuration config, String key) {
String value = config.getString(key);
if (StringUtils.isEmpty(value)) {
LOGGER.error("Property with key ({}) not found in configuration. " +
"Provided configuration:\n{}", new Object[]{key, config});
throw new SystemException("Property with key (" + key
+ ") not found in configuration. Provided configuration:\n"
+ config);
}
return value;
}
@Override
public void destroyService(AuditService service) throws AuditServiceFactoryException {
//we don't need destroying service objects, they will be GC correctly
}
@Override
public AuditService getAuditService() throws AuditServiceFactoryException {
return auditService;
}
}