Package org.activemq.jca

Source Code of org.activemq.jca.JCAConnector

/**
*
* Copyright 2004 Protique Ltd
*
* Licensed 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.activemq.jca;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.jms.MessageListener;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.BootstrapContext;
import javax.resource.spi.ResourceAdapter;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.TransactionManager;

/**
* A Lightweight Message Driven Object Container which is a
* lightweight alternative to MDBs.
*
* @version $Revision: 1.1.1.1 $
*/
public class JCAConnector implements InitializingBean, DisposableBean, BeanFactoryAware {
    private static final transient Log log = LogFactory.getLog(JCAConnector.class);

    private ActivationSpec activationSpec;
    private BootstrapContext bootstrapContext;
    private MessageEndpointFactory endpointFactory;
    private ResourceAdapter resourceAdapter;
    private String ref;
    private TransactionManager transactionManager;
    private BeanFactory beanFactory;

    public JCAConnector() {
    }

    public JCAConnector(BootstrapContext bootstrapContext, ResourceAdapter resourceAdapter) {
        this.bootstrapContext = bootstrapContext;
        this.resourceAdapter = resourceAdapter;
    }

    public void afterPropertiesSet() throws Exception {
        if (activationSpec == null) {
            throw new IllegalArgumentException("activationSpec must be set");
        }

        ResourceAdapter temp = activationSpec.getResourceAdapter();
        if (temp == null && resourceAdapter != null) {
            activationSpec.setResourceAdapter(resourceAdapter);
        }
        else if (resourceAdapter == null) {
            resourceAdapter = activationSpec.getResourceAdapter();
            if (resourceAdapter == null) {
                throw new IllegalArgumentException("resourceAdapter property must be set on the activationSpec object");
            }
        }
        if (bootstrapContext == null) {
            throw new IllegalArgumentException("bootstrapContext must be set");
        }
        if (endpointFactory == null) {
            if (ref == null) {
                throw new IllegalArgumentException("either the endpointFactory or ref properties must be set");
            }
            if (transactionManager != null) {
                endpointFactory = new DefaultEndpointFactory(beanFactory, ref, transactionManager);
            }
            else {
                // TODO should we have some way of finding a ManagedConnection or other local transaction hook?
                endpointFactory = new DefaultEndpointFactory(beanFactory, ref);
            }
        }
        log.info("Activating endpoint for activationSpec: " + activationSpec);
        resourceAdapter.endpointActivation(endpointFactory, activationSpec);
    }

    public void destroy() throws Exception {
        if (resourceAdapter != null && activationSpec != null) {
            resourceAdapter.endpointDeactivation(endpointFactory, activationSpec);
        }
    }

    // Properties
    //-------------------------------------------------------------------------
    public ActivationSpec getActivationSpec() {
        return activationSpec;
    }

    public void setActivationSpec(ActivationSpec activationSpec) {
        this.activationSpec = activationSpec;
    }

    /**
     * Returns the name of the MessageListener POJO in Spring
     */
    public String getRef() {
        return ref;
    }

    /**
     * Sets the name of the MessageListener POJO in Spring
     */
    public void setRef(String ref) {
        this.ref = ref;
    }

    public MessageEndpointFactory getEndpointFactory() {
        return endpointFactory;
    }

    public void setEndpointFactory(MessageEndpointFactory endpointFactory) {
        this.endpointFactory = endpointFactory;
    }

    public BootstrapContext getBootstrapContext() {
        return bootstrapContext;
    }

    public void setBootstrapContext(BootstrapContext bootstrapContext) {
        this.bootstrapContext = bootstrapContext;
    }

    public ResourceAdapter getResourceAdapter() {
        return resourceAdapter;
    }

    public void setResourceAdapter(ResourceAdapter resourceAdapter) {
        this.resourceAdapter = resourceAdapter;
    }

    public TransactionManager getTransactionManager() {
        return transactionManager;
    }

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }
}
TOP

Related Classes of org.activemq.jca.JCAConnector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.