Package org.activemq.ra

Source Code of org.activemq.ra.ActiveMQActivationSpec

/**
*
* 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.activemq.ra;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;
import javax.resource.ResourceException;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.InvalidPropertyException;
import javax.resource.spi.ResourceAdapter;

import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQQueue;
import org.activemq.message.ActiveMQTopic;
import org.activemq.selector.SelectorParser;

/**
* @version $Revision: 1.1.1.1 $ $Date: 2005/03/11 21:15:09 $
*/
public class ActiveMQActivationSpec implements ActivationSpec {

    /** Auto-acknowledge constant for <code>acknowledgeMode</code> property **/
    public static final String AUTO_ACKNOWLEDGE_MODE = "Auto-acknowledge";
    /** Dups-ok-acknowledge constant for <code>acknowledgeMode</code> property * */
    public static final String DUPS_OK_ACKNOWLEDGE_MODE = "Dups-ok-acknowledge";   
    /** Durable constant for <code>subscriptionDurability</code> property * */
    public static final String DURABLE_SUBSCRIPTION = "Durable";
    /** NonDurable constant for <code>subscriptionDurability</code> property * */
    public static final String NON_DURABLE_SUBSCRIPTION = "NonDurable";
   
    public static final int INVALID_ACKNOWLEDGE_MODE = -1;
   
    private ActiveMQResourceAdapter resourceAdapter;
    private String destinationType;
    private String messageSelector;
    private String destination;
    private String acknowledgeMode = AUTO_ACKNOWLEDGE_MODE;
    private String userName;
    private String password;
    private String clientId;
    private String subscriptionName;
    private String subscriptionDurability = NON_DURABLE_SUBSCRIPTION;   
    private String noLocal = "false";
    private String useRAManagedTransaction = "false";
    private String maxSessions="10";
    private String maxMessagesPerSessions="10";
   
   
    /**
     * @see javax.resource.spi.ActivationSpec#validate()
     */
    public void validate() throws InvalidPropertyException {
        List errorMessages = new ArrayList();
        List propsNotSet = new ArrayList();
        try {
            if (!isValidDestination(errorMessages))
                propsNotSet.add(new PropertyDescriptor("destination", ActiveMQActivationSpec.class));
            if (!isValidDestinationType(errorMessages))
                propsNotSet.add(new PropertyDescriptor("destinationType", ActiveMQActivationSpec.class));
            if (!isValidAcknowledgeMode(errorMessages))
                propsNotSet.add(new PropertyDescriptor("acknowledgeMode", ActiveMQActivationSpec.class));
            if (!isValidSubscriptionDurability(errorMessages))
                propsNotSet.add(new PropertyDescriptor("subscriptionDurability", ActiveMQActivationSpec.class));
            if (!isValidClientId(errorMessages))
                propsNotSet.add(new PropertyDescriptor("clientId", ActiveMQActivationSpec.class));
            if (!isValidSubscriptionName(errorMessages))
                propsNotSet.add(new PropertyDescriptor("subscriptionName", ActiveMQActivationSpec.class));
            if (!isValidMaxMessagesPerSessions(errorMessages))
                propsNotSet.add(new PropertyDescriptor("maxMessagesPerSessions", ActiveMQActivationSpec.class));
            if (!isValidMaxSessions(errorMessages))
                propsNotSet.add(new PropertyDescriptor("maxSessions", ActiveMQActivationSpec.class));
            if (!isValidMessageSelector(errorMessages))
                propsNotSet.add(new PropertyDescriptor("messageSelector", ActiveMQActivationSpec.class));
            if (!isValidNoLocal(errorMessages))
                propsNotSet.add(new PropertyDescriptor("noLocal", ActiveMQActivationSpec.class));
            if (!isValidUseRAManagedTransaction(errorMessages))
                propsNotSet.add(new PropertyDescriptor("useRAManagedTransaction", ActiveMQActivationSpec.class));
           
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
       
        if (propsNotSet.size() > 0) {
            StringBuffer b = new StringBuffer();
            b.append("Invalid settings:");
            for (Iterator iter = errorMessages.iterator(); iter.hasNext();) {
                b.append(" ");
                b.append(iter.next());
            }
            InvalidPropertyException e = new InvalidPropertyException(b.toString());
            final PropertyDescriptor[] descriptors = (PropertyDescriptor[]) propsNotSet.toArray(new PropertyDescriptor[propsNotSet.size()]);
            e.setInvalidPropertyDescriptors(descriptors);
            throw e;
        }
    }

    private boolean isValidUseRAManagedTransaction(List errorMessages) {
        try {
            new Boolean(noLocal);
            return true;
        } catch (Throwable e) {
        }
        errorMessages.add("noLocal must be set to: true or false.");
        return false;
    }

    private boolean isValidNoLocal(List errorMessages) {
        try {
            new Boolean(noLocal);
            return true;
        } catch (Throwable e) {
        }
        errorMessages.add("noLocal must be set to: true or false.");
        return false;
    }

    private boolean isValidMessageSelector(List errorMessages) {
        try {
            if( !isEmpty(messageSelector) ) {
                new SelectorParser().parse(messageSelector);
            }
            return true;
        } catch (Throwable e) {
            errorMessages.add("messageSelector not set to valid message selector: "+e.getMessage());
            return false;
        }
    }

    private boolean isValidMaxSessions(List errorMessages) {
        try {
            if( Integer.parseInt(maxSessions) > 0 ) {
                return true;
            }
        } catch (NumberFormatException e) {
        }
        errorMessages.add("maxSessions must be set to number > 0");
        return false;
    }

    private boolean isValidMaxMessagesPerSessions(List errorMessages) {
        try {
            if( Integer.parseInt(maxMessagesPerSessions) > 0 ) {
                return true;
            }
        } catch (NumberFormatException e) {
        }
        errorMessages.add("maxMessagesPerSessions must be set to number > 0");
        return false;
    }

    /**
     * @see javax.resource.spi.ResourceAdapterAssociation#getResourceAdapter()
     */
    public ResourceAdapter getResourceAdapter() {
        return resourceAdapter;
    }

    /**
     * @see javax.resource.spi.ResourceAdapterAssociation#setResourceAdapter(javax.resource.spi.ResourceAdapter)
     */
    public void setResourceAdapter(ResourceAdapter resourceAdapter) throws ResourceException {
        //spec section 5.3.3
        if (this.resourceAdapter != null) {
            throw new ResourceException("ResourceAdapter already set");
        }
        if (!(resourceAdapter instanceof ActiveMQResourceAdapter)) {
            throw new ResourceException("ResourceAdapter is not of type: " + ActiveMQResourceAdapter.class.getName());
        }
        this.resourceAdapter = (ActiveMQResourceAdapter) resourceAdapter;
    }


    /////////////////////////////////////////////////////////////////////////
    //
    // Java Bean getters and setters for this ActivationSpec class.
    //
    /////////////////////////////////////////////////////////////////////////
    /**
     * @return Returns the destinationType.
     */
    public String getDestinationType() {
        if (!isEmpty(destinationType)) {
            return destinationType;
        }
        return null;
    }

    /**
     * @param destinationType The destinationType to set.
     */
    public void setDestinationType(String destinationType) {
        this.destinationType = destinationType;
    }
   
    public String getPassword() {
        if (!isEmpty(password)) {               
            return password;
        }
        return null;
    }
   
    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        if (!isEmpty(userName)) {       
            return userName;
        }
        return null;
    }
   
