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.pool;
018
019import javax.annotation.PostConstruct;
020import javax.annotation.PreDestroy;
021import javax.jms.ConnectionFactory;
022import javax.transaction.TransactionManager;
023//import org.apache.activemq.jms.pool.PooledConnectionFactory;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.beans.factory.FactoryBean;
027
028/**
029 * Simple factory bean used to create a jencks connection pool.
030 * Depending on the properties set, it will create a simple pool,
031 * a transaction aware connection pool, or a jca aware connection pool.
032 *
033 * <pre class="code">
034 * <bean id="pooledConnectionFactory" class="javax.script.ScriptEngineFactory.PooledConnectionFactoryFactoryBean">
035 *   <property name="connectionFactory" ref="connectionFactory" />
036 *   <property name="transactionManager" ref="transactionManager" />
037 *   <property name="resourceName" value="ResourceName" />
038 * </bean>
039 * </pre>
040 *
041 * The <code>resourceName</code> property should be used along with the {@link org.apache.activemq.jms.pool.GenericResourceManager} and have
042 * the same value than its <code>resourceName</code> property. This will make sure the transaction manager
043 * maps correctly the connection factory to the recovery process.
044 *
045 * @org.apache.xbean.XBean
046 */
047public class PooledConnectionFactoryBean implements FactoryBean {
048
049    private static final Logger LOGGER = LoggerFactory.getLogger(PooledConnectionFactoryBean.class);
050
051    private Object pooledConnectionFactory = null;
052    private ConnectionFactory connectionFactory;
053    private int maxConnections = 1;
054    private int maximumActive = 500;
055    private Object transactionManager;
056    private String resourceName;
057
058    public int getMaxConnections() {
059        return maxConnections;
060    }
061
062    public void setMaxConnections(int maxConnections) {
063        this.maxConnections = maxConnections;
064    }
065
066    public int getMaximumActive() {
067        return maximumActive;
068    }
069
070    public void setMaximumActive(int maximumActive) {
071        this.maximumActive = maximumActive;
072    }
073
074    public Object getTransactionManager() {
075        return transactionManager;
076    }
077
078    public void setTransactionManager(Object transactionManager) {
079        this.transactionManager = transactionManager;
080    }
081
082    public String getResourceName() {
083        return resourceName;
084    }
085
086    public void setResourceName(String resourceName) {
087        this.resourceName = resourceName;
088    }
089
090    public ConnectionFactory getConnectionFactory() {
091        return connectionFactory;
092    }
093
094    public void setConnectionFactory(ConnectionFactory connectionFactory) {
095        this.connectionFactory = connectionFactory;
096    }
097
098    /**
099     * JSR-250 callback wrapper; converts checked exceptions to runtime exceptions
100     *
101     * delegates to afterPropertiesSet, done to prevent backwards incompatible signature change.
102     */
103    @PostConstruct
104    private void postConstruct() {
105        try {
106            afterPropertiesSet();
107        } catch (Exception ex) {
108            throw new RuntimeException(ex);
109        }
110    }
111
112    /**
113     *
114     * @throws Exception
115     * @org.apache.xbean.InitMethod
116     */
117    public void afterPropertiesSet() throws Exception {
118        /*if (pooledConnectionFactory == null && transactionManager != null && resourceName != null) {
119            try {
120                LOGGER.debug("Trying to build a JcaPooledConnectionFactory");
121                JcaPooledConnectionFactory f = new JcaPooledConnectionFactory();
122                f.setName(resourceName);
123                f.setTransactionManager((TransactionManager) transactionManager);
124                f.setMaxConnections(maxConnections);
125                f.setMaximumActiveSessionPerConnection(maximumActive);
126                f.setConnectionFactory(connectionFactory);
127                this.pooledConnectionFactory = f;
128            } catch (Throwable t) {
129                LOGGER.debug("Could not create JCA enabled connection factory: " + t, t);
130            }
131        }
132        if (pooledConnectionFactory == null && transactionManager != null) {
133            try {
134                LOGGER.debug("Trying to build a XaPooledConnectionFactory");
135                XaPooledConnectionFactory f = new XaPooledConnectionFactory();
136                f.setTransactionManager((TransactionManager) transactionManager);
137                f.setMaxConnections(maxConnections);
138                f.setMaximumActiveSessionPerConnection(maximumActive);
139                f.setConnectionFactory(connectionFactory);
140                this.pooledConnectionFactory = f;
141            } catch (Throwable t) {
142                LOGGER.debug("Could not create XA enabled connection factory: " + t, t);
143            }
144        }
145        if (pooledConnectionFactory == null) {
146            try {
147                LOGGER.debug("Trying to build a PooledConnectionFactory");
148                PooledConnectionFactory f = new PooledConnectionFactory();
149                f.setMaxConnections(maxConnections);
150                f.setMaximumActiveSessionPerConnection(maximumActive);
151                f.setConnectionFactory(connectionFactory);
152                this.pooledConnectionFactory = f;
153            } catch (Throwable t) {
154                LOGGER.debug("Could not create pooled connection factory: " + t, t);
155            }
156        }
157        if (pooledConnectionFactory == null) {
158            throw new IllegalStateException("Unable to create pooled connection factory.  Enable DEBUG log level for more informations");
159        }*/
160    }
161
162    /**
163     * JSR-250 callback wrapper; converts checked exceptions to runtime exceptions
164     *
165     * delegates to destroy, done to prevent backwards incompatible signature change.
166     */
167    @PreDestroy
168    private void preDestroy() {
169        try {
170            destroy();
171        } catch (Exception ex) {
172            throw new RuntimeException(ex);
173        }
174    }
175
176    /**
177     *
178     * @throws Exception
179     * @org.apache.xbean.DestroyMethod
180     */
181    public void destroy() throws Exception {
182        if (pooledConnectionFactory != null) {
183            pooledConnectionFactory = null;
184        }
185    }
186
187    @Override
188    public Object getObject() throws Exception {
189        // in case spring-dm calls getObject before this bean has been initialized
190        if (pooledConnectionFactory == null) {
191            afterPropertiesSet();
192        }
193        return pooledConnectionFactory;
194    }
195
196    @Override
197    public Class getObjectType() {
198        return ConnectionFactory.class;
199    }
200
201    @Override
202    public boolean isSingleton() {
203        return true;
204    }
205}