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 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="connectionFactory"
030 *
031 *
032 */
033public class ActiveMQConnectionFactory extends org.apache.activemq.ActiveMQConnectionFactory implements BeanNameAware {
034
035    private String beanName;
036    private boolean useBeanNameAsClientIdPrefix;
037
038    /**
039     * JSR-250 callback wrapper; converts checked exceptions to runtime exceptions
040     *
041     * delegates to afterPropertiesSet, done to prevent backwards incompatible signature change.
042     */
043    @PostConstruct
044    private void postConstruct() {
045        try {
046            afterPropertiesSet();
047        } catch (Exception ex) {
048            throw new RuntimeException(ex);
049        }
050    }
051
052    /**
053     * @throws Exception
054     * @org.apache.xbean.InitMethod
055     */
056    public void afterPropertiesSet() throws Exception {
057        if (isUseBeanNameAsClientIdPrefix() && getClientIDPrefix() == null) {
058            setClientIDPrefix(getBeanName());
059        }
060    }
061
062    public String getBeanName() {
063        return beanName;
064    }
065
066    @Override
067    public void setBeanName(String beanName) {
068        this.beanName = beanName;
069    }
070
071    public boolean isUseBeanNameAsClientIdPrefix() {
072        return useBeanNameAsClientIdPrefix;
073    }
074
075    public void setUseBeanNameAsClientIdPrefix(boolean useBeanNameAsClientIdPrefix) {
076        this.useBeanNameAsClientIdPrefix = useBeanNameAsClientIdPrefix;
077    }
078}