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 java.util.Properties;
020import javax.naming.NamingException;
021import javax.naming.Reference;
022import org.apache.activemq.ActiveMQConnectionFactory;
023import org.apache.activemq.jndi.JNDIReferenceFactory;
024import org.apache.activemq.jndi.JNDIStorableInterface;
025import org.apache.activemq.pool.PooledConnectionFactory;
026
027/**
028* AmqJNDIPooledConnectionFactory.java
029* Created by linus on 2008-03-07.
030*/
031public class AmqJNDIPooledConnectionFactory extends PooledConnectionFactory
032        implements JNDIStorableInterface {
033    private Properties properties;
034
035    public AmqJNDIPooledConnectionFactory() {
036        super();
037    }
038    
039    public AmqJNDIPooledConnectionFactory(String brokerURL) {
040        super(brokerURL);
041    }
042
043    public AmqJNDIPooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
044        super(connectionFactory);
045    }
046
047    /**
048     * set the properties for this instance as retrieved from JNDI
049     * 
050     * @param props
051     */
052    public synchronized void setProperties(Properties props) {
053        this.properties = props;
054        buildFromProperties(props);
055    }
056
057    /**
058     * Get the properties from this instance for storing in JNDI
059     * 
060     * @return the properties
061     */
062    public synchronized Properties getProperties() {
063        if (this.properties == null) {
064            this.properties = new Properties();
065        }
066        populateProperties(this.properties);
067        return this.properties;
068    }
069
070    /**
071     * Retrive a Reference for this instance to store in JNDI
072     * 
073     * @return the built Reference
074     * @throws NamingException
075     *             if error on building Reference
076     */
077    public Reference getReference() throws NamingException {
078        return JNDIReferenceFactory.createReference(this.getClass().getName(),
079                this);
080    }
081
082    public void buildFromProperties(Properties properties) {
083        if (properties == null) {
084            properties = new Properties();
085        }
086        ((ActiveMQConnectionFactory) getConnectionFactory())
087                .buildFromProperties(properties);
088        String temp = properties.getProperty("maximumActive");
089        if (temp != null && temp.length() > 0) {
090            setMaximumActive(Integer.parseInt(temp));
091        }
092        temp = properties.getProperty("maxConnections");
093        if (temp != null && temp.length() > 0) {
094            setMaxConnections(Integer.parseInt(temp));
095        }
096    }
097
098    public void populateProperties(Properties props) {
099        ((ActiveMQConnectionFactory) getConnectionFactory())
100                .populateProperties(props);
101        props
102                .setProperty("maximumActive", Integer
103                        .toString(getMaximumActive()));
104        props.setProperty("maxConnections", Integer
105                .toString(getMaxConnections()));
106    }
107}