org.jbasics.jms.JMSConnectionDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbasics Show documentation
Show all versions of jbasics Show documentation
jBasics is a collection of useful utility classes for Java. This includes helper for XML, mathematic functions,
restful web services helper, pattern oriented programming interfaces and more. Currently Java7 and up is
supported. Version 1.0 will required at leaset Java8.
/*
* Copyright (c) 2009-2015
* IT-Consulting Stephan Schloepke (http://www.schloepke.de/)
* klemm software consulting Mirko Klemm (http://www.klemm-scs.com/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jbasics.jms;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.XAConnectionFactory;
import org.jbasics.checker.ContractCheck;
import org.jbasics.exception.DelegatedException;
import org.jbasics.pattern.delegation.Delegate;
import org.jbasics.pattern.delegation.ReleasableDelegate;
import org.jbasics.types.tuples.Tuple;
public class JMSConnectionDelegate implements ReleasableDelegate, AutoCloseable {
private final Delegate connectionFactoryDelegate;
private final Delegate extends Tuple> credentialsDelegate;
private Connection connection;
public static JMSConnectionDelegate createForConnectionFactory(Delegate connectionFactoryDelegate) {
return new JMSConnectionDelegate(connectionFactoryDelegate, null);
}
public static JMSConnectionDelegate createForConnectionFactory(Delegate connectionFactoryDelegate, Delegate extends Tuple> credentialsDelegate) {
return new JMSConnectionDelegate(connectionFactoryDelegate, credentialsDelegate);
}
public JMSConnectionDelegate(Delegate connectionFactoryDelegate, Delegate extends Tuple> credentialsDelegate) {
this.connectionFactoryDelegate = ContractCheck.mustNotBeNull(connectionFactoryDelegate, "connectionFactoryDelegate");
this.credentialsDelegate = credentialsDelegate;
}
public JMSSessionDelegate createSessionDelegate(JMSSessionMode sessionMode) {
return JMSSessionDelegate.createForConnection(this, sessionMode);
}
@Override
public Connection delegate() {
if (this.connection == null) {
try {
final ConnectionFactory factory = connectionFactoryDelegate.delegate();
if (this.credentialsDelegate != null) {
Tuple c = credentialsDelegate.delegate();
this.connection = factory.createConnection(c.left(), c.right());
} else {
if (factory instanceof XAConnectionFactory) {
this.connection = ((XAConnectionFactory) factory).createXAConnection();
} else {
this.connection = factory.createConnection();
}
}
} catch (JMSException e) {
throw DelegatedException.delegate(e);
}
}
return connection;
}
@Override
public boolean release() {
try {
close();
} catch(JMSException e) {
return false;
}
return true;
}
@Override
public void close() throws JMSException {
if (this.connection != null) {
this.connection.close();
this.connection = null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy