mirror of https://github.com/apache/activemq.git
https://issues.apache.org/jira/browse/AMQ-3100 - refactoring to support multiple service factories
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1058490 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
921d6ca25d
commit
7b1cdbc080
|
@ -52,7 +52,7 @@ public class AnnotatedMBean extends StandardMBean {
|
|||
}
|
||||
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
|
||||
if (audit) {
|
||||
auditLog = new AuditLogService();
|
||||
auditLog = AuditLogService.getAuditLog();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.activemq.broker.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AuditLogFactory {
|
||||
|
||||
public List<AuditLog> getAuditLogs();
|
||||
|
||||
}
|
|
@ -16,26 +16,37 @@
|
|||
*/
|
||||
package org.apache.activemq.broker.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.ServiceLoader;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class AuditLogService {
|
||||
|
||||
private ArrayList<AuditLog> auditLogs = new ArrayList<AuditLog>();
|
||||
private static final Log LOG = LogFactory.getLog(AuditLogService.class);
|
||||
|
||||
public AuditLogService() {
|
||||
ServiceLoader<AuditLog> logs = ServiceLoader.load(AuditLog.class);
|
||||
for (AuditLog log : logs) {
|
||||
auditLogs.add(log);
|
||||
private AuditLogFactory factory;
|
||||
|
||||
private static AuditLogService auditLog;
|
||||
|
||||
public static AuditLogService getAuditLog() {
|
||||
if (auditLog == null) {
|
||||
auditLog = new AuditLogService();
|
||||
}
|
||||
// add default audit log if non was found
|
||||
if (auditLogs.size() == 0) {
|
||||
auditLogs.add(new DefaultAuditLog());
|
||||
return auditLog;
|
||||
}
|
||||
|
||||
private AuditLogService() {
|
||||
String auditLogFactory = System.getProperty("org.apache.activemq.audit.factory", "org.apache.activemq.broker.util.DefaultAuditLogFactory");
|
||||
try {
|
||||
factory = (AuditLogFactory) Class.forName(auditLogFactory).newInstance();
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Cannot instantiate audit log factory '" + auditLogFactory + "', using default audit log factory", e);
|
||||
factory = new DefaultAuditLogFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void log(String message) {
|
||||
for (AuditLog log : auditLogs) {
|
||||
for (AuditLog log : factory.getAuditLogs()) {
|
||||
log.log(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.activemq.broker.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class DefaultAuditLogFactory implements AuditLogFactory {
|
||||
|
||||
private ArrayList<AuditLog> auditLogs = new ArrayList<AuditLog>();
|
||||
|
||||
public DefaultAuditLogFactory() {
|
||||
ServiceLoader<AuditLog> logs = ServiceLoader.load(AuditLog.class);
|
||||
|
||||
for (AuditLog log : logs) {
|
||||
auditLogs.add(log);
|
||||
}
|
||||
|
||||
// add default audit log if non was found
|
||||
if (auditLogs.size() == 0) {
|
||||
auditLogs.add(new DefaultAuditLog());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AuditLog> getAuditLogs() {
|
||||
return auditLogs;
|
||||
}
|
||||
}
|
|
@ -36,7 +36,9 @@ public class AuditFilter implements Filter {
|
|||
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
|
||||
auditLog = new AuditLogService();
|
||||
if (audit) {
|
||||
auditLog = AuditLogService.getAuditLog();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
|
|
Loading…
Reference in New Issue