| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | import java.util.logging.Level; |
|---|
| 36 | import java.util.logging.Logger; |
|---|
| 37 | |
|---|
| 38 | import javax.jms.Connection; |
|---|
| 39 | import javax.jms.JMSException; |
|---|
| 40 | import javax.jms.MessageProducer; |
|---|
| 41 | import javax.jms.Session; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Collects some JMS utility functions. |
|---|
| 45 | * |
|---|
| 46 | */ |
|---|
| 47 | public final class JmsUtil |
|---|
| 48 | { |
|---|
| 49 | /** The full qualified name of this class. */ |
|---|
| 50 | private static final String CLASSNAME = JmsUtil.class.getName(); |
|---|
| 51 | |
|---|
| 52 | /** The logger to use. */ |
|---|
| 53 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Constructor. |
|---|
| 57 | */ |
|---|
| 58 | private JmsUtil () |
|---|
| 59 | { |
|---|
| 60 | // Utility class -- only static methods |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Closes the jms session (safe). |
|---|
| 65 | * |
|---|
| 66 | * This method tries to close the given session and if an |
|---|
| 67 | * {@link JMSException} occurs a message with the level {@link Level#FINE} is |
|---|
| 68 | * logged. It's safe to pass a <code>null</code> reference for the argument. |
|---|
| 69 | * |
|---|
| 70 | * @param session The jms session that should be closed. |
|---|
| 71 | */ |
|---|
| 72 | public static void close (Session session) |
|---|
| 73 | { |
|---|
| 74 | if (session != null) |
|---|
| 75 | { |
|---|
| 76 | try |
|---|
| 77 | { |
|---|
| 78 | session.close(); |
|---|
| 79 | } |
|---|
| 80 | catch (JMSException e) |
|---|
| 81 | { |
|---|
| 82 | logCloseFailedWarningMessage( |
|---|
| 83 | Session.class, session.getClass(), e); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Closes the jms connection (safe). |
|---|
| 90 | * |
|---|
| 91 | * This method tries to close the given connection and if an |
|---|
| 92 | * {@link JMSException} occurs a message with the level {@link Level#FINE} is |
|---|
| 93 | * logged. It's safe to pass a <code>null</code> reference for the argument. |
|---|
| 94 | * |
|---|
| 95 | * @param connection The connection to be closed. |
|---|
| 96 | */ |
|---|
| 97 | public static void close (Connection connection) |
|---|
| 98 | { |
|---|
| 99 | if (connection != null) |
|---|
| 100 | { |
|---|
| 101 | try |
|---|
| 102 | { |
|---|
| 103 | connection.close(); |
|---|
| 104 | } |
|---|
| 105 | catch (JMSException e) |
|---|
| 106 | { |
|---|
| 107 | logCloseFailedWarningMessage( |
|---|
| 108 | Connection.class, connection.getClass(), e); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Closes the jms message producer (safe). |
|---|
| 115 | * |
|---|
| 116 | * This method tries to close the given message producer and if an |
|---|
| 117 | * {@link JMSException} occurs a message with the level {@link Level#FINE} is |
|---|
| 118 | * logged. It's safe to pass a <code>null</code> reference for the argument. |
|---|
| 119 | * |
|---|
| 120 | * @param producer The message producer to be closed. |
|---|
| 121 | */ |
|---|
| 122 | public static void close (MessageProducer producer) |
|---|
| 123 | { |
|---|
| 124 | if (producer != null) |
|---|
| 125 | { |
|---|
| 126 | try |
|---|
| 127 | { |
|---|
| 128 | producer.close(); |
|---|
| 129 | } |
|---|
| 130 | catch (JMSException e) |
|---|
| 131 | { |
|---|
| 132 | logCloseFailedWarningMessage( |
|---|
| 133 | MessageProducer.class, producer.getClass(), e); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | private static void logCloseFailedWarningMessage (Class resource, |
|---|
| 139 | Class clazz, JMSException x) |
|---|
| 140 | { |
|---|
| 141 | logger.log(Level.FINE, "Error while closing " + resource.getName() + ": " |
|---|
| 142 | + clazz.getName() + ".close()", x); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|