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:
Bosanac Dejan 2011-01-13 10:36:05 +00:00
parent 921d6ca25d
commit 7b1cdbc080
5 changed files with 95 additions and 13 deletions

View File

@ -52,7 +52,7 @@ public class AnnotatedMBean extends StandardMBean {
} }
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit")); audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
if (audit) { if (audit) {
auditLog = new AuditLogService(); auditLog = AuditLogService.getAuditLog();
} }
} }

View File

@ -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();
}

View File

@ -16,26 +16,37 @@
*/ */
package org.apache.activemq.broker.util; package org.apache.activemq.broker.util;
import java.util.ArrayList; import org.apache.commons.logging.Log;
import java.util.ServiceLoader; import org.apache.commons.logging.LogFactory;
public class AuditLogService { public class AuditLogService {
private ArrayList<AuditLog> auditLogs = new ArrayList<AuditLog>(); private static final Log LOG = LogFactory.getLog(AuditLogService.class);
public AuditLogService() { private AuditLogFactory factory;
ServiceLoader<AuditLog> logs = ServiceLoader.load(AuditLog.class);
for (AuditLog log : logs) { private static AuditLogService auditLog;
auditLogs.add(log);
public static AuditLogService getAuditLog() {
if (auditLog == null) {
auditLog = new AuditLogService();
} }
// add default audit log if non was found return auditLog;
if (auditLogs.size() == 0) { }
auditLogs.add(new DefaultAuditLog());
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) { public void log(String message) {
for (AuditLog log : auditLogs) { for (AuditLog log : factory.getAuditLogs()) {
log.log(message); log.log(message);
} }
} }

View File

@ -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;
}
}

View File

@ -36,7 +36,9 @@ public class AuditFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit")); audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
auditLog = new AuditLogService(); if (audit) {
auditLog = AuditLogService.getAuditLog();
}
} }
public void destroy() { public void destroy() {