From 1f9cc43890a7a539d17d409d4c07e5a00f35aea7 Mon Sep 17 00:00:00 2001 From: Bosanac Dejan Date: Fri, 24 Dec 2010 13:09:27 +0000 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-3100 - refactoring to use ServiceLoader git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1052501 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/broker/jmx/AnnotatedMBean.java | 7 +++- .../activemq/broker/util/AuditLogService.java | 42 +++++++++++++++++++ .../activemq/broker/util/DefaultAuditLog.java | 12 ------ .../org/apache/activemq/web/AuditFilter.java | 5 ++- 4 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 activemq-core/src/main/java/org/apache/activemq/broker/util/AuditLogService.java diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/AnnotatedMBean.java b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/AnnotatedMBean.java index da93e23546..e207b8d506 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/AnnotatedMBean.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/AnnotatedMBean.java @@ -17,6 +17,7 @@ package org.apache.activemq.broker.jmx; import org.apache.activemq.broker.util.AuditLog; +import org.apache.activemq.broker.util.AuditLogService; import org.apache.activemq.broker.util.DefaultAuditLog; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -42,7 +43,7 @@ public class AnnotatedMBean extends StandardMBean { private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit"); private static boolean audit; - private static AuditLog auditLog; + private static AuditLogService auditLog; static { Class[] p = { byte.class, short.class, int.class, long.class, float.class, double.class, char.class, boolean.class, }; @@ -50,7 +51,9 @@ public class AnnotatedMBean extends StandardMBean { primitives.put(c.getName(), c); } audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit")); - auditLog = DefaultAuditLog.getAuditLog(); + if (audit) { + auditLog = new AuditLogService(); + } } @SuppressWarnings("unchecked") diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/util/AuditLogService.java b/activemq-core/src/main/java/org/apache/activemq/broker/util/AuditLogService.java new file mode 100644 index 0000000000..bfd71c8012 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/broker/util/AuditLogService.java @@ -0,0 +1,42 @@ +/** + * 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.ServiceLoader; + +public class AuditLogService { + + private ArrayList auditLogs = new ArrayList(); + + public AuditLogService() { + ServiceLoader 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()); + } + } + + public void log(String message) { + for (AuditLog log : auditLogs) { + log.log(message); + } + } +} diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/util/DefaultAuditLog.java b/activemq-core/src/main/java/org/apache/activemq/broker/util/DefaultAuditLog.java index a776b5f37e..bfe6680535 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/util/DefaultAuditLog.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/util/DefaultAuditLog.java @@ -23,18 +23,6 @@ public class DefaultAuditLog implements AuditLog { private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit"); - public static AuditLog getAuditLog() { - String auditLogClass = System.getProperty("org.apache.activemq.audit.class", "org.apache.activemq.broker.util.DefaultAuditLog"); - AuditLog log; - try { - log = (AuditLog) Class.forName(auditLogClass).newInstance(); - } catch (Exception e) { - LOG.warn("Cannot instantiate audit log class '" + auditLogClass + "', using default audit log", e); - log = new DefaultAuditLog(); - } - return log; - } - public void log(String message) { LOG.info(message); } diff --git a/activemq-web/src/main/java/org/apache/activemq/web/AuditFilter.java b/activemq-web/src/main/java/org/apache/activemq/web/AuditFilter.java index 4b91c82587..8ffb71ed2c 100644 --- a/activemq-web/src/main/java/org/apache/activemq/web/AuditFilter.java +++ b/activemq-web/src/main/java/org/apache/activemq/web/AuditFilter.java @@ -17,6 +17,7 @@ package org.apache.activemq.web; import org.apache.activemq.broker.util.AuditLog; +import org.apache.activemq.broker.util.AuditLogService; import org.apache.activemq.broker.util.DefaultAuditLog; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -31,11 +32,11 @@ public class AuditFilter implements Filter { private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit"); private boolean audit; - private AuditLog auditLog; + private AuditLogService auditLog; public void init(FilterConfig filterConfig) throws ServletException { audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit")); - auditLog = DefaultAuditLog.getAuditLog(); + auditLog = new AuditLogService(); } public void destroy() {