|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.core.appender.mom.jakarta; |
| 18 | + |
| 19 | +import jakarta.jms.JMSException; |
| 20 | +import java.io.Serializable; |
| 21 | +import java.util.Properties; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import org.apache.logging.log4j.core.Appender; |
| 24 | +import org.apache.logging.log4j.core.Filter; |
| 25 | +import org.apache.logging.log4j.core.Layout; |
| 26 | +import org.apache.logging.log4j.core.LogEvent; |
| 27 | +import org.apache.logging.log4j.core.appender.AbstractAppender; |
| 28 | +import org.apache.logging.log4j.core.appender.AbstractManager; |
| 29 | +import org.apache.logging.log4j.core.appender.mom.jakarta.JmsManager.JmsManagerConfiguration; |
| 30 | +import org.apache.logging.log4j.core.config.Node; |
| 31 | +import org.apache.logging.log4j.core.config.Property; |
| 32 | +import org.apache.logging.log4j.core.config.plugins.Plugin; |
| 33 | +import org.apache.logging.log4j.core.config.plugins.PluginAliases; |
| 34 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; |
| 35 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; |
| 36 | +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; |
| 37 | +import org.apache.logging.log4j.core.net.JndiManager; |
| 38 | + |
| 39 | +/** |
| 40 | + * Jakarta JMS Appender plugin for both queues and topics. |
| 41 | + */ |
| 42 | +@Plugin(name = "JMS-Jakarta", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true) |
| 43 | +public final class JmsAppender extends AbstractAppender { |
| 44 | + |
| 45 | + public static final class Builder extends AbstractAppender.Builder<Builder> |
| 46 | + implements org.apache.logging.log4j.core.util.Builder<JmsAppender> { |
| 47 | + |
| 48 | + public static final int DEFAULT_RECONNECT_INTERVAL_MILLIS = 5000; |
| 49 | + |
| 50 | + @PluginBuilderAttribute |
| 51 | + private String factoryName; |
| 52 | + |
| 53 | + @PluginBuilderAttribute |
| 54 | + private String providerUrl; |
| 55 | + |
| 56 | + @PluginBuilderAttribute |
| 57 | + private String urlPkgPrefixes; |
| 58 | + |
| 59 | + @PluginBuilderAttribute |
| 60 | + private String securityPrincipalName; |
| 61 | + |
| 62 | + @PluginBuilderAttribute(sensitive = true) |
| 63 | + private String securityCredentials; |
| 64 | + |
| 65 | + @PluginBuilderAttribute |
| 66 | + @Required(message = "A jakarta.jms.ConnectionFactory JNDI name must be specified") |
| 67 | + private String factoryBindingName; |
| 68 | + |
| 69 | + @PluginBuilderAttribute |
| 70 | + @PluginAliases({"queueBindingName", "topicBindingName"}) |
| 71 | + @Required(message = "A jakarta.jms.Destination JNDI name must be specified") |
| 72 | + private String destinationBindingName; |
| 73 | + |
| 74 | + @PluginBuilderAttribute |
| 75 | + private String userName; |
| 76 | + |
| 77 | + @PluginBuilderAttribute(sensitive = true) |
| 78 | + private char[] password; |
| 79 | + |
| 80 | + @PluginBuilderAttribute |
| 81 | + private long reconnectIntervalMillis = DEFAULT_RECONNECT_INTERVAL_MILLIS; |
| 82 | + |
| 83 | + @PluginBuilderAttribute |
| 84 | + private boolean immediateFail; |
| 85 | + |
| 86 | + // Programmatic access only for now. |
| 87 | + private JmsManager jmsManager; |
| 88 | + |
| 89 | + private Builder() {} |
| 90 | + |
| 91 | + @SuppressWarnings("resource") // actualJmsManager and jndiManager are managed by the JmsAppender |
| 92 | + @Override |
| 93 | + public JmsAppender build() { |
| 94 | + JmsManager actualJmsManager = jmsManager; |
| 95 | + JmsManagerConfiguration configuration = null; |
| 96 | + if (actualJmsManager == null) { |
| 97 | + final Properties jndiProperties = JndiManager.createProperties( |
| 98 | + factoryName, providerUrl, urlPkgPrefixes, securityPrincipalName, securityCredentials, null); |
| 99 | + configuration = new JmsManagerConfiguration( |
| 100 | + jndiProperties, |
| 101 | + factoryBindingName, |
| 102 | + destinationBindingName, |
| 103 | + userName, |
| 104 | + password, |
| 105 | + immediateFail, |
| 106 | + reconnectIntervalMillis); |
| 107 | + actualJmsManager = AbstractManager.getManager(getName(), JmsManager.FACTORY, configuration); |
| 108 | + } |
| 109 | + if (actualJmsManager == null) { |
| 110 | + // JmsManagerFactory has already logged an ERROR. |
| 111 | + return null; |
| 112 | + } |
| 113 | + final Layout<? extends Serializable> layout = getLayout(); |
| 114 | + if (layout == null) { |
| 115 | + LOGGER.error("No layout provided for JmsAppender"); |
| 116 | + return null; |
| 117 | + } |
| 118 | + try { |
| 119 | + return new JmsAppender( |
| 120 | + getName(), getFilter(), layout, isIgnoreExceptions(), getPropertyArray(), actualJmsManager); |
| 121 | + } catch (final JMSException e) { |
| 122 | + // Never happens since the ctor no longer actually throws a JMSException. |
| 123 | + throw new IllegalStateException(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public Builder setDestinationBindingName(final String destinationBindingName) { |
| 128 | + this.destinationBindingName = destinationBindingName; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + public Builder setFactoryBindingName(final String factoryBindingName) { |
| 133 | + this.factoryBindingName = factoryBindingName; |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + public Builder setFactoryName(final String factoryName) { |
| 138 | + this.factoryName = factoryName; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public Builder setImmediateFail(final boolean immediateFail) { |
| 143 | + this.immediateFail = immediateFail; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public Builder setJmsManager(final JmsManager jmsManager) { |
| 148 | + this.jmsManager = jmsManager; |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + public Builder setPassword(final char[] password) { |
| 153 | + this.password = password; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + public Builder setProviderUrl(final String providerUrl) { |
| 158 | + this.providerUrl = providerUrl; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + public Builder setReconnectIntervalMillis(final long reconnectIntervalMillis) { |
| 163 | + this.reconnectIntervalMillis = reconnectIntervalMillis; |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + public Builder setSecurityCredentials(final String securityCredentials) { |
| 168 | + this.securityCredentials = securityCredentials; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + public Builder setSecurityPrincipalName(final String securityPrincipalName) { |
| 173 | + this.securityPrincipalName = securityPrincipalName; |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) { |
| 178 | + this.urlPkgPrefixes = urlPkgPrefixes; |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + public Builder setUserName(final String userName) { |
| 183 | + this.userName = userName; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Does not include the password. |
| 189 | + */ |
| 190 | + @Override |
| 191 | + public String toString() { |
| 192 | + return "Builder [name=" + getName() + ", factoryName=" + factoryName + ", providerUrl=" + providerUrl |
| 193 | + + ", urlPkgPrefixes=" + urlPkgPrefixes + ", securityPrincipalName=" + securityPrincipalName |
| 194 | + + ", securityCredentials=" + securityCredentials + ", factoryBindingName=" + factoryBindingName |
| 195 | + + ", destinationBindingName=" + destinationBindingName + ", username=" + userName + ", layout=" |
| 196 | + + getLayout() + ", filter=" + getFilter() + ", ignoreExceptions=" + isIgnoreExceptions() |
| 197 | + + ", jmsManager=" + jmsManager + "]"; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @PluginBuilderFactory |
| 202 | + public static Builder newBuilder() { |
| 203 | + return new Builder(); |
| 204 | + } |
| 205 | + |
| 206 | + private volatile JmsManager manager; |
| 207 | + |
| 208 | + /** |
| 209 | + * Constructs a new instance. |
| 210 | + * |
| 211 | + * @throws JMSException not thrown as of 2.9 but retained in the signature for compatibility, will be removed in 3.0 |
| 212 | + */ |
| 213 | + private JmsAppender( |
| 214 | + final String name, |
| 215 | + final Filter filter, |
| 216 | + final Layout<? extends Serializable> layout, |
| 217 | + final boolean ignoreExceptions, |
| 218 | + final Property[] properties, |
| 219 | + final JmsManager manager) |
| 220 | + throws JMSException { |
| 221 | + super(name, filter, layout, ignoreExceptions, properties); |
| 222 | + this.manager = manager; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public void append(final LogEvent event) { |
| 227 | + this.manager.send(event, toSerializable(event)); |
| 228 | + } |
| 229 | + |
| 230 | + public JmsManager getManager() { |
| 231 | + return manager; |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public boolean stop(final long timeout, final TimeUnit timeUnit) { |
| 236 | + setStopping(); |
| 237 | + boolean stopped = super.stop(timeout, timeUnit, false); |
| 238 | + stopped &= this.manager.stop(timeout, timeUnit); |
| 239 | + setStopped(); |
| 240 | + return stopped; |
| 241 | + } |
| 242 | +} |
0 commit comments