Return-Path: X-Original-To: apmail-logging-commits-archive@minotaur.apache.org Delivered-To: apmail-logging-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AB86111C7F for ; Mon, 7 Jul 2014 04:38:32 +0000 (UTC) Received: (qmail 25436 invoked by uid 500); 7 Jul 2014 04:38:32 -0000 Delivered-To: apmail-logging-commits-archive@logging.apache.org Received: (qmail 25415 invoked by uid 500); 7 Jul 2014 04:38:32 -0000 Mailing-List: contact commits-help@logging.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@logging.apache.org Delivered-To: mailing list commits@logging.apache.org Received: (qmail 25403 invoked by uid 99); 7 Jul 2014 04:38:32 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Jul 2014 04:38:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Jul 2014 04:38:33 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 22E7723889EA; Mon, 7 Jul 2014 04:38:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1608335 - /logging/log4j/log4j2/branches/messaging-module/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java Date: Mon, 07 Jul 2014 04:38:08 -0000 To: commits@logging.apache.org From: mattsicker@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140707043808.22E7723889EA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mattsicker Date: Mon Jul 7 04:38:07 2014 New Revision: 1608335 URL: http://svn.apache.org/r1608335 Log: Add JNDI manager class for use with JMS. Added: logging/log4j/log4j2/branches/messaging-module/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java Added: logging/log4j/log4j2/branches/messaging-module/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/branches/messaging-module/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java?rev=1608335&view=auto ============================================================================== --- logging/log4j/log4j2/branches/messaging-module/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java (added) +++ logging/log4j/log4j2/branches/messaging-module/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java Mon Jul 7 04:38:07 2014 @@ -0,0 +1,105 @@ +/* + * 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.logging.log4j.mom.jms.manager; + +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.appender.AbstractManager; +import org.apache.logging.log4j.core.appender.ManagerFactory; +import org.apache.logging.log4j.core.util.Closer; +import org.apache.logging.log4j.status.StatusLogger; + +/** + * JNDI context manager. Mainly useful for finding JMS objects and configuring the InitialContext. + */ +public class JndiManager extends AbstractManager { + + private static final Logger LOGGER = StatusLogger.getLogger(); + + private static final JndiManagerFactory FACTORY = new JndiManagerFactory(); + + private final Context context; + + private JndiManager(String name, Context context) { + super(name); + this.context = context; + } + + public static JndiManager getJndiManager(final String initialContextFactoryName, + final String providerURL, + final String urlPkgPrefixes, + final String securityPrincipal, + final String securityCredentials, + final Properties additionalProperties) { + final String name = JndiManager.class.getName() + '@' + JndiManager.class.hashCode(); + if (initialContextFactoryName == null) { + return getManager(name, FACTORY, null); + } + final Properties properties = new Properties(); + properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName); + if (providerURL != null) { + properties.setProperty(Context.PROVIDER_URL, providerURL); + } else { + LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated " + + "provider URL. This is likely to cause problems.", initialContextFactoryName); + } + if (urlPkgPrefixes != null) { + properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes); + } + if (securityPrincipal != null) { + properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal); + if (securityCredentials != null) { + properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials); + } else { + LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.", + securityPrincipal); + } + } + if (additionalProperties != null) { + properties.putAll(additionalProperties); + } + return getManager(name, FACTORY, properties); + } + + @Override + protected void releaseSub() { + Closer.closeSilently(this.context); + } + + @SuppressWarnings("unchecked") + public T lookup(final String name) throws NamingException { + return (T) this.context.lookup(name); + } + + private static class JndiManagerFactory implements ManagerFactory { + + @Override + public JndiManager createManager(String name, Properties data) { + try { + return new JndiManager(name, new InitialContext(data)); + } catch (final NamingException e) { + LOGGER.error("Error creating JNDI InitialContext.", e); + return null; + } + } + } +}