    public void setUserName(String userName) {
        this.userName = userName;
    }
   
    /**
     * @return Returns the messageSelector.
     */
    public String getMessageSelector() {
        if (!isEmpty(messageSelector)) {
            return messageSelector;
        }
        return null;
    }

    /**
     * @param messageSelector The messageSelector to set.
     */
    public void setMessageSelector(String messageSelector) {
        this.messageSelector = messageSelector;
    }

    /**
     * @return Returns the noLocal.
     */
    public String getNoLocal() {
        return noLocal;
    }

    /**
     * @param noLocal The noLocal to set.
     */
    public void setNoLocal(String noLocal) {
        if( noLocal!=null ) {
            this.noLocal = noLocal;
        }
    }

    public String getAcknowledgeMode() {
        if (!isEmpty(acknowledgeMode)) {
            return acknowledgeMode;
        }
        return null;
    }

    public void setAcknowledgeMode(String acknowledgeMode) {
        this.acknowledgeMode = acknowledgeMode;
    }
   
    public String getClientId() {
        if (!isEmpty(clientId)) {
            return clientId;
        }
        return null;
    }

    public void setClientId(String clientId) {       
        this.clientId = clientId;           
    }

    public String getDestination() {
        if (!isEmpty(destination)) {
            return destination;
        }
        return null;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getSubscriptionDurability() {
        if (!isEmpty(subscriptionDurability)) {               
            return subscriptionDurability;
        }
        return null;
    }

    public void setSubscriptionDurability(String subscriptionDurability) {
        this.subscriptionDurability = subscriptionDurability;
    }

    public String getSubscriptionName() {
        if (!isEmpty(subscriptionName)) {
            return subscriptionName;
        }
        return null;
    }

    public void setSubscriptionName(String subscriptionName) {
        this.subscriptionName = subscriptionName;
    }
   
    private boolean isValidSubscriptionName(List errorMessages) {
        if( !isDurableSubscription() ? true : subscriptionName != null && subscriptionName.trim().length() > 0 ) {
            return true;
        }
        errorMessages.add("subscriptionName must be set since durable subscription was requested.");
        return false;
    }

    private boolean isValidClientId(List errorMessages) {
        if( !isDurableSubscription() ? true : clientId != null && clientId.trim().length() > 0 ) {
            return true;
        }
        errorMessages.add("clientId must be set since durable subscription was requested.");
        return false;
    }

    public boolean isDurableSubscription() {
        return DURABLE_SUBSCRIPTION.equals(subscriptionDurability);
    }

    private boolean isValidSubscriptionDurability(List errorMessages) {
        if (NON_DURABLE_SUBSCRIPTION.equals(subscriptionDurability) || DURABLE_SUBSCRIPTION.equals(subscriptionDurability))
            return true;
        errorMessages.add("subscriptionDurability must be set to: "+NON_DURABLE_SUBSCRIPTION+" or "+DURABLE_SUBSCRIPTION+".");
        return false;
    }

    private boolean isValidAcknowledgeMode(List errorMessages) {
        if (AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) || DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode))
            return true;
        errorMessages.add("acknowledgeMode must be set to: "+AUTO_ACKNOWLEDGE_MODE+" or "+DUPS_OK_ACKNOWLEDGE_MODE+".");
        return false;
    }

    private boolean isValidDestinationType(List errorMessages) {
        if (Queue.class.getName().equals(destinationType) || Topic.class.getName().equals(destinationType))
            return true;
        errorMessages.add("destinationType must be set to: "+Queue.class.getName()+" or "+Topic.class.getName()+".");
        return false;
    }

    private boolean isValidDestination(List errorMessages) {
        if(!(destination == null || destination.equals("")))
            return true;
        errorMessages.add("destination is a required field and must be set to the destination name.");
        return false;
    }
    
    private boolean isEmpty(String value) {
        return value == null || "".equals(value.trim());
    }

   public String toString() {
        return "ActiveMQActivationSpec{" +
                "acknowledgeMode='" + acknowledgeMode + "'" +
                ", destinationType='" + destinationType + "'" +
                ", messageSelector='" + messageSelector + "'" +
                ", destination='" + destination + "'" +
                ", clientId='" + clientId + "'" +
                ", subscriptionName='" + subscriptionName + "'" +
                ", subscriptionDurability='" + subscriptionDurability + "'" +
                "}";
    }

    public int getAcknowledgeModeForSession() {
        if( AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) ) {
            return Session.AUTO_ACKNOWLEDGE;
        } else if( DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode) ) {
            return Session.DUPS_OK_ACKNOWLEDGE;
        } else {
            return INVALID_ACKNOWLEDGE_MODE;
        }
    }

    public ActiveMQDestination createDestination() {
        if( isEmpty(destinationType) || isEmpty(destination) )
            return null;
       
        ActiveMQDestination dest = null;
        if (Queue.class.getName().equals(destinationType)) {
            dest = new ActiveMQQueue(destination);
        } else if (Topic.class.getName().equals(destinationType)) {
            dest = new ActiveMQTopic(destination);
        } else {
            assert false : "Execution should never reach here";
        }
        return dest;
    }

    public String getMaxMessagesPerSessions() {
        return maxMessagesPerSessions.toString();
    }
   
    public void setMaxMessagesPerSessions(String maxMessagesPerSessions) {
        if( maxMessagesPerSessions!=null ) {
            this.maxMessagesPerSessions = maxMessagesPerSessions;
        }
    }
   

    public String getMaxSessions() {
        return maxSessions;
    }
    public void setMaxSessions(String maxSessions) {
        if( maxSessions!=null ) {
            this.maxSessions = maxSessions;
        }
    }
   
    public String getUseRAManagedTransaction() {
        return useRAManagedTransaction;
    }
   
    public void setUseRAManagedTransaction(String useRAManagedTransaction) {
        if( useRAManagedTransaction!=null ) {
            this.useRAManagedTransaction = useRAManagedTransaction;
        }
    }

    public int getMaxMessagesPerSessionsIntValue() {
        return Integer.parseInt(maxMessagesPerSessions);
    }

    public int getMaxSessionsIntValue() {
        return Integer.parseInt(maxSessions);
    }

    public boolean isUseRAManagedTransactionEnabled() {
        return new Boolean(useRAManagedTransaction).booleanValue();
    }

    public boolean getNoLocalBooleanValue() {
        return new Boolean(noLocal).booleanValue();
    }

}
TOP

Related Classes of org.activemq.ra.ActiveMQActivationSpec

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.