Java源码示例:javax.resource.cci.ConnectionSpec

示例1
@Test
public void testTemplateExecuteInputOutputConnectionSpec() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	ConnectionSpec connectionSpec = mock(ConnectionSpec.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection(connectionSpec)).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);

	ConnectionSpecConnectionFactoryAdapter adapter = new ConnectionSpecConnectionFactoryAdapter();
	adapter.setTargetConnectionFactory(connectionFactory);
	adapter.setConnectionSpec(connectionSpec);
	CciTemplate ct = new CciTemplate(adapter);
	ct.execute(interactionSpec, inputRecord, outputRecord);

	verify(interaction).execute(interactionSpec, inputRecord, outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
示例2
@Test
public void testTemplateExecuteInputOutputConnectionSpec() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	ConnectionSpec connectionSpec = mock(ConnectionSpec.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection(connectionSpec)).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);

	ConnectionSpecConnectionFactoryAdapter adapter = new ConnectionSpecConnectionFactoryAdapter();
	adapter.setTargetConnectionFactory(connectionFactory);
	adapter.setConnectionSpec(connectionSpec);
	CciTemplate ct = new CciTemplate(adapter);
	ct.execute(interactionSpec, inputRecord, outputRecord);

	verify(interaction).execute(interactionSpec, inputRecord, outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
示例3
@Test
public void testTemplateExecuteInputOutputConnectionSpec() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	ConnectionSpec connectionSpec = mock(ConnectionSpec.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection(connectionSpec)).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);

	ConnectionSpecConnectionFactoryAdapter adapter = new ConnectionSpecConnectionFactoryAdapter();
	adapter.setTargetConnectionFactory(connectionFactory);
	adapter.setConnectionSpec(connectionSpec);
	CciTemplate ct = new CciTemplate(adapter);
	ct.execute(interactionSpec, inputRecord, outputRecord);

	verify(interaction).execute(interactionSpec, inputRecord, outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
示例4
public ConnectionSpec createConnectionSpec(final Credentials credentials) {
    Class<?> connSpecClass;
    try {
        connSpecClass = getConnectionSpecClass(CONNECTION_SPEC_IMPL_CLASS_NAME);

        Object[] arguments = new Object[]{
                credentials.getUser(),
                credentials.getPassword(),
                credentials.getLanguage()};
        Class<?>[] parameterTypes = new Class<?>[]{String.class, String.class, String.class};

        return newConnectionSpecInstance(connSpecClass, parameterTypes, arguments);
    } catch (IllegalArgumentException | ClassNotFoundException e) {
        throw new InternalHiberSapException(e.getMessage(), e);
    }
}
 
示例5
public ConnectionSpec createConnectionSpec(Credentials credentials) {
        try {
            Object[] arguments = {
                    credentials.getUser(),
                    credentials.getPassword()
//                    ,
//                    credentials.getLanguage(),
//                    credentials.getClient(),
//                    credentials.getAliasUser(),
//                    credentials.getSsoTicket(),
//                    credentials.getX509Certificate()
            };

            Class<?>[] parameterTypes = new Class<?>[arguments.length];
            Arrays.fill(parameterTypes, String.class);

            Class<?> connSpecClass = getConnectionSpecClass(CONNECTION_SPEC_IMPL_CLASS_NAME);
            return newConnectionSpecInstance(connSpecClass, parameterTypes, arguments);
        } catch (IllegalArgumentException | ClassNotFoundException e) {
            throw new InternalHiberSapException(e.getMessage(), e);
        }
    }
 
示例6
/**
 * Determine whether there is currently a thread-bound ConnectionSpec,
 * using it if available, falling back to the statically specified
 * "connectionSpec" property else.
 * @see #doGetConnection
 */
@Override
public final Connection getConnection() throws ResourceException {
	ConnectionSpec threadSpec = this.threadBoundSpec.get();
	if (threadSpec != null) {
		return doGetConnection(threadSpec);
	}
	else {
		return doGetConnection(this.connectionSpec);
	}
}
 
示例7
/**
 * Construct a new CciTemplate, given a ConnectionFactory to obtain Connections from.
 * Note: This will trigger eager initialization of the exception translator.
 * @param connectionFactory the JCA ConnectionFactory to obtain Connections from
 * @param connectionSpec the CCI ConnectionSpec to obtain Connections for
 * (may be {@code null})
 */
public CciTemplate(ConnectionFactory connectionFactory, @Nullable ConnectionSpec connectionSpec) {
	setConnectionFactory(connectionFactory);
	if (connectionSpec != null) {
		setConnectionSpec(connectionSpec);
	}
	afterPropertiesSet();
}
 
示例8
/**
 * Determine whether there is currently a thread-bound ConnectionSpec,
 * using it if available, falling back to the statically specified
 * "connectionSpec" property else.
 * @see #doGetConnection
 */
@Override
public final Connection getConnection() throws ResourceException {
	ConnectionSpec threadSpec = this.threadBoundSpec.get();
	if (threadSpec != null) {
		return doGetConnection(threadSpec);
	}
	else {
		return doGetConnection(this.connectionSpec);
	}
}
 
示例9
/**
 * Construct a new CciTemplate, given a ConnectionFactory to obtain Connections from.
 * Note: This will trigger eager initialization of the exception translator.
 * @param connectionFactory the JCA ConnectionFactory to obtain Connections from
 * @param connectionSpec the CCI ConnectionSpec to obtain Connections for
 * (may be {@code null})
 */
public CciTemplate(ConnectionFactory connectionFactory, @Nullable ConnectionSpec connectionSpec) {
	setConnectionFactory(connectionFactory);
	if (connectionSpec != null) {
		setConnectionSpec(connectionSpec);
	}
	afterPropertiesSet();
}
 
示例10
/**
 * Determine whether there is currently a thread-bound ConnectionSpec,
 * using it if available, falling back to the statically specified
 * "connectionSpec" property else.
 * @see #doGetConnection
 */
@Override
public final Connection getConnection() throws ResourceException {
	ConnectionSpec threadSpec = this.threadBoundSpec.get();
	if (threadSpec != null) {
		return doGetConnection(threadSpec);
	}
	else {
		return doGetConnection(this.connectionSpec);
	}
}
 
示例11
/**
 * Determine whether there is currently a thread-bound ConnectionSpec,
 * using it if available, falling back to the statically specified
 * "connectionSpec" property else.
 * @see #doGetConnection
 */
@Override
public final Connection getConnection() throws ResourceException {
	ConnectionSpec threadSpec = this.threadBoundSpec.get();
	if (threadSpec != null) {
		return doGetConnection(threadSpec);
	}
	else {
		return doGetConnection(this.connectionSpec);
	}
}
 
示例12
/**
 * This implementation delegates to the {@code getConnection(ConnectionSpec)}
 * method of the target ConnectionFactory, passing in the specified user credentials.
 * If the specified username is empty, it will simply delegate to the standard
 * {@code getConnection()} method of the target ConnectionFactory.
 * @param spec the ConnectionSpec to apply
 * @return the Connection
 * @see javax.resource.cci.ConnectionFactory#getConnection(javax.resource.cci.ConnectionSpec)
 * @see javax.resource.cci.ConnectionFactory#getConnection()
 */
protected Connection doGetConnection(ConnectionSpec spec) throws ResourceException {
	if (getTargetConnectionFactory() == null) {
		throw new IllegalStateException("targetConnectionFactory is required");
	}
	if (spec != null) {
		return getTargetConnectionFactory().getConnection(spec);
	}
	else {
		return getTargetConnectionFactory().getConnection();
	}
}
 
示例13
/**
 * Looks up and returns the specified class. First, it will try to get the class from the
 * context class loader, if not found, it will try to get it from the current class loader.
 *
 * @param connectionSpecClass The fully qualified class name of the ConnectionSpec
 * implementation.
 * @return The Class object of the ConnectionSpec implementation
 * @throws ClassNotFoundException - if the class could not be found.
 * @throws IllegalArgumentException - if the class does not implement
 * javax.resource.cci.ConnectionSpec.
 */
protected Class<? extends ConnectionSpec> getConnectionSpecClass(final String connectionSpecClass)
        throws ClassNotFoundException {
    Class<?> connSpecClass = ReflectionHelper.getClassForName(connectionSpecClass);

    if (!ConnectionSpec.class.isAssignableFrom(connSpecClass)) {
        throw new IllegalArgumentException(
                connSpecClass.getName() + " does not implement " + ConnectionSpec.class);
    }

    //noinspection unchecked
    return (Class<? extends ConnectionSpec>) connSpecClass;
}
 
示例14
private Connection newConnection() {
    try {
        if (credentials == null) {
            return connectionFactory.getConnection();
        } else {
            ConnectionSpec connectionSpec = newConnectionSpecFactory(connectionSpecFactoryName)
                    .createConnectionSpec(credentials);
            return connectionFactory.getConnection(connectionSpec);
        }
    } catch (ResourceException e) {
        throw new HibersapException("Problem creating Connection", e);
    }
}
 
示例15
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	return obtainTargetConnectionFactory().getConnection(connectionSpec);
}
 
