Package org.codehaus.activemq.ra

Source Code of org.codehaus.activemq.ra.ActiveMQResourceAdapter

/**
*
* Copyright 2004 Hiram Chirino
*
* 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.codehaus.activemq.ra;

import java.util.HashMap;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.XAConnection;
import javax.jms.XASession;
import javax.resource.NotSupportedException;
import javax.resource.ResourceException;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.BootstrapContext;
import javax.resource.spi.ResourceAdapter;
import javax.resource.spi.ResourceAdapterInternalException;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.xa.XAResource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.codehaus.activemq.broker.BrokerContainerFactory;
import org.codehaus.activemq.broker.BrokerContainer;
import org.codehaus.activemq.broker.BrokerContext;
import org.codehaus.activemq.XmlConfigHelper;
import org.codehaus.activemq.util.IdGenerator;


/**
* Knows how to connect to one ActiveMQ server.  It can then activate endpoints and deliver
* messages to those enpoints using the connection configure in the resource adapter.
* <p/>
* Must override equals and hashCode (JCA spec 16.4)
*
* @version $Revision: 1.15 $
*/
public class ActiveMQResourceAdapter implements ResourceAdapter {
    private static final Log log = LogFactory.getLog(ActiveMQResourceAdapter.class);

    private static final String ASF_ENDPOINT_WORKER_TYPE = "asf";
    private static final String POLLING_ENDPOINT_WORKER_TYPE = "polling";

    private BootstrapContext bootstrapContext;
    private HashMap endpointWorkers = new HashMap();
    final private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo();
    private String endpointWorkerType = ASF_ENDPOINT_WORKER_TYPE;
    private ActiveMQRAConnectionFactory connectionFactory;
    private BrokerContainer container;

    public ActiveMQResourceAdapter() {
    }

    /**
     * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
     */
    public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException {
        this.bootstrapContext = bootstrapContext;
        if(isUseEmbeddedBroker().booleanValue()) {
            createBroker();
        }
    }

    private void createBroker() throws ResourceAdapterInternalException {
        try {
            BrokerContainerFactory brokerContainerFactory = XmlConfigHelper.createBrokerContainerFactory(getBrokerXmlConfig());
            IdGenerator idgen = new IdGenerator();
            container = brokerContainerFactory.createBrokerContainer(idgen.generateId(), BrokerContext.getInstance());
            container.start();
            connectionFactory = new ActiveMQRAConnectionFactory(container, getServerUrl());
        } catch (JMSException e) {
            log.error(e.toString(), e);
            throw new ResourceAdapterInternalException("Failed to startup an embedded broker", e);
        }
    }

    /**
     */
    public Connection makeConnection() throws JMSException {

        ConnectionFactory connectionFactory = getConnectionFactory();

      String userName = info.getUserName();
      String password = info.getPassword();
      Connection physicalConnection = connectionFactory.createConnection(userName, password);
       
      String clientId = info.getClientid();
        if ( clientId != null ) {
            physicalConnection.setClientID(clientId);
        }
        return physicalConnection;
    }

    /**
     * @param activationSpec
     */
    public Connection makeConnection(ActiveMQActivationSpec activationSpec) throws JMSException {
       
        ActiveMQRAConnectionFactory connectionFactory = getConnectionFactory();

      String userName = defaultValue( activationSpec.getUserName(), info.getUserName() );
      String password = defaultValue( activationSpec.getPassword(), info.getPassword() );
        Connection physicalConnection = connectionFactory.createConnection(userName, password);
        if(activationSpec.isDurableSubscription()) {
            physicalConnection.setClientID(activationSpec.getClientId());
        }     
        return physicalConnection;
       
    }

    /**
     * @return
     */
    private ActiveMQRAConnectionFactory getConnectionFactory() {
        if( connectionFactory == null ) {
          connectionFactory = new ActiveMQRAConnectionFactory(info.getServerUrl());
        }
        return connectionFactory;
    }

    private String defaultValue(String value, String defaultValue) {       
        if ( value!=null )
            return value;
        return defaultValue;
    }

    /**
     * @see javax.resource.spi.ResourceAdapter#stop()
     */
    public void stop() {
        stopBroker();
        this.bootstrapContext = null;
    }

    private void stopBroker() {
        if (container != null) {
            try {
                container.stop();
            } catch (JMSException e) {
                log.warn("Exception while stopping the broker container", e);
            }
        }
    }

    /**
     * @return
     */
    public BootstrapContext getBootstrapContext() {
        return bootstrapContext;
    }

    /**
     * @see javax.resource.spi.ResourceAdapter#endpointActivation(javax.resource.spi.endpoint.MessageEndpointFactory,
            *      javax.resource.spi.ActivationSpec)
     */
    public void endpointActivation(MessageEndpointFactory endpointFactory,
                                   ActivationSpec activationSpec) throws ResourceException {

        //spec section 5.3.3
        if (activationSpec.getResourceAdapter() != this) {
            throw new ResourceException("Activation spec not initialized with this ResourceAdapter instance");
        }

        if (activationSpec.getClass().equals(ActiveMQActivationSpec.class)) {

            ActiveMQEndpointActivationKey key = new ActiveMQEndpointActivationKey(endpointFactory, (ActiveMQActivationSpec) activationSpec);
            // This is weird.. the same endpoint activated twice.. must be a container error.
            if (endpointWorkers.containsKey(key)) {
                throw new IllegalStateException("Endpoint previously activated");
            }

            ActiveMQBaseEndpointWorker worker;
            if (POLLING_ENDPOINT_WORKER_TYPE.equals(getEndpointWorkerType())) {
                worker = new ActiveMQPollingEndpointWorker(this, key);
            }
            else if (ASF_ENDPOINT_WORKER_TYPE.equals(getEndpointWorkerType())) {
                worker = new ActiveMQAsfEndpointWorker(this, key);
            }
            else {
                throw new NotSupportedException("That type of EndpointWorkerType is not supported: " + getEndpointWorkerType());
            }

            endpointWorkers.put(key, worker);
            worker.start();

        }
        else {
            throw new NotSupportedException("That type of ActicationSpec not supported: " + activationSpec.getClass());
        }

    }

    /**
     * @see javax.resource.spi.ResourceAdapter#endpointDeactivation(javax.resource.spi.endpoint.MessageEndpointFactory,
            *      javax.resource.spi.ActivationSpec)
     */
    public void endpointDeactivation(MessageEndpointFactory endpointFactory,
                                     ActivationSpec activationSpec) {

        if (activationSpec.getClass().equals(ActiveMQActivationSpec.class)) {
            ActiveMQEndpointActivationKey key = new ActiveMQEndpointActivationKey(endpointFactory, (ActiveMQActivationSpec) activationSpec);
            ActiveMQBaseEndpointWorker worker = (ActiveMQBaseEndpointWorker) endpointWorkers.get(key);
            if (worker == null) {
                // This is weird.. that endpoint was not activated..  oh well.. this method
                // does not throw exceptions so just return.
                return;
            }
            try {
                worker.stop();
            }
            catch (InterruptedException e) {
                // We interrupted.. we won't throw an exception but will stop waiting for the worker
                // to stop..  we tried our best.  Keep trying to interrupt the thread.
                Thread.currentThread().interrupt();
            }

        }

    }

    /**
     * We only connect to one resource manager per ResourceAdapter instance, so any ActivationSpec
     * will return the same XAResource.
     *
     * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[])
     */
    public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException {
        Connection connection=null;
        try {
            connection = makeConnection();
            if (connection instanceof XAConnection) {
                XASession session = ((XAConnection) connection).createXASession();
                XAResource xaResource = session.getXAResource();
                return new XAResource[]{xaResource};
            }
            else {
                return new XAResource[]{};
            }
        }
        catch (JMSException e) {
            throw new ResourceException(e);
        } finally {
            try {
                connection.close();
            } catch ( Throwable ignore) {}
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    // Java Bean getters and setters for this ResourceAdapter class.
    //
    /////////////////////////////////////////////////////////////////////////

    /**
     * @return
     */
    public String getClientid() {
        return emptyToNull(info.getClientid());
    }

    /**
     * @return
     */
    public String getPassword() {
        return emptyToNull(info.getPassword());
    }

    /**
     * @return
     */
    public String getServerUrl() {
        return info.getServerUrl();
    }

    /**
     * @return
     */
    public String getUserName() {
        return emptyToNull(info.getUserName());
    }

    /**
     * @param clientid
     */
    public void setClientid(String clientid) {
        info.setClientid(clientid);
    }

    /**
     * @param password
     */
    public void setPassword(String password) {
        info.setPassword(password);
    }

    /**
     * @param url
     */
    public void setServerUrl(String url) {
        info.setServerUrl(url);
    }

    /**
     * @param userid
     */
    public void setUserName(String userid) {
        info.setUserName(userid);
    }

    /**
     * @return Returns the endpointWorkerType.
     */
    public String getEndpointWorkerType() {
        return endpointWorkerType;
    }

    /**
     * @param endpointWorkerType The endpointWorkerType to set.
     */
    public void setEndpointWorkerType(String endpointWorkerType) {
        this.endpointWorkerType = endpointWorkerType.toLowerCase();
    }

    public String getBrokerXmlConfig() {
        return info.getBrokerXmlConfig();
    }

    /**
     * Sets the <a href="http://activemq.codehaus.org/Xml+Configuration">XML configuration file</a>
     * used to configure the ActiveMQ broker via Spring if using embedded mode.
     *
     * @param brokerXmlConfig is the filename which is assumed to be on the classpath unless a URL
     *                        is specified. So a value of <code>foo/bar.xml</code> would be assumed to be on the classpath
     *                        whereas <code>file:dir/file.xml</code> would use the file system.
     *                        Any valid URL string is supported.
     * @see #setUseEmbeddedBroker(Boolean)
     */   
    public void setBrokerXmlConfig(String brokerXmlConfig) {
        info.setBrokerXmlConfig(brokerXmlConfig);
    }

    public Boolean isUseEmbeddedBroker() {
        return Boolean.valueOf(info.isUseEmbeddedBroker());
    }

    public void setUseEmbeddedBroker(Boolean useEmbeddedBroker) {
        info.setUseEmbeddedBroker(useEmbeddedBroker.booleanValue());
    }
   
    /**
     * @return Returns the info.
     */
    public ActiveMQConnectionRequestInfo getInfo() {
        return info;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ActiveMQResourceAdapter)) {
            return false;
        }

        final ActiveMQResourceAdapter activeMQResourceAdapter = (ActiveMQResourceAdapter) o;

        if (!endpointWorkerType.equals(activeMQResourceAdapter.endpointWorkerType)) {
            return false;
        }
        if (!info.equals(activeMQResourceAdapter.info)) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result = info.hashCode();
        result = 29 * result + endpointWorkerType.hashCode();
        return result;
    }
   
    private String emptyToNull(String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        return value;
    }
   
}
TOP

Related Classes of org.codehaus.activemq.ra.ActiveMQResourceAdapter

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.