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