示例16
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	throw new NotSupportedException(
			"SingleConnectionFactory does not support custom ConnectionSpec");
}
 
示例17
/**
 * Obtain a Connection from the given ConnectionFactory. Translates ResourceExceptions
 * into the Spring hierarchy of unchecked generic data access exceptions, simplifying
 * calling code and making any exception that is thrown more meaningful.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * @param cf the ConnectionFactory to obtain Connection from
 * @param spec the ConnectionSpec for the desired Connection (may be {@code null}).
 * Note: If this is specified, a new Connection will be obtained for every call,
 * without participating in a shared transactional Connection.
 * @return a CCI Connection from the given ConnectionFactory
 * @throws org.springframework.jca.cci.CannotGetCciConnectionException
 * if the attempt to get a Connection failed
 * @see #releaseConnection
 */
public static Connection getConnection(ConnectionFactory cf, @Nullable ConnectionSpec spec)
		throws CannotGetCciConnectionException {
	try {
		if (spec != null) {
			Assert.notNull(cf, "No ConnectionFactory specified");
			return cf.getConnection(spec);
		}
		else {
			return doGetConnection(cf);
		}
	}
	catch (ResourceException ex) {
		throw new CannotGetCciConnectionException("Could not get CCI Connection", ex);
	}
}
 
示例18
/**
 * Return the CCI ConnectionSpec used by this template, if any.
 */
@Nullable
public ConnectionSpec getConnectionSpec() {
	return this.connectionSpec;
}
 
示例19
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	return obtainTargetConnectionFactory().getConnection(connectionSpec);
}
 
示例20
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	throw new NotSupportedException(
			"SingleConnectionFactory does not support custom ConnectionSpec");
}
 
示例21
/**
 * Obtain a Connection from the given ConnectionFactory. Translates ResourceExceptions
 * into the Spring hierarchy of unchecked generic data access exceptions, simplifying
 * calling code and making any exception that is thrown more meaningful.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * @param cf the ConnectionFactory to obtain Connection from
 * @param spec the ConnectionSpec for the desired Connection (may be {@code null}).
 * Note: If this is specified, a new Connection will be obtained for every call,
 * without participating in a shared transactional Connection.
 * @return a CCI Connection from the given ConnectionFactory
 * @throws org.springframework.jca.cci.CannotGetCciConnectionException
 * if the attempt to get a Connection failed
 * @see #releaseConnection
 */
public static Connection getConnection(ConnectionFactory cf, @Nullable ConnectionSpec spec)
		throws CannotGetCciConnectionException {
	try {
		if (spec != null) {
			Assert.notNull(cf, "No ConnectionFactory specified");
			return cf.getConnection(spec);
		}
		else {
			return doGetConnection(cf);
		}
	}
	catch (ResourceException ex) {
		throw new CannotGetCciConnectionException("Could not get CCI Connection", ex);
	}
}
 
示例22
/**
 * Return the CCI ConnectionSpec used by this template, if any.
 */
@Nullable
public ConnectionSpec getConnectionSpec() {
	return this.connectionSpec;
}
 
示例23
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	return getTargetConnectionFactory().getConnection(connectionSpec);
}
 
示例24
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	throw new NotSupportedException(
			"SingleConnectionFactory does not support custom ConnectionSpec");
}
 
示例25
/**
 * Obtain a Connection from the given ConnectionFactory. Translates ResourceExceptions
 * into the Spring hierarchy of unchecked generic data access exceptions, simplifying
 * calling code and making any exception that is thrown more meaningful.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * @param cf the ConnectionFactory to obtain Connection from
 * @param spec the ConnectionSpec for the desired Connection (may be {@code null}).
 * Note: If this is specified, a new Connection will be obtained for every call,
 * without participating in a shared transactional Connection.
 * @return a CCI Connection from the given ConnectionFactory
 * @throws org.springframework.jca.cci.CannotGetCciConnectionException
 * if the attempt to get a Connection failed
 * @see #releaseConnection
 */
public static Connection getConnection(ConnectionFactory cf, ConnectionSpec spec)
		throws CannotGetCciConnectionException {
	try {
		if (spec != null) {
			Assert.notNull(cf, "No ConnectionFactory specified");
			return cf.getConnection(spec);
		}
		else {
			return doGetConnection(cf);
		}
	}
	catch (ResourceException ex) {
		throw new CannotGetCciConnectionException("Could not get CCI Connection", ex);
	}
}
 
示例26
/**
 * Return the CCI ConnectionSpec used by this template, if any.
 */
public ConnectionSpec getConnectionSpec() {
	return this.connectionSpec;
}
 
示例27
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	return getTargetConnectionFactory().getConnection(connectionSpec);
}
 
示例28
@Override
public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
	throw new NotSupportedException(
			"SingleConnectionFactory does not support custom ConnectionSpec");
}
 
示例29
/**
 * Obtain a Connection from the given ConnectionFactory. Translates ResourceExceptions
 * into the Spring hierarchy of unchecked generic data access exceptions, simplifying
 * calling code and making any exception that is thrown more meaningful.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * @param cf the ConnectionFactory to obtain Connection from
 * @param spec the ConnectionSpec for the desired Connection (may be {@code null}).
 * Note: If this is specified, a new Connection will be obtained for every call,
 * without participating in a shared transactional Connection.
 * @return a CCI Connection from the given ConnectionFactory
 * @throws org.springframework.jca.cci.CannotGetCciConnectionException
 * if the attempt to get a Connection failed
 * @see #releaseConnection
 */
public static Connection getConnection(ConnectionFactory cf, ConnectionSpec spec)
		throws CannotGetCciConnectionException {
	try {
		if (spec != null) {
			Assert.notNull(cf, "No ConnectionFactory specified");
			return cf.getConnection(spec);
		}
		else {
			return doGetConnection(cf);
		}
	}
	catch (ResourceException ex) {
		throw new CannotGetCciConnectionException("Could not get CCI Connection", ex);
	}
}
 
示例30
/**
 * Return the CCI ConnectionSpec used by this template, if any.
 */
public ConnectionSpec getConnectionSpec() {
	return this.connectionSpec;
}