001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.spring;
018
019import javax.annotation.PostConstruct;
020
021import org.springframework.beans.factory.BeanNameAware;
022
023/**
024 * A <a href="http://www.springframework.org/">Spring</a> enhanced XA connection
025 * factory which will automatically use the Spring bean name as the clientIDPrefix property
026 * so that connections created have client IDs related to your Spring.xml file for
027 * easier comprehension from <a href="http://activemq.apache.org/jmx.html">JMX</a>.
028 *
029 * @org.apache.xbean.XBean element="xaConnectionFactory"
030 */
031public class ActiveMQXAConnectionFactory extends org.apache.activemq.ActiveMQXAConnectionFactory implements BeanNameAware {
032
033    private String beanName;
034    private boolean useBeanNameAsClientIdPrefix;
035
036    /**
037     * JSR-250 callback wrapper; converts checked exceptions to runtime exceptions
038     *
039     * delegates to afterPropertiesSet, done to prevent backwards incompatible signature change.
040     */
041    @PostConstruct
042    private void postConstruct() {
043        try {
044            afterPropertiesSet();
045        } catch (Exception ex) {
046            throw new RuntimeException(ex);
047        }
048    }
049
050    /**
051     *
052     * @throws Exception
053     * @org.apache.xbean.InitMethod
054     */
055    public void afterPropertiesSet() throws Exception {
056        if (isUseBeanNameAsClientIdPrefix() && getClientIDPrefix() == null) {
057            setClientIDPrefix(getBeanName());
058        }
059    }
060
061    public String getBeanName() {
062        return beanName;
063    }
064
065    @Override
066    public void setBeanName(String beanName) {
067        this.beanName = beanName;
068    }
069
070    public boolean isUseBeanNameAsClientIdPrefix() {
071        return useBeanNameAsClientIdPrefix;
072    }
073
074    public void setUseBeanNameAsClientIdPrefix(boolean useBeanNameAsClientIdPrefix) {
075        this.useBeanNameAsClientIdPrefix = useBeanNameAsClientIdPrefix;
076    }
